CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jan 2015
    Posts
    100

    Post How to register WM_USER in winproc API

    Hi,

    I am trying to register a WM_USER message in windowsclass so that i can use the message in winproc for performing some hardware operation,so how i can register WM_USER in winMain for windowClass,I am following the below steps for manipulating the operations,

    1) Registering the window class
    2) Creating the window handle
    3)Registering the hardware window event message
    4)Windows Procedure where user function are passed registering interrupt for switch

    so can you please tell me how to register WM_USER + some signal in windows for winClass which is user defined.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include "matrix_dio.h"
    
    #define HARDWARE_EXTERNAL_SIGNAL	1
    
    void poweroff();
    I16 initializeEmbeddedPC();
    I16 closeEmbeddedPC();
    
    const char g_szClassName[] = "ShutdownInterrupt";
    
    /*****************************************************
     * This API is used for system shutdown
     ******************************************************/
    void poweroff()
    {
    	system("C:\\WINDOWS\\System32\\shutdown -s");
    	//return NULL;
    }
    
    /*****************************************************
     * This API is used for initializing the embedded pc
     ******************************************************/
    
    I16 initializeEmbeddedPC()
    {
    	I16  nStatus;
    	nStatus = ADMX_DIO_Init();
    	if(nStatus == NoError)
    	{
    		return NoError;
    	}
    	else if(nStatus  == ErrorOpenDriverFailed)
    	{
    		return ErrorOpenDriverFailed ;
    	}
    	else
    	{
    		return ErrorDeviceIoctl ;
    	}
    }
    
    /*****************************************************
     * This API is used for closing the embedded pc
     ******************************************************/
    
    I16 closeEmbeddedPC()
    {
    	I16 ErrNum;
    	ErrNum = ADMX_DIO_Close() ;
    
    	if (ErrNum == NoError)
    	{
    		return NoError;
    	}
    	return NoError;
    }
    
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_USER + HARDWARE_EXTERNAL_SIGNAL:
    	printf("Interrupt for hardware events\n");
    	poweroff();
    	break;
    
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		ADMX_DIO_INT1_EventMessage(INT1_DISABLE,hwnd,WM_USER + HARDWARE_EXTERNAL_SIGNAL,0);
    		closeEmbeddedPC();
    		break;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    
    	default:
    		return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    		LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG Msg;
    	U16 Value;
    	I16 ErrNum1;
    
    	//Step 1: Initializing the embedded PC
    	initializeEmbeddedPC();
    
    	Value = 0;
    
    	//Step 2: Registering the Window Class
    	wc.cbSize        = sizeof(WNDCLASSEX);
    	wc.style         = 0;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = hInstance;
    	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    				MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	// Step 3: Creating the Window
    	hwnd = CreateWindowEx(
    			WS_EX_CLIENTEDGE,
    			g_szClassName,
    			"Shutdown Hardware Interrupt",
    			WS_OVERLAPPEDWINDOW,
    			CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
    			NULL, NULL, hInstance, NULL);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    				MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	//Registering the hardware event message in the message loop
    	while(1)
    	{
    		ADMX_DO_ReadLine (0,&Value);
    		if(Value == 1)
    		{
    			ErrNum1 = ADMX_DIO_INT1_EventMessage (INT1_EXT_SIGNAL, hwnd, WM_USER + HARDWARE_EXTERNAL_SIGNAL,0);
    			Msg.message =  WM_USER + HARDWARE_EXTERNAL_SIGNAL;
    			break;
    			if (ErrNum1 == NoError)
    			{
    				return NoError;
    			}
    
    		}
    
    	}
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	// Step 3: The Message Loop
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to register WM_USER in winproc API

    I'm not sure I'm totally following you. You don't 'register' messages you just define them to be used.
    Code:
    //define the required hardware message numbers
    #define HWM1   1
    #define HWM2   2
    #define HWM3   3
    
    //define the related messages
    #define WM_HWM1 (WM_USER + HWM1)
    #define WM_HWM2 (WM_USER + HWM2)
    #define WM_HWM3 (WM_USER + HWM3)
    ...
    switch(msg)
    	{
    	case WM_HWM1:
    	    break;
    
            case WM_HWM2:
                break;
    
            case WM_HWM3:
                break;
    
    ....
    You then use SendMessage() or PostMessage() as needed with the required message id - or use other functions (eg your ADMX_DIO_INT1_EventMessage() function) passing the required message ID.

    Code:
    ErrNum1 = ADMX_DIO_INT1_EventMessage (INT1_EXT_SIGNAL, hwnd, WM_HMW1, 0);
    Msg.message =  WM_HMW1;
    as appropriate.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2015
    Posts
    100

    Re: How to register WM_USER in winproc API

    As you told i will be using ADMX_DIO_INT1_EventMessage() function for generating required message ID so still we require GetMessageA()

  4. #4
    Join Date
    Jan 2015
    Posts
    100

    Re: How to register WM_USER in winproc API

    Can you please anyone explain me when winProc api will be called in winmain so before using the GetMessage or in windowsRegisterclass.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to register WM_USER in winproc API

    Code:
    //Registering the hardware event message in the message loop
    	while(1)
    	{
    		ADMX_DO_ReadLine (0,&Value);
    		if(Value == 1)
    		{
    			ErrNum1 = ADMX_DIO_INT1_EventMessage (INT1_EXT_SIGNAL, hwnd, WM_USER + HARDWARE_EXTERNAL_SIGNAL,0);
    			Msg.message =  WM_USER + HARDWARE_EXTERNAL_SIGNAL;
    			break;
    			if (ErrNum1 == NoError)
    			{
    				return NoError;
    			}
    
    		}
    
    	}
    This code makes no sense as
    Code:
    if (ErrNum1 == NoError)
    			{
    				return NoError;
    			}
    will never be executed as it follows a break.

    What does ADMX_DIO_INT1_EventMessage() function do?

    What happens if the value read from ADMX_DO_ReadLine (0,&Value) is never 1?

    so still we require GetMessageA()
    Yes, you still require the message pump loop.

    ...and I'm still having some difficulty actually understanding your issue because in Windows you don't register messages - you just use them?

    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx has info about the available message numbers that can be used.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jan 2015
    Posts
    100

    Re: How to register WM_USER in winproc API

    what i an trying to do there means i am getting Hardware button signal input whenever its high i am trying to generate interrupt for window user defined message,so in thAT windowproc api i aM trying to register event interrupt the below is the syntax of that .

    DIO_INT1_EventMessage
    Controls the INT1 interrupt sources for a dual-interrupt system
    and notifies the application when an interrupt event occurs.
    Notification is implemented through a user-specified callback
    function or the Windows PostMessage API.
    Syntax
    C/C++ and Borland C++
    I16 DIO_INT1_EventMessage (I16 Int1Mode, HANDLE windowHandle, U32 message, void *callbackAddr());

    Parameter(s)
    Int1Mode
    Interrupt mode of INT1.
    Valid values include: MXC-6300, INT1_DISABLE,
    INT1_EXT_SIGNAL INT1 by COS of Ch0 of Port 0
    windowHandle
    Handle to the destination window for a Windows message
    when the specified INT event occurs. If windowHandle is 0, no
    Windows messages will be sent.
    message
    User-defined message. When the specified INT event occurs,
    MXC-6300 matrix_dio remits this message. The message can
    be of any value. In Windows, the message can be set to a
    value including any Windows predefined messages, such as
    WM_PAINT. However, to define a designated message, any
    value ranging from WM_USER (0x400) to 0x7fff can be used.
    This range is reserved by Windows for user-defined messages.
    callbackAddr
    Address of the user callback function. The MXC-6300
    matrix_dio calls this function when the specified INT event
    occurs. If no callback function is desired, set callbackAddr to 0.

    Return codes
    NoError
    ErrorInvalidCardNumber
    ErrorCardNotRegistered
    ErrorFuncNotSupport


    so whatever i am getting the interrupt i am trying copy to the windows message id and i am trying to handle the interrupt in winProc

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to register WM_USER in winproc API

    Fine. What I explained in my post #2 is valid.

    Code:
    //define the required hardware message numbers
    #define HWM1   1
    
    //define the related messages
    #define WM_HWM1 (WM_USER + HWM1)
    ...
    ErrNum1 = ADMX_DIO_INT1_EventMessage (INT1_EXT_SIGNAL, hwnd, WM_HMW1, 0);
    ...
    switch(msg)
    	{
    	case WM_HWM1:
    	    printf("Interrupt for hardware events\n");
    	    poweroff();
    	    break;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to register WM_USER in winproc API

    Quote Originally Posted by 2kaud View Post
    I'm not sure I'm totally following you. You don't 'register' messages you just define them to be used.
    Well... you don't register WM_USER messages,
    but you can register (system unique) message numbers for communicating between 2 processes (typically in a broadcast, since point to point you can just as well stick to WM_USER).

    see RegisterWindowMessage()

  9. #9
    Join Date
    Jan 2015
    Posts
    100

    Re: How to register WM_USER in winproc API

    Can i know winAPI supports for AMDX_ReadLine and AMDX_WriteLine drivers support because when i use in winMain the API is not supporting for AMDX_ReadLine and AMDX_WriteLine.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to register WM_USER in winproc API

    Quote Originally Posted by OReubens View Post
    for communicating between 2 processes
    ... point to point you can just as well stick to WM_USER.
    Did you mean WM_APP?
    WM_USER range is defined and used by an application to send messages within a private window class. According ro MSDN
    ... Messages in this range should not be sent to other applications unless the applications have been designed to exchange messages and to attach the same meaning to the message numbers.
    Victor Nijegorodov

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to register WM_USER in winproc API

    Quote Originally Posted by VictorN View Post
    Did you mean WM_APP?
    WM_USER range is defined and used by an application to send messages within a private window class. According ro MSDN
    No I did mean WM_USER (+something of course). But maybe I should have added that it assumes you created both apps, and the one app know perfectly what the WM_USER+(something) will do in the other app. This should have been fairly obvious following the RegisterWindowMessage, since that too can only happen if both apps have agreed on the "name" to register the message under in the first place.

    that's exactly what MSDN alludes to as well with the "unless applications have been designed..."

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to register WM_USER in winproc API

    Well, what I agree with is using IDs generated by RegisterWindowMessage would be (is) the best choice for interprocess messaging.
    Victor Nijegorodov

  13. #13
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to register WM_USER in winproc API

    Well that's my point, if you are sending or posting a message to a specific window of another app you created, then whether you use a WM_USER or a registered message doesn't make much difference.

    But if you use a broadcast type send/post (like BroadcastSystemMessage()), that's an entirely different thing ofc, you can't guarantee that some random app won't have a internal meaning for WM_USER+something as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured