Solved

Component messaging

Posted on 1998-04-29
14
163 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Security Threats Are You Missing?

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.

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

18 Experts available now in Live!

Get 1:1 Help Now