locked
How to call an API function by ordinal? RRS feed

  • Question

  • Hi all,

    I'm working on a project that uses some DLL that export nameless functions, in which exported only by their ordinal numbers; How can I make a declaration for that ordinal function in C# and call it?

    Also, if I have an API function retrieved by address using GetProcAddress, is it possible to call a function in C# directly by its address? Is so, how?

    Thanks in advance,


    -chris
    Thursday, September 1, 2005 1:48 AM

Answers

  • Chris,

    The DllImport attribute has an EntryPoint member that can be used to refer to a method by its ordinal value:

      // The # character is required.
      [DllImport("dllname", EntryPoint="#123")]

    If possible, don't use GetProcAddress.  The best way in C# to declare a native method is via DllImport.  If you need to load the DLL dynamically at runtime when the app starts, you can still use DllImport attributes along with LoadLibrary using this technique:

      http://www.dotnetinterop.com/faq/?q=LoadLibrary

    If you must use GetProcAddress in .Net 1.0/1.1, here's an article on how to use the function pointer returned by GetProcAddress:

      http://www.codeproject.com/csharp/dyninvok.asp

    In .Net 2.0 I believe there is a way to get a delegate from a function pointer, and then call the function thru the delegate:

      System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer
      http://msdn2.microsoft.com/library/zdx6dyyh(en-us,vs.80).aspx
    Thursday, September 1, 2005 3:31 AM

All replies

  • Chris,

    The DllImport attribute has an EntryPoint member that can be used to refer to a method by its ordinal value:

      // The # character is required.
      [DllImport("dllname", EntryPoint="#123")]

    If possible, don't use GetProcAddress.  The best way in C# to declare a native method is via DllImport.  If you need to load the DLL dynamically at runtime when the app starts, you can still use DllImport attributes along with LoadLibrary using this technique:

      http://www.dotnetinterop.com/faq/?q=LoadLibrary

    If you must use GetProcAddress in .Net 1.0/1.1, here's an article on how to use the function pointer returned by GetProcAddress:

      http://www.codeproject.com/csharp/dyninvok.asp

    In .Net 2.0 I believe there is a way to get a delegate from a function pointer, and then call the function thru the delegate:

      System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer
      http://msdn2.microsoft.com/library/zdx6dyyh(en-us,vs.80).aspx
    Thursday, September 1, 2005 3:31 AM
  • Thanks a lot Chris, I'll give this a try.

    Thanks again,

    -chris
    Thursday, September 1, 2005 3:35 AM