Getting a message with wanted lparam from queue

This is a discussion on Getting a message with wanted lparam from queue within the Windows Programming forums, part of the Platform Specific Boards category; Hello! I'm using a custom WM_MYCUSTOM message to send chars received by a thread to my Dialog using PostMessage(). Each ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    Getting a message with wanted lparam from queue

    Hello! I'm using a custom WM_MYCUSTOM message to send chars received by a thread to my Dialog using PostMessage(). Each char is sent as parameter of that message.
    Now, during elaboration of that message I need to know how many times the same char value was received.

    So, I'm trying to "walk through" the message queue to count the WM_MYCUSTOM messages that have a lparam wanted, removing those messages.
    Code:
    UINT MyDlg::privGetNumCharsInQueue(CHAR c)
    {
    	MSG msg;
    	UINT count = 0;
    	UINT n = 0;
    
    	while( PeekMessage( &msg, this->m_hWnd, WM_CUSTOMMESSAGE_RECEIVED_CHAR, WM_CUSTOMMESSAGE_RECEIVED_CHAR, PM_NOREMOVE ))
    	{
    		if( msg.lParam == c )
    		{
    			count++;
    			PeekMessage( &msg, this->m_hWnd, WM_CUSTOMMESSAGE_RECEIVED_CHAR, WM_CUSTOMMESSAGE_RECEIVED_CHAR, PM_REMOVE );
    		}
    	}
    	return count;
    }
    The problem is that it seems not possible to use PeekMessage() because it returns the first wanted message in the queue, always. If this message is WM_MYCUSTOM but has a wrong lparam, it must remain in the quque and peekmessage will return it again in the next call....

    I think I should use different WM_ IDs for each char I need to find....but I'd like to know if there's a better way.

    Thank you all.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Perhaps elaborate a little on what you're trying to do? I don't find any easy solution out of this.
    But then again, you could simply push them into an array or such and then send a message to your main window to process that array (and your thread should not touch that array while the main thread is processing that array).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Ok, let's say something like this:
    Code:
    UINT MyDlg::privGetNumCharsInQueue(CHAR c)
    {
    	MSG msg;
    	UINT count = 0;
    	UINT k = 0, i = 0;
    
    #define MAX_MESSAGES	10
    	MSG aMsg[MAX_MESSAGES];
    
    	while( PeekMessage( &msg, this->m_hWnd, WM_CUSTOMMESSAGE_RECEIVED_CHAR, WM_CUSTOMMESSAGE_RECEIVED_CHAR, PM_REMOVE ) && k < MAX_MESSAGES)
    	{
    		if( msg.lParam == c )
    		{
    			count++;
    		}
    		else
    		{
    			memcpy( &aMsg[k++] , &msg, sizeof( msg ));
    		}	
    	}
    
    	for( ; i < k; i++ )
    		this->PostMessage( aMsg[i].message , aMsg[i].wParam , aMsg[i].lParam );
    	return count;
    I'm going to try...thank you very much, Elysia

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    I don't think you got the point.
    Use an array in your class (std::vector), where you can put your chars in.
    With sane times, you can call PostMessage or SendMessage to send your message and allow your GUI to process that array and all the characters.
    Be sure to block your thread until your dialog is complete or use synchronization functions such as critical sections or mutexes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Interfering in the msg queue is a bad idea IMO.


    I would suggest one of the other interprocess communication methods.

    I think I would use a mailslot, MSMQ appears more complex than this requires. Works/codes very much like a file, shared by both apps.
    Last edited by novacain; 12-22-2007 at 11:44 PM. Reason: Need to read the code better before posting
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I agree with novacain that messing with the message queue is not a good idea.

    But also consider WM_COPYDATA which IMHO is proably the simplist IPC mechanism to implement and the easiest to understand.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I think the OP should also consider the limits of GetQueueStatus()

    Quote Originally Posted by MSDN
    The presence of a QS_ flag in the return value does not guarantee that a subsequent call to the GetMessage or PeekMessage function will return a message.
    GetMessage and PeekMessage perform some internal filtering that may cause the message to be processed internally.
    For this reason, the return value from GetQueueStatus should be considered only a hint as to whether GetMessage or PeekMessage should be called.
    This is one reason that searching the msg queue may not be the best method.



    http://msdn2.microsoft.com/en-us/lib...40(VS.85).aspx
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 03:46 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 08:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21