Call C function from C# problem EntryPointNotFoundException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • peetersb@gmail.com

    Call C function from C# problem EntryPointNotFoundException

    Hello,

    I'v a dll written in C/C++. I only have the header file.
    I dont know how to declare the external funtion:
    I've got a EntryPointNotFo undException, I think because of the wrong
    return type.
    The function in the header file return a enum. But I don't know how to
    declare it in my c# file.
    Can anybody help me?

    [DllImport("IHUA PI.dll")]
    public static extern int ihuConnect(Stri ng server,String
    username,String password,ref long serverhandle);

    This is the header file:


    #define IHUAPI __stdcall

    /*
    ** All possible errors returned by the API functions.
    ** These do not need to match system API
    */
    typedef enum {
    ihuSTATUS_OK = 0,
    ihuSTATUS_FAILE D = 100,
    ihuSTATUS_API_T IMEOUT = 101,
    ihuSTATUS_NOT_C ONNECTED = 102,
    ihuSTATUS_INTER FACE_NOT_FOUND = 103,
    ihuSTATUS_NOT_S UPPORTED = 104,
    ihuSTATUS_DUPLI CATE_DATA = 105,
    ihuSTATUS_NOT_V ALID_USER = 106,
    ihuSTATUS_ACCES S_DENIED = 107,
    ihuSTATUS_WRITE _IN_FUTURE = 108,
    ihuSTATUS_WRITE _ARCH_OFFLINE = 109,
    ihuSTATUS_ARCH_ READONLY = 110,
    ihuSTATUS_WRITE _OUTSIDE_ACTIVE = 111,
    ihuSTATUS_WRITE _NO_ARCH_AVAIL = 112,
    ihuSTATUS_INVAL ID_TAGNAME = 113,
    ihuSTATUS_LIC_T OO_MANY_TAGS = 114,
    ihuSTATUS_LIC_T OO_MANY_USERS = 115,
    ihuSTATUS_LIC_I NVALID_LIC_DLL = 116,
    ihuSTATUS_NO_VA LUE = 117,
    ihuSTATUS_NOT_L ICENSED = 118,
    ihuSTATUS_CALC_ CIRC_REFERENCE = 119,
    ihuSTATUS_DUPLI CATE_INTERFACE = 120,
    ihuSTATUS_BACKU P_EXCEEDED_SPAC E = 121,
    ihuSTATUS_INVAL ID_SERVER_VERSI ON = 122,
    ihuSTATUS_DATA_ RETRIEVAL_COUNT _EXCEEDED = 123,
    ihuSTATUS_INVAL ID_PARAMETER = 124,
    ihuSTATUS_MAX_E RROR_NUM = 124

    } ihuErrorCode;


    /* for connecting to one server */
    ihuErrorCode IHUAPI ihuConnect
    (
    char * server,
    char * username,
    char * password,
    long * serverhandle
    );
  • Ben Voigt [C++ MVP]

    #2
    Re: Call C function from C# problem EntryPointNotFo undException

    peetersb@gmail. com wrote:
    Hello,
    >
    I'v a dll written in C/C++. I only have the header file.
    I dont know how to declare the external funtion:
    I've got a EntryPointNotFo undException, I think because of the wrong
    return type.
    The function in the header file return a enum. But I don't know how to
    declare it in my c# file.
    Can anybody help me?
    >
    [DllImport("IHUA PI.dll")]
    public static extern int ihuConnect(Stri ng server,String
    username,String password,ref long serverhandle);
    Well, first up, you forgot to specify the CallingConventi on. It's pretty
    clear from your header file you want CallingConventi on.StdCall
    >
    This is the header file:
    >
    >
    #define IHUAPI __stdcall
    >
    /*
    ** All possible errors returned by the API functions.
    ** These do not need to match system API
    */
    typedef enum {
    ihuSTATUS_OK = 0,
    ihuSTATUS_FAILE D = 100,
    ihuSTATUS_API_T IMEOUT = 101,
    ihuSTATUS_NOT_C ONNECTED = 102,
    ihuSTATUS_INTER FACE_NOT_FOUND = 103,
    ihuSTATUS_NOT_S UPPORTED = 104,
    ihuSTATUS_DUPLI CATE_DATA = 105,
    ihuSTATUS_NOT_V ALID_USER = 106,
    ihuSTATUS_ACCES S_DENIED = 107,
    ihuSTATUS_WRITE _IN_FUTURE = 108,
    ihuSTATUS_WRITE _ARCH_OFFLINE = 109,
    ihuSTATUS_ARCH_ READONLY = 110,
    ihuSTATUS_WRITE _OUTSIDE_ACTIVE = 111,
    ihuSTATUS_WRITE _NO_ARCH_AVAIL = 112,
    ihuSTATUS_INVAL ID_TAGNAME = 113,
    ihuSTATUS_LIC_T OO_MANY_TAGS = 114,
    ihuSTATUS_LIC_T OO_MANY_USERS = 115,
    ihuSTATUS_LIC_I NVALID_LIC_DLL = 116,
    ihuSTATUS_NO_VA LUE = 117,
    ihuSTATUS_NOT_L ICENSED = 118,
    ihuSTATUS_CALC_ CIRC_REFERENCE = 119,
    ihuSTATUS_DUPLI CATE_INTERFACE = 120,
    ihuSTATUS_BACKU P_EXCEEDED_SPAC E = 121,
    ihuSTATUS_INVAL ID_SERVER_VERSI ON = 122,
    ihuSTATUS_DATA_ RETRIEVAL_COUNT _EXCEEDED = 123,
    ihuSTATUS_INVAL ID_PARAMETER = 124,
    ihuSTATUS_MAX_E RROR_NUM = 124
    >
    } ihuErrorCode;
    >
    >
    /* for connecting to one server */
    ihuErrorCode IHUAPI ihuConnect
    (
    char * server,
    char * username,
    char * password,
    long * serverhandle
    );

    Comment

    • peetersb@gmail.com

      #3
      Re: Call C function from C# problem EntryPointNotFo undException

      I tryed this:

      [DllImport("IHUA PI.dll", CallingConventi on =
      CallingConventi on.StdCall)]
      public static extern ihuErrorCode ihuConnect(stri ng server,
      string username, string password, ref long serverhandle);

      [DllImport("IHUA PI.dll", CallingConventi on =
      CallingConventi on.StdCall)]
      public static extern ihuErrorCode ihuDisconnect(r ef long
      serverhandle);

      public Form1()
      {
      InitializeCompo nent();
      }

      private void Form1_Load(obje ct sender, EventArgs e)
      {
      long handle = 0;

      //ihuConnect("dat aserver", "", "", ref handle);
      ihuDisconnect(r ef handle);
      //this.textBox1.T ext = error.ToString( );
      }


      But still gives me a EntryPointNotFo undException

      Comment

      • Joachim Van den Bogaert

        #4
        Re: Call C function from C# problem EntryPointNotFo undException

        Hi there,

        Try an IntPtr instead of an int and use the Marshal.PtrToSt ructure
        method.
        Note that you will have to describe the enum with the [StructLayout]
        attribute.
        Since you have ints in the original struct I don't think this is going
        to be a problem.

        Here's an article about it:



        And this is a very good book, it can help you with lots of interop
        scenarios:


        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: Call C function from C# problem EntryPointNotFo undException

          peetersb@gmail. com wrote:
          I tryed this:
          >
          [DllImport("IHUA PI.dll", CallingConventi on =
          CallingConventi on.StdCall)]
          public static extern ihuErrorCode ihuConnect(stri ng server,
          string username, string password, ref long serverhandle);
          >
          [DllImport("IHUA PI.dll", CallingConventi on =
          CallingConventi on.StdCall)]
          public static extern ihuErrorCode ihuDisconnect(r ef long
          serverhandle);
          >
          public Form1()
          {
          InitializeCompo nent();
          }
          >
          private void Form1_Load(obje ct sender, EventArgs e)
          {
          long handle = 0;
          >
          //ihuConnect("dat aserver", "", "", ref handle);
          ihuDisconnect(r ef handle);
          //this.textBox1.T ext = error.ToString( );
          }
          >
          >
          But still gives me a EntryPointNotFo undException
          Is the DLL export C++ name mangled? That will cause some grief.

          Use Dependency Walker (comes with the Windows SDK, updates at
          www.dependencywalker.com) to view the export table of the DLL and post the
          exact name of the export here.

          Also, C++ long is 32 bits, so change your serverhandle parameter to ref
          Int32 instead of ref long.


          Comment

          Working...