Solved

Doesn't Work in Delphi

Posted on 2001-06-13
9
229 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now