locked
Using static extern keyword with DLLIMPORT RRS feed

  • Question

  • Greetings!

    I would like to know why do we need to specify 'static extern' as keywords when using DllImport in C# for managed-unmanaged code interface.

    For example, to import an unmanaged DLL function 'myFunc', in C# we declare:

    [DllImport "myDll.dll"]
    public static extern void myFunc(......)

    I understand the need for 'extern' but why should it be 'static extern'?

    Thanks
    Aditya
    Tuesday, October 28, 2008 7:30 PM

Answers

  • It's static because none of them need an object instance to operate on.  All the information these methods need is passed in via it's parameters. Therefore, the method is, by it's very definition, static, so it should be marked as such.  .NET simplifies this by requiring it when declaring these methods.
    David Morton - http://blog.davemorton.net/
    Tuesday, October 28, 2008 7:59 PM
    Moderator

All replies

  • It's static because none of them need an object instance to operate on.  All the information these methods need is passed in via it's parameters. Therefore, the method is, by it's very definition, static, so it should be marked as such.  .NET simplifies this by requiring it when declaring these methods.
    David Morton - http://blog.davemorton.net/
    Tuesday, October 28, 2008 7:59 PM
    Moderator
  • Sorry for a late reply, but thank you.
    Monday, November 17, 2008 8:51 PM