外部应用程序使用sharedmemory接收消息但不包含数据

时间:2012-05-23 16:17:12

标签: c# c++ interop shared-memory memory-mapped-files

我在c#app中使用sharememory和c ++ interop。目前,我正在将结构编组为指针并广播消息。我正在广播的程序使用调试消息正确打开,但是没有显示/引入我在结构中使用的数据。

谢谢!

我试图与之交谈的应用程序是用c ++编写的,我用c#编写代码。我正在使用所有DLLImports(我认为)并且它编译并运行无错误。

using System.Runtime.InteropServices;

[DllImport("user32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern uint RegisterWindowMessageW([In]string lpString);

[DllImport("user32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern uint RegisterWindowMessageA([In]string lpString);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern IntPtr OpenFileMapping(FileMapAccessRights dwDesiredAccess, int bInheritHandle, [In]String lpName);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, FileMapAccessRights dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall)]
public static extern int UnmapViewOfFile(IntPtr lpBaseAddress);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall)]
public static extern int CloseHandle(IntPtr hObject);

[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

uint WM_ZOOM_XYZ = RegisterWindowMessageA("WM_ZOOM_XYZ");

int i = Broadcast_Zoom_Message(10000, 10000, 0, WM_ZOOM_XYZ);

public int Broadcast_Zoom_Message(double dbX, double dbY, double dbZ, uint uMessage) 
{
    string smSharedMemory = "COORDINATES";

    IntPtr hMem = OpenFileMapping(FileMapAccessRights.Write, FALSE, smSharedMemory);

    if (IntPtr.Zero == hMem)
    {
        return 0;
    }

    IntPtr pvHead = MapViewOfFile(hMem, FileMapAccessRights.Write, 0, 0, UIntPtr.Zero);


    if (IntPtr.Zero == pvHead)
    {
        CloseHandle(hMem);
        MessageBox.Show(
            "Unable to view " + smSharedMemory, 
            "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        return 0;
    }

    CoordinatesStruct structCoords = new CoordinatesStruct();


    Marshal.PtrToStructure(pvHead, structCoords);

    int bVersionOk = FALSE;

    if (1 == structCoords.uMajorVersion)
    {
        if (WM_ZOOM_XYZ == uMessage)
        {
            structCoords.dbDesiredX = dbX;
            structCoords.dbDesiredY = dbY;
            structCoords.dbDesiredZ = dbZ;
        }
        bVersionOk = TRUE;
    }
    else
    {
        MessageBox.Show(
            "Unrecognized shared memory: " +
            structCoords.uMajorVersion.ToString() + "." + structCoords.uMinorVersion.ToString());
    }
    if (IntPtr.Zero != hMem)
    {
        CloseHandle(hMem);
    }
    UnmapViewOfFile(pvHead);

    IntPtr HWND_BROADCAST = (IntPtr)0xffff;

    if (bVersionOk == TRUE)
    {
        PostMessage(HWND_BROADCAST, uMessage, 0, 0);
        return 1;
    }
    else
        return 0;
}

1 个答案:

答案 0 :(得分:1)

我认为您的目的是将更改后的structCoords放回映射文件中。当我们使用Marshal.PtrToStructure()时,我们会收到非托管内存的内容副本。接收对象的更改不会反映在非托管内存中。完成数据后,我们应该使用Marshal.StructureToPtr将更改恢复到内存中。

我认为这应该是:

if (1 == structCoords.uMajorVersion)
{
    if (WM_ZOOM_XYZ == uMessage)
    {
        structCoords.dbDesiredX = dbX;
        structCoords.dbDesiredY = dbY;
        structCoords.dbDesiredZ = dbZ;
    }
    bVersionOk = TRUE;
    Marshal.StructureToPtr(structCoords , pvHead, false); // <-- this is what you (I) forgot!
}