Solved

Sending a self made message.

Posted on 1997-06-13
9
147 Views
Last Modified: 2010-04-06
How do I make, send and receive a message in Delphi pascal 2.0 (and Win95) from an application to a thread?
0
Comment
Question by:Lillan061397
  • 5
  • 4
9 Comments
 

Author Comment

by:Lillan061397
Comment Utility
Edited text of question
0
 
LVL 2

Accepted Solution

by:
icampbe1 earned 50 total points
Comment Utility
There are two categories of your own messages.  Those that start at WM_User.  ie.   CONST MyMsg = WM_USER + 101.  and system unique messages you get from  MyMsg := RegisterWindowMessage('Some unique string' );

You can SendMessage( Handle, MyMsg, hparm, lparm ), Or PostMessage which places the message in the queue and returns immediately, or perform to send it your own window.

The techniques for receiving are various.  You can declare a local window message like PROCEDURE WMMyMessage( VAR MSG: TMEssage);  Message MyMsg;  and then implement it in the form object.
You can out a handler in the application.onmessage event, and several other more sophisticated techniques.  

These are all described in the Delphi Documentation rather well.  Take a look, if you still need help, let me know.

Cheers,
Ian C.

0
 

Author Comment

by:Lillan061397
Comment Utility
I use the procedure WMMyMessage( VAR MSG: TMEssage); Message MyMsg;, but that doesn't work!!! It's location is in the thread, but nothing happens.
0
 
LVL 2

Expert Comment

by:icampbe1
Comment Utility
Hi again,

Once you have implemented the MyMessage handler in the form (ie made it a message handling method and implemented it), you can then invoke it by sending a message if that type (number) to the window which contains the definition.  You specify the handle of the window that has the declaration and that expects to receive the message.

It you want to test the facility, simply 'perform' from the same form object and specity your Message ID then work from there.  If this doesn't work, you will have to show me some code and I'll try and determine what you missed.

Cheers,
Ian C.

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Lillan061397
Comment Utility
Here is the code that I'm using.

    const
      WM_TESTMESS = 10000 + WM_USER;  //Declarated in Constants.pas

     type
      TWMTestMess = record
        Msg: Cardinal;
      end;  // Declarated in Types.pas

     procedure TTestThread.WMTestMess(var Message: TWMTestMess);
     begin
        Form1.Caption:='Okej';
     end;   //Declarated in TestThread.pas

     PostThreadMessage(FTestT.ThreadID, WM_TESTMESS,0,0); //This is done in Main.pas, which is the mainprojekt. It uses Constants, Types and TestThread. FTestT is Created with TTestThread.Create(Form1); The reason I'm using Form1 is that I want to write in the mainform. The procedure behind Create is inherited Create(False);.
What am I doing wrong????  
0
 
LVL 2

Expert Comment

by:icampbe1
Comment Utility
You must declare the procedure as a message procedure:
 
TYPE xxx= CLASS( yyy)
PRIVATE
  PROCEDURE WMTestMsg( VAR Msg:TMEssage); Message; WM_TestMess;

Then, You must implement it.
Procedure xxx.WMTestMSg( VAR Msg.TMessage);
BEGIN
 ....
END;

DO NOT create a mesage type... Use TMessage as the type.  VAR Msg: TMessage.

Declare the procedure using the keyword 'Message' as show above.

I look later on for more comments.  I hope this helps.

Ian C.
0
 
LVL 2

Expert Comment

by:icampbe1
Comment Utility
Also, it isn't necessary to use such a large message number.  Use WM_TestMsg = WM_User + 500.  This will make it unique.
There are some controls that use WM_User +... but only for the first 50 or 60 as I recall.  WM_User +500 is very safe.

0
 

Author Comment

by:Lillan061397
Comment Utility
I've changed the messagetype and the message number. Thanks! The class yyy is TThread. Now I think that I'm doing everything right, but it still doesn't work. I've tried with
var Koll : Boll;
    Mess : TMsg;
Koll:= GetMessage(Mess,Null,WM_TESTMESS,WM_TESTMESS);
When I'm using this I get an EVariantError whith the code:
'Invalid variant type conversion'. I think that it is the TMsg type, but that is just my guess. Do I have to use GetMessage? Sorry for bothering you!

Thanks Lillan.
0
 
LVL 2

Expert Comment

by:icampbe1
Comment Utility
No, you do not use GetMessage.  Look at my previous example.
 The procedure  Procedure xxx.WMTestMSg( VAR Msg: TMessage)  wiill be called when you send the message.  It must be declared in the object as a message procedure (
PROCEDURE WMTestMsg( VAR Msg:TMEssage); Message; WM_TestMess; )  Also, the type  is 'TMessage' not 'TMsg'.

I have to leave right now,  send me a piece of your code to E-Mail:  iancamp@interlog.com  and I'll look at it this evening (I'm in Toronto).  It will be easier if I can see more specifically what you have.

Also, there is no need to appologize for asking questions.  That is what helping others is all about.  Sometime, I may be asking you for help.

Ian Campbell

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

11 Experts available now in Live!

Get 1:1 Help Now