CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2006
    Location
    Mumbai, India
    Posts
    292

    passing pointers between the process

    Hi,
    I need to pass the character array or char* to the another process through BroadcastSystemMessage() API. If I use WPARAM for this, I get the junk address in the recieving process.I am doing something like this
    Sender
    Code:
    ..
    char szTemp[32]={0};
    strcpy(szTemp,"TEST");
    ULONG uiMsg = RegisterWindowMessage(KP_EXPORT_STATUS_MSG);
    		DWORD dwRecipients		= BSM_APPLICATIONS;
    		DWORD dwFlags			= BSF_FORCEIFHUNG | BSF_POSTMESSAGE;
    		
    BroadcastSystemMessage(	BSF_FORCEIFHUNG|BSF_POSTMESSAGE,
    						&dwRecepients,
    						iServerFileFailure,
    					(WPARAM)temp,
    						0);
    ...
    Reciever
    Code:
    ULONG uiMsg = RegisterWindowMessage(KP_EXPORT_STATUS_MSG);
    		DWORD dwRecipients
    LRESULT CALLBACK WndMainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	if(	message ==	uiMsg )
    	{
    		char* szTemp = (char*)wParam;
                                    //szTemp is junk here
    	}
    Later I figured out that, pointers are meaningless between two processes as they will be having different process space. But it works if i send an interger as WPARAM instead of pointer.
    I need to pass a string to another process. Is there any workaround for this problem??

    Thanks

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: passing pointers between the process

    Like you figured out yourself, you cannot send a pointer since it is only meaningful in 1 process.
    Since it seems you are writing both applications, try to use WM_COPYDATA.
    Make sure you read the remarks in that article!
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: passing pointers between the process

    There are several forms of Interprocess Communication on Windows Platforms.

    Another option is a Win32 dll with a share data segment. The dll provides a pair of GetSet methods and you put a string buffer into the shared seg area of the dll. You would have to load the dll in both processes.

    One of the options mentioned in the link above is a memory mapped file. I use this technique in one of my articles in my subject line (Win32.. Part II). There is a Log Send and Log Receive sample that uses a MMF to send a structure containing a string buffer between two processes.

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

    Re: passing pointers between the process

    As Arjay wrote there are many ways to send data between processes, and as Marc G pointed out using WM_COPYDATA is one of the simplest ones providing that both processes are on the same machine/desktop. But there could be also some problems with WM_COPYDATA if someone else (not your sender process) broadcasts it! See this essay:Using WM_COPYDATA
    Victor Nijegorodov

  5. #5
    Join Date
    May 2006
    Location
    Mumbai, India
    Posts
    292

    Re: passing pointers between the process

    Quote Originally Posted by Marc G
    Like you figured out yourself, you cannot send a pointer since it is only meaningful in 1 process.
    Since it seems you are writing both applications, try to use WM_COPYDATA.
    Make sure you read the remarks in that article!
    Thanks a lot for the reply..
    I gone through the link regarding WM_COPYDATA, But I dont have the handle for the window. That is why I am broadcasting the custom message. So I am afraid I cant use WM_COPYDATA.

    However I am not getting why this works in the case of integers.

    Thanks

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: passing pointers between the process

    Because in the case of integers you send the real integer and not some pointer.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    May 2006
    Location
    Mumbai, India
    Posts
    292

    Re: passing pointers between the process

    Thanks Arjay and VictorN for reply..

    Hello Mark G, I see that WPARAM is defined as UINT_PTR, so its a pointer. Doesnt it means that we are sending pointer and not the integer.

    I am little confused abt it.

    Thanks anyway.

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: passing pointers between the process

    No, UINT_PTR is defined as follows:
    Code:
    #if defined(_WIN64)
     typedef unsigned __int64 UINT_PTR;
    #else
     typedef unsigned int UINT_PTR;
    #endif
    So, it's a 32 bit uint when building 32 bit version and it's 64 bit when building 64 bit version. It is called _PTR because it could be used to store pointers since it will have the correct size.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  9. #9
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: passing pointers between the process

    Quote Originally Posted by sachin871
    But I dont have the handle for the window.
    But, is it possible for you to get a handle to the window , say using FindWindow/Ex ?

    If it is a string of known length, check to see if this post about atom tables is a viable option. http://www.codeguru.com/forum/showpo...7&postcount=14

  10. #10
    Join Date
    May 2006
    Location
    Mumbai, India
    Posts
    292

    Re: passing pointers between the process

    Thanks a lot for all ur replies..
    Meanwhile I came across this tutorial on MSDN which is regarding the use of shared memory. I think its pretty easy and I can use that in future as well.

    Here is the link


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