How do I fix “System.MissingMethodException = {”Can't find PInvoke DLL 'user32.dll'."} on Windows 6.5?

StackOverflow https://stackoverflow.com/questions/7353067

  •  28-10-2019
  •  | 
  •  

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:

[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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top