Call C function from C# problem EntryPointNotFoundException

P

peetersb

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 EntryPointNotFoundException, 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("IHUAPI.dll")]
public static extern int ihuConnect(String 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_FAILED = 100,
ihuSTATUS_API_TIMEOUT = 101,
ihuSTATUS_NOT_CONNECTED = 102,
ihuSTATUS_INTERFACE_NOT_FOUND = 103,
ihuSTATUS_NOT_SUPPORTED = 104,
ihuSTATUS_DUPLICATE_DATA = 105,
ihuSTATUS_NOT_VALID_USER = 106,
ihuSTATUS_ACCESS_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_INVALID_TAGNAME = 113,
ihuSTATUS_LIC_TOO_MANY_TAGS = 114,
ihuSTATUS_LIC_TOO_MANY_USERS = 115,
ihuSTATUS_LIC_INVALID_LIC_DLL = 116,
ihuSTATUS_NO_VALUE = 117,
ihuSTATUS_NOT_LICENSED = 118,
ihuSTATUS_CALC_CIRC_REFERENCE = 119,
ihuSTATUS_DUPLICATE_INTERFACE = 120,
ihuSTATUS_BACKUP_EXCEEDED_SPACE = 121,
ihuSTATUS_INVALID_SERVER_VERSION = 122,
ihuSTATUS_DATA_RETRIEVAL_COUNT_EXCEEDED = 123,
ihuSTATUS_INVALID_PARAMETER = 124,
ihuSTATUS_MAX_ERROR_NUM = 124

} ihuErrorCode;


/* for connecting to one server */
ihuErrorCode IHUAPI ihuConnect
(
char * server,
char * username,
char * password,
long * serverhandle
);
 
B

Ben Voigt [C++ MVP]

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 EntryPointNotFoundException, 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("IHUAPI.dll")]
public static extern int ihuConnect(String server,String
username,String password,ref long serverhandle);

Well, first up, you forgot to specify the CallingConvention. It's pretty
clear from your header file you want CallingConvention.StdCall
 
P

peetersb

I tryed this:

[DllImport("IHUAPI.dll", CallingConvention =
CallingConvention.StdCall)]
public static extern ihuErrorCode ihuConnect(string server,
string username, string password, ref long serverhandle);

[DllImport("IHUAPI.dll", CallingConvention =
CallingConvention.StdCall)]
public static extern ihuErrorCode ihuDisconnect(ref long
serverhandle);

public Form1()
{
InitializeComponent();
}

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

//ihuConnect("dataserver", "", "", ref handle);
ihuDisconnect(ref handle);
//this.textBox1.Text = error.ToString();
}


But still gives me a EntryPointNotFoundException
 
J

Joachim Van den Bogaert

Hi there,

Try an IntPtr instead of an int and use the Marshal.PtrToStructure
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:

http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx

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

http://www.amazon.com/NET-2-0-Interoperability-Recipes-Problem-Solution/dp/1590596692
 
B

Ben Voigt [C++ MVP]

I tryed this:

[DllImport("IHUAPI.dll", CallingConvention =
CallingConvention.StdCall)]
public static extern ihuErrorCode ihuConnect(string server,
string username, string password, ref long serverhandle);

[DllImport("IHUAPI.dll", CallingConvention =
CallingConvention.StdCall)]
public static extern ihuErrorCode ihuDisconnect(ref long
serverhandle);

public Form1()
{
InitializeComponent();
}

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

//ihuConnect("dataserver", "", "", ref handle);
ihuDisconnect(ref handle);
//this.textBox1.Text = error.ToString();
}


But still gives me a EntryPointNotFoundException

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top