0
internal class OpenMainForm
{
    public const int HWND_BROADCAST = 0xffff;
    public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
    [DllImport("user32")]
    public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);//here
    [DllImport("user32")]
    public static extern int RegisterWindowMessage([MarshalAs(UnmanagedType.BStr)] string message);//here
}

Warnings: Since the method is a P/Invoke method, 'Open Main Form.PostMessage (IntPtr, int, IntPtr, IntPtr)' must be defined in a class named NativeMethods, SafeNativeMethods, Or UnsafeNativeMethods.

matt
  • 10,892
  • 3
  • 22
  • 34
  • 1
    Why is this tagged `C`? – Adrian Mole Jan 02 '20 at 11:06
  • It is a code analysis warning, not a compile error. Microsoft programmers must pay attention to it, the code analyzer is based on a version that was used in-house while .NET was being developed. Named "FxCop". Whether you pay attention as well is up to you, as an end-user there is no point to it. – Hans Passant Jan 02 '20 at 11:10
  • https://stackoverflow.com/q/13590447/2067492 – matt Jan 02 '20 at 11:13
  • What part of the warning do you find incomprehensible? With `[DllImport]` you're "P/Invoking", and it clearly states what it wants you to do (put them in a class with a certain name). I can understand if you _disagree_ with the warning, though. – C.Evenhuis Jan 02 '20 at 11:15
  • You can show me the solutions, I would not ask a question if I knew the solution ... – Сергей Маерович Jan 02 '20 at 11:34
  • See the native functions declaration [here](https://stackoverflow.com/a/48812831/7444103) (last section). You can wrap all declaration in a single class, named `NativeMethods`. Use only `public` method wrappers/helpers to invoke the native functions (don't make the Win32 functions `public`, only `internal` or `private`: expose only the wrappers/helpers methods). – Jimi Jan 02 '20 at 13:18

0 Answers0