Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Doesn't Work in Delphi

Posted on 2001-06-13
9
Medium Priority
?
246 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
Industry Leaders: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

604 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