Solved

Doesn't Work in Delphi

Posted on 2001-06-13
9
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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