外部应用程序使用共享内存接收消息,但没有包含的数据

本文关键字:包含 数据 消息 应用程序 共享 内存 外部 | 更新日期: 2023-09-27 18:35:03

我正在我的 c# 应用程序中使用 sharedmemory 和 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;
}

外部应用程序使用共享内存接收消息,但没有包含的数据

我认为您的意图是将更改后的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!
}