Solved

Component messaging

Posted on 1998-04-29
14
168 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
[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
  • 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
Industry Leaders: 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!

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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