Solved

Doesn't Work in Delphi

Posted on 2001-06-13
9
232 Views
Last Modified: 2010-04-06
Can anyone tell me why this snippet of code works in VB but not in Delphi??

It's a Winpopup message sender.  If you use the function in VB then it sends the message fine.  In Delphi it doesn't send the message.

VB Code
-------

Const MAILSLOT_WAIT_FOREVER = (-1)
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const GENERIC_EXECUTE = &H20000000
Const GENERIC_ALL = &H10000000
Const INVALID_HANDLE_VALUE = -1
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Private Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFileName As Long, ByVal lpBuff As Any, ByVal nNrBytesToWrite As Long, lpNrOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwAccess As Long, ByVal dwShare As Long, ByVal lpSecurityAttrib As Long, ByVal dwCreationDisp As Long, ByVal dwAttributes As Long, ByVal hTemplateFile As Long) As Long


Function SendMsg(From1 As String, To2 As String, Text3 As String) As Long
Dim rc As Long
Dim mshandle As Long
Dim msgtxt As String
Dim byteswritten As Long
Dim mailslotname As String
' name of the mailslot
    mailslotname = "\\" + To2 + "\mailslot\messngr"
    msgtxt = From1 + Chr(0) + To2 + Chr(0) + Text3 + Chr(0)
    mshandle = CreateFile(mailslotname, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, -1)
    rc = WriteFile(mshandle, msgtxt, Len(msgtxt), byteswritten, 0)
    rc = CloseHandle(mshandle)
End Function



Delphi Code
-----------

procedure TForm1.SendNetMessage(YourName, ComputerName,
  MessageStr: String);
Var
   Rc : LongBool;
   MsHandle : Longint;
   MsgText : String;
   BytesWritten : Cardinal;
   MailSlotName : String;
begin
    // Name of the mailslot
    MailSlotName := '\\' + ComputerName + '\mailslot\messngr';
    MsgText := YourName + Chr(0) + ComputerName + Chr(0) + MessageStr + Chr(0);
    MsHandle := CreateFile(PChar(MailSlotName), GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    Rc := WriteFile(MsHandle, MsgText, Length(MsgText), BytesWritten, 0);
    Rc := CloseHandle(MsHandle);
end;



Can anyone fix it or tell me where I went wrong??

Cheers
Woody.
0
Comment
Question by:WoodyJ007
  • 4
  • 4
9 Comments
 
LVL 8

Expert Comment

by:TOndrej
ID: 6186752
Hi Woody,
is MsHandle returned from CreateFile valid or is it INVALID_HANDLE_VALUE?

perhaps you could write it in this way:

MsHandle := CreateFile(...);
if MsHandle = INVALID_HANDLE_VALUE then
  RaiseLastWin32Error;
try
  Rc := WriteFile(...);
  if not Rc then
    RaiseLastWin32Error;
finally
  Rc := CloseHandle(MsHandle);
end;

so you can see an error message if something goes wrong...
0
 

Author Comment

by:WoodyJ007
ID: 6186871
The handle is fine.  

If you have VB or Delphi installed, and your on a network as well. try it out.

The only difference I did was using 0 instead of -1 in the last param of create file.
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6186910
oops... ;-)
Does it work now?

I wish we could write sections marked as <CODE> (rendered in a non-proportional font) so the code posted here is more readable...
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:WoodyJ007
ID: 6186964
No it still doesn't work.  You can't put -1 as the last param because it is cardinal.
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6186974
just typecast:

Cardinal(-1)

that will compile.
0
 

Author Comment

by:WoodyJ007
ID: 6187141
that didn't work either,still doesn't work under delphi.
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6187151
Define "doesn't work"... Does the code compile? If it does, it's time to put in the error checking lines I posted earlier ;-)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 50 total points
ID: 6188097
Woody,

With the understanding that you have a valid handle (from what I can tell from above), the problem lies in the WriteFile().

Switch to:

WriteFile(MsHandle, Pointer(MsgText)^, Length(MsgText), BytesWritten, 0);
   
0
 

Author Comment

by:WoodyJ007
ID: 6190008
Yeah, that works great.  For some reason I thought I was supposed to be putting the string in instead of a pointer to it.

Thanks to everyone else for the help.

Cheers
Woody.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 60
When I am typing into a TEdit last word repeats 7 134
QRReport  TQrmemo vertical stretching 1 46
Delphi problems to abort a printjob 9 19
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question