1

我在我的 c# 应用程序中使用共享内存和 c++ 互操作。目前我正在将一个结构编组为一个指针并广播消息。我正在广播的程序使用调试消息正确打开,但不显示/引入我在结构中使用的数据。

谢谢!

我试图与之交谈的应用程序是用 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;
}
4

1 回答 1

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!
}
于 2012-05-24T04:14:56.803 回答