Solved

Component messaging

Posted on 1998-04-29
14
166 Views
Last Modified: 2010-04-06
I defined my own windows message.  I have a handler procedure.  It works in the form, but not in the component.

The code is as follows:

const
  WM_VVTInterfaceMessage = 1125;

  protected
    procedure WMVVTInterfaceMessage(var Msg: TMessage); message WM_VVTInterfaceMessage;

Additionally, how can I pass a string in the message?
I tried using a PChar in the lParam, but I get GPFs and Delphi exits immediately.

Bryan
0
Comment
Question by:bcrotaz
  • 4
  • 3
  • 2
  • +3
14 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339402
Hi Bryan

What component are you using as your base class?  (I.e. what are you inheriting from?)  I don't think TComponent has an HWND or a WndProc, so that may explain why your msg handler isn't working.

Also, what method are you currently using to pass a PChar?  How are you sending the message in lParam (PostMessage, SendMessage, Perform)?

JB
0
 
LVL 4

Expert Comment

by:d003303
ID: 1339403
Yo,
like JimBob said, you need a window handle and a window proc. Only descendants of TWinControl have these property/method.
If your message is received in the same process, i.e. the same application, you could typecast a PChar variable in the lParam using LongInt(PString) on the sender and PChar(lParam) on the receiver side.
If your message goes out of process boundaries you cannot transfer pointers, only the direct values of wParam and lParam. To get a string through in this scenario, it is getting tricky.

Slash/d003303
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339404
Hi doo3303

You are right about TWinControl having these, but you can use a work around.  Look at TThread in Classes.pas.  In particular, look at "AllocateWindow" which is a nested function inside the procedure "AddThread".

Cheers,
JB
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:bcrotaz
ID: 1339405
OK - I used TControl, so that explains that part.

How could I send a string between processes using a message?
0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1339406
I'm currently using PostMessage, cos I don't want the sender to wait for an answer.

I sent the PChar in the lParam by typecasting it to an longint.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339407
If you use PostMessage, you could have a problem.  With SendMessage you could solve your problem, because the calling procedure will then allocate AND free the string.

JB
0
 
LVL 4

Expert Comment

by:d003303
ID: 1339408
Yo,
maybe you yould use the GetWindowText/SetWindowText API functions to work around these things. It also works across process boundaries. Note that this only works if the window text is not used for its standard purpose, like captioning.

Slash/d003303
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339409
To get messages to your component you need to make an invisible window :

 TCDEvents = class(TComponent)
  private
    { Private declarations }
     FWindowHandle: HWND;
     procedure WndProc(var Msg: TMessage);
  protected
    { Protected declarations }
  public
    { Public declarations }
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
  published
    { Published declarations }
  end;

constructor TCDEvents.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWindowHandle := AllocateHWnd(WndProc);
end;

procedure TCDEvents.WndProc(var Msg: TMessage);
begin
 {handle the messages}
 if message then begin
 end
    else
      Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam,         Msg.lParam);
end;

destructor TCDEvents.Destroy;
begin
  DeallocateHWnd(FWindowHandle);
  inherited Destroy;
end;

Regards, ZiF.


0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1339410
Hmmm... This isn't as simple as it could be.
Is there no way of allocating memory with global permissions, so every process can access it?
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339411
Hi

You can create a global shared memory area using CreateFileMapping.  For an example of this, look for MY comments under the following (unanswered) question:
http://www.experts-exchange.com/topics/comp/lang/delphi/Q.10050013

Cheers,
JB
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339412
Not easy?  Not much code ....

It's the way delphi does it...

Regards, Zif.
0
 
LVL 1

Accepted Solution

by:
ssite earned 100 total points
ID: 1339413
The right way to pass strings betweek apps is to use Atoms. Better yet, global atoms. Look it up in win32 help. It's pretty simple to do, but the string is limited to 255 chars.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1339414
There is a way to pass message to tControl: by calling the dispatch method eg  myControl.dispatch( userMessage);
0
 
LVL 1

Expert Comment

by:ssite
ID: 1339415
Did you use control.perform ?

maybe 1125 is used as an internal mesage in the component. Try WM_MYMESSAGE = WM_USER + 100

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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