Solved

more dll help

Posted on 2002-07-10
11
192 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
[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
  • 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
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: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 34

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 34

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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