Solved

more dll help

Posted on 2002-07-10
11
176 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
 

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
Free Trending Threat Insights Every Day

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.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

10 Experts available now in Live!

Get 1:1 Help Now