Solved

more dll help

Posted on 2002-07-10
11
185 Views
Last Modified: 2010-04-04
App1 can call processes in a dll
App2 can send data directly to App1.
App1 cannot send messages directly to App2. (App1 is not my own creation)

Dll unloads itself between calls (I don't want to keep it loaded as it may not be used frequently enough to warrant that).

Goal:  Event driven messages from App1 to App2 with dll as mediator.

Details:  App1 calls a process in the dll (this already works).  DLL then sends a string to App2 (initiates the event). The help files say Sendmessage() will not work for this but that seems wrong.

Sample code, please, for DLL initiating a message with a string (or PChar) to App2 and create an event in App2 to receive the string.

I already have code for locating the HWND for each app.

Thanks for you time.
Don


0
Comment
Question by:d32coder
  • 6
  • 2
  • 2
  • +1
11 Comments
 

Author Comment

by:d32coder
ID: 7144084
FROM the help file:

...Windows message. OnMessage only receives messages that are posted to the message queue, not those sent directly with the Windows API SendMessage function.
0
 
LVL 17

Expert Comment

by:geobul
ID: 7144229
Hi,
You may use system-wide message defined by yourself and Application.OnMessage to receive it:

in the dll:

// declaration
var TheMessage: cardinal;
// initialization
TheMessage := RegisterWindowMessage(PChar('geobul'));
// sending the message
PostMessage(<WindowHandle>, TheMessage, 0,0);

in the App2:

// in TForm1 class
procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
// declaration
var TheMessage: cardinal;
// in Form1.OnCreate register the same message
TheMessage := RegisterWindowMessage(PChar('geobul'));
Application.OnMessage := AppMessage; // assign the event handler
// and
procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
var
  i,j : integer;
begin
  // our registered custom message received
  if (Msg.Message = TheMessage) then begin
    // do here what you want
    Handled := True;
   end;
end;

Regards, Geo
0
 

Author Comment

by:d32coder
ID: 7144765
If I understand this so far, the 'GeoBul' message is a static message.  Do my all of my messages have to be pre-defined or can I send any run-time-defined PChar(string) in there?

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:d32coder
ID: 7144773
Sorry, that was vague..  What I need is a message that I can attach an undefined string, perhaps as a param.  If GeoBul is the message can I pass a param along with it?  

example
 message = 'GeoBul'  (I realize this is arbitrary)
 param = 'C:\Temp\AnyFile.txt'
 param2 = '64k'

Thanks again,
Don
0
 
LVL 33

Expert Comment

by:Slick812
ID: 7145025
Sending a message from a .DLL to an App that has that .DLL loaded can get information to the app using Pointers. Since the App and the Dll share memory space, pointers are an effective way to get info to or from a .DLL.

you could add a procedure like this to your .DLL

procedure SendPointer(Handle: THandle);
var
SomeText, OtherText: PChar;
Recieved: Boolean
begin
{Handle is the Handle of the App you want to sent this to}
SomeText := 'Attention this is an improtant message, maybe';
OtherText := 'This is a text of the Other';
if SendMessage(Handle, WM_USER+33, Integer(SomeText), Integer(OtherText)) = 33 then
Recieved := True else
Recieved := False;
{you can send other types of variables, just use pointers}
end;


and in your app you have to be sure the .DLL is loaded and get the message to your form as WM_USER+ whatever. Here's a kind of Form message procedure,

procedure TForm1.WindProc(var Message: TMessage);
var
SomeText, OtherText: String;
begin
if Message.Msg = WM_USER+33 then
  begin
  SomeText := PChar(Message.WParam);
  OtherText := PChar(Message.LParam);
  Label3.Caption := SomeText;
  if Length(SomeText) > 0 then
  Message.Result := 33 else
  Message.Result := 0;
  Exit;
  end;

inherited;
end;

- - - -- - - - - - - - - - - - - - --

hope this helps you
0
 

Author Comment

by:d32coder
ID: 7145168
Thanks Slick.  I didn't know that.

"Since the App and the Dll share memory space, pointers are an effective way to get info to or from a .DLL"

Can 2 Apps have the same .dll loaded at the same time or does each app make it's own copy?  If App1 posts data to the dll and App2 has the dll loaded, seems to me like there would be 2 copies of the dll and thus not sharing the same data between the 2 copies.

Hope I'm wrong.

Don


0
 
LVL 33

Expert Comment

by:Slick812
ID: 7145193
As far as I can figure, but I whole Windows system memory management is not entirely clear to me, I think the functional data memory (the .DLL file loaded) is a single copy. There may be some memory separation of function parameters and variables acording to what process calls the function. But I Know two apps can not share info with a pointer sent in a SendMessage.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 7145478
Each process has its own memory/address context. And each process loads its own copy of each dll (except if the dll has shared segments, which you can't do with Delphi, though).

So if app1 loads your dll and app2 loads your dll, that are 2 different copies, which are NOT in the same address/memory context. So just sending a message with a string pointer doesn't work, sorry.

But you can use WM_COPYDATA, which was built just for the purpose to copy data from one process to another. Look at the documentation, it's not too difficult to use.

Sorry, no time to write code right now...

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7145480
P.S: If App1 is not your application, how to you make it load your dll? It's possible, of course, just want to know, whether you really succeeded in that...   :-)
0
 

Author Comment

by:d32coder
ID: 7145501
mIRC has a script language that allows it.  Thanks for the tip on COPYDATA, I'll look into that right now.

Don
0
 

Author Comment

by:d32coder
ID: 7145535
Borland had a FAQ (link below) that shows WM_COPYDATA code.  It does exactly what I needed.  Thanks to all for responses.

http://community.borland.com/article/0,1410,16525,00.html

Don
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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