I based my code on the ipctest.c and .h files included with the SDK.
The problem is that the only result I'm getting from the SendMessage call is the value 0. I've also determined that the size of the query struct returned by Marshal.SizeOf() is 2 bytes smaller than that returned by sizeof() in c.
Here's the code I tried, along with the .NET struct definitions.
Any idea what I'm doing wrong?
Any help is greatly appreciated!
Code: Select all
public bool SendSearch(string searchString)
{
Process everythingProcess = Process.GetProcessesByName("Everything").FirstOrDefault();
if(everythingProcess == null)
{
return false;
}
int length = searchString.Length;
int ipcQuerySize = Marshal.SizeOf(typeof(IpcQuery)) - Marshal.SizeOf(typeof(Char)) + length * Marshal.SizeOf(typeof(Char)) + Marshal.SizeOf(typeof(Char));
Console.Out.WriteLine("Size: {0}", ipcQuerySize);
IntPtr replyHandle = Process.GetCurrentProcess().MainWindowHandle;
IpcQuery ipcQuery =
new IpcQuery
{
MaxResults = int.MaxValue,// 0xFFFFFFFF,
Offset = 0,
ReplyCopyDataMessage = new IntPtr(0),
SearchFlags = 0,
ReplyHandle = replyHandle,
SearchString = searchString,
};
IntPtr ipcQueryPointer = Marshal.AllocHGlobal(ipcQuerySize);
Marshal.StructureToPtr(ipcQuery, ipcQueryPointer, false);
CopyDataStruct copyDataStruct =
new CopyDataStruct
{
dwData = COPY_DATA_UNICODE,
cbData = ipcQuerySize,
lpData = ipcQueryPointer,
};
int cdsSize = Marshal.SizeOf(typeof(CopyDataStruct));
IntPtr cdsPointer = Marshal.AllocHGlobal(cdsSize);
Marshal.StructureToPtr(copyDataStruct, cdsPointer, false);
IntPtr targetHandle = everythingProcess.MainWindowHandle;
IntPtr reply = NativeMethods.SendMessage(targetHandle, replyHandle, cdsPointer);
return reply.ToInt32() == 1;
}
Code: Select all
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct IpcQuery
{
public IntPtr ReplyHandle;
public IntPtr ReplyCopyDataMessage;
public int SearchFlags;
public int Offset;
public int MaxResults;
[MarshalAs(UnmanagedType.LPWStr)]
public string SearchString;
}
Code: Select all
[StructLayout(LayoutKind.Sequential)]
public struct CopyDataStruct
{
public int dwData;
public int cbData;
public IntPtr lpData;
}
Code: Select all
private const UInt32 WM_COPYDATA = 0x004A;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public static IntPtr SendMessage(IntPtr targetHandle, IntPtr replyHandle, IntPtr copyDataStruct)
{
return SendMessage(targetHandle, WM_COPYDATA, replyHandle, copyDataStruct);
}