none
PInvoke - SendMessage Problem

    Question

  •  

    OK, so I'm using PInvoke - doing the following:


    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);



    But I'm receiving the following error:

    Error 2 The modifier 'extern' is not valid for this item
    Error 3 Expected class, delegate, enum, interface, or struct


    Why ???
    Tuesday, September 18, 2007 8:51 AM

Answers

  • Hi Programm3r,

     

    You have to declare SendMessage method in a class.

    It should look like.

     

    Code Snippet

    public static class WinAPI

    {

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

    public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    }

     

    Regards,

    Oleh.

    Tuesday, September 18, 2007 9:57 AM

All replies

  • Forgot a "using System.Runtime.InteropServices"?

    Tuesday, September 18, 2007 9:57 AM
  • Hi Programm3r,

     

    You have to declare SendMessage method in a class.

    It should look like.

     

    Code Snippet

    public static class WinAPI

    {

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

    public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    }

     

    Regards,

    Oleh.

    Tuesday, September 18, 2007 9:57 AM
  •  Programm3r wrote:

     

    OK, so I'm using PInvoke - doing the following:


    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);



    But I'm receiving the following error:

    Error 2 The modifier 'extern' is not valid for this item
    Error 3 Expected class, delegate, enum, interface, or struct



    Why ???

     

    Hi Programm3r,

     

    As far as I know, you can try to declare a SendMessage API in C# as follows:

     

    Code Snippet

     using System.Runtime.InteropServices;

     

     [DllImport("user32.dll")]
     private static extern int SendMessage (int hWnd, int wMsg,int wParam, ref int lParam);

     

    Try to check out this KB article for details - http://support.microsoft.com/kb/q318601/

     

    Hope this helps,

     

    Regards,

    Citizens on the earth

    Thursday, September 20, 2007 3:15 AM