希望有人能帮助我:
void irsend_qtask(t_irsend *x)
{
LRESULT copyDataResult;
HWND pOtherWnd = FindWindow(NULL, "WinLirc");
if (pOtherWnd)
{
COPYDATASTRUCT cpd;
cpd.dwData = 0;
cpd.cbData = strlen(x->s_string) + 1;
cpd.lpData = (void*)x->s_string;
copyDataResult = SendMessage(pOtherWnd, WM_COPYDATA, (WPARAM)(HINSTANCE)GetModuleHandle(NULL), (LPARAM)&cpd);
//post("irsend: I send: %s", x->s_string);
outlet_bang(x->p_outlet); //Send a Bang
}
else
{
post("irsend: Error! WinLIRC not running?"); //Error
}
}
另一个程序应该只有一个数据结构。
我现在有一个正常工作的代码:
[StructLayout(LayoutKind.Sequential)]
struct COPYDATASTRUCT
{
public IntPtr dwData; // Any value the sender chooses. Perhaps its main window handle?
public int cbData; // The count of bytes in the message.
public IntPtr lpData; // The address of the message.
}
const int WM_COPYDATA = 0x004A;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct MYSTRUCT
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 127)]
public string mystring;
}
MYSTRUCT mystruct;
// Allocate a pointer to an arbitrary structure on the global heap.
public static IntPtr IntPtrAlloc<T>(T param)
{
IntPtr retval = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, retval, false);
return retval;
}
// Free a pointer to an arbitrary structure from the global heap.
public static void IntPtrFree(ref IntPtr preAllocated)
{
if (IntPtr.Zero == preAllocated)
throw (new NullReferenceException("Go Home"));
Marshal.FreeHGlobal(preAllocated);
preAllocated = IntPtr.Zero;
}
...
private void irsend()
{
mystruct.mystring = "LED-Controller-#1 RED1 0";
IntPtr hWnd = (IntPtr)FindWindow(null,"WinLirc");
if (hWnd!=IntPtr.Zero)
{
IntPtr buffer = IntPtrAlloc(mystruct);
COPYDATASTRUCT cpd = new COPYDATASTRUCT();
cpd.dwData = IntPtr.Zero;
cpd.cbData = (Marshal.SizeOf(mystruct)+1);
cpd.lpData = buffer;
IntPtr copyDataBuff = IntPtrAlloc(cpd);
SendMessage(hWnd, WM_COPYDATA, GetModuleHandle(null), copyDataBuff);
IntPtrFree(ref copyDataBuff);
IntPtrFree(ref buffer);
}
}
我只是不了解一些事情,例如&#34;非托管结构&#34; 我应该在这里写点什么:&#34;权力无意识,心灵无能为力!&#34;