I am trying to use pInvoke, but on both the emulator and on the device the invoke fails. I am new to .NET (Am a C++ developer) and I don't understand how the JIT/framework can't find that DLL/method/etc.
Is there something else I have to do to get it to work?
In looking at similar questions it appears that I may or may not have to add the DLL to either the solution or the CAB - but where do I get that file.
Surely the OS on the device has user32.dll? And the Windows 7 version cannot possibly be the correct one to install on the device, can it?
EDIT
Any one of these fails:
开发者_JAVA技巧[DllImport("coredll.dll", EntryPoint = "FindWindowW", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("coredll.dll", EntryPoint = "SipShowIM")]
public static extern bool SipShowIMP(int code);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
Surely the OS on the device has user32.dll?
No, unfortunately it doesn't. Windows Mobile doesn't include user32.dll
, as well as many other normal Windows API DLLs. Instead, you typically need to P/Invoke into coredll.dll
instead. For signatures, see PInvoke.net's section (at the bottom left) for "Smart Device Functions".
Edit:
Some of the signatures on there are obviously incorrect, as you mention in the comments. You can look at the Windows Mobile API for the functions (such as SetWindowPos) to get the correct signature.
I believe, for yours, most should be in coredll.dll
:
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("coredll.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("coredll.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("coredll.dll", EntryPoint = "SipShowIM")]
public static extern bool SipShowIMP(int code);
[DllImport("coredll.dll")]
public static extern IntPtr GetForegroundWindow();
精彩评论