C# 外部应用程序使用sharedmemory接收消息,但不包含任何数据 我在C++应用程序中用C++互操作使用SysDead内存。目前,我正在将结构编组到指针并广播消息。我正在广播的程序会正确打开并显示调试消息,但不会显示/引入我在结构中使用的数据
谢谢 <>我正在尝试用的应用程序是用C++编写的,我用C语言编码。我正确地使用了所有的DLLImports(我认为),它编译和运行时没有错误C# 外部应用程序使用sharedmemory接收消息,但不包含任何数据 我在C++应用程序中用C++互操作使用SysDead内存。目前,我正在将结构编组到指针并广播消息。我正在广播的程序会正确打开并显示调试消息,但不会显示/引入我在结构中使用的数据,c#,c++,interop,shared-memory,memory-mapped-files,C#,C++,Interop,Shared Memory,Memory Mapped Files,谢谢 我正在尝试用的应用程序是用C++编写的,我用C语言编码。我正确地使用了所有的DLLImports(我认为),它编译和运行时没有错误 using System.Runtime.InteropServices; [DllImport("user32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] public static extern uint RegisterWindowMessag
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!
}
if(1==structCoords.uMajorVersion)
{
if(WM_ZOOM_XYZ==uMessage)
{
structCoords.dbDesiredX=dbX;
structCoords.dbDesiredY=dbY;
structCoords.dbDesiredZ=dbZ;
}
bVersionOk=真;
Marshal.StructureToPtr(structCoords,pvHead,false);//我认为您的意图是将更改的structCoords
放回映射文件
我们收到非托管内存内容的副本。收到对象的更改不会反映在非托管内存中。处理完数据后,我们应该使用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!
}
if(1==structCoords.uMajorVersion)
{
if(WM_ZOOM_XYZ==uMessage)
{
structCoords.dbDesiredX=dbX;
structCoords.dbDesiredY=dbY;
structCoords.dbDesiredZ=dbZ;
}
bVersionOk=真;
Marshal.StructureToPtr(structCoords,pvHead,false);//迫不及待地想明天试一试!会让你知道它是如何运行的。Thx,KPBINGO!今天早上工作得很好。得到来自世界各地的程序员的帮助真是太棒了!谢谢!迫不及待地想明天试一试!会让你知道它是如何运行的。Thx,KPBINGO!今天早上工作得很好。得到专业人士的帮助真是太棒了来自世界各地的格拉默!谢谢你