views:

581

answers:

1

I am having problem with the contents of a pointer passed as the wParam from a 32-bit C# application is changing along the way to a 64-bit C++ process.

There are two processes 32.exe (in C#) and 64.exe (in C++). 64.exe is started as a child process of 32.exe. 32.exe post window message for 64.exe, one of which has a wParam that is a pointer to an array of RECT structures. Both 64.exe and 32.exe have a common DLL (written in C++, but compiled for different platforms, of course), called 32.dll and 64.dll.

A function expecting a RECT* in 32.dll is called directly from 32.exe with the same RECT* that is later posted, and this works well. Afterwards it posts a message to 64.exe, which calls the same function and casts the wParam to RECT*:

else if (WM_SetDisabledAreas == message)
{
 SetDisabledAreas((RECT*)wParam, (UINT)lParam);
}

The message is posted as follows:

    if (Is64Bit() && SubProcess64 != null)
    {
        Win32.PostMessage(SubProcess64.MainWindowHandle, WindowMessages.SetDisabledAreas,
            (uint)pointer.ToInt32(), length);
    }
    MessageBox.Show(pointer.ToString());
    DLL32.SetDisabledAreas(pointer, length);

By debugging I've validated that the message is received, however the wParam address is not the same as it was before. This is not unexpected, but the contents of the memory it now points to is undefined (and I get an access violation when attempting to see what is there).

What is happening here?

+5  A: 

Each of the two processes has an own address space, so that the pointer from process 32.exe is not valid in 64.exe.

However, this has nothing to do with 32bit vs 64bit at all. You just have to use an interprocess communication technique of your choice to transfer the data between the two processes.

For example, you could use CreateFileMapping to create a named section of shared memory.

Timbo
I know that they can have different address spaces, but isn't Windows supposed to translate this for you when you pass pointers along?
Vegard Larsen
How should windows know that the (uint)pointer.ToInt32() is a pointer to an array of rectangles? And how should it know the length of this array when call it?
Timbo
Windows does the translation for you for messages that it knows about. For custom messages, it has no idea what the data is so it just passes the values and doesn't do anything to them.
Andy