?
Solved

Doesn't Work in Delphi

Posted on 2001-06-13
9
Medium Priority
?
242 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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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