calling from c# into a dll

G

Guest

I have a dll that consists of unmanaged C++ routines. I can call a function
in the dll from a C++ executable, but not from a C# executable. I am calling
an independent function, not a class. C# gives me a runtime exception saying
the entry point to the dll cannot be found.

I declare the function in C# with the following lines:
[DllImport("simpleDLL.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int fnsimpleDLL(int i);

I then call the function with the line:
fnsimpleDLL(1);

Any ideas?
 
W

Willy Denoyette [MVP]

Make sure the exported C function is not mangled (has C linkage), make also
sure the calling convention is cdecl (CallingConvention.Cdecl)]).

Willy.
 
G

Guest

Thanks for the info. I remember the mangling issue from when I had to call C
code from C++, but the C# docs are silent on the issue.

What do I do when I want to export a C++ class in a dll to a C# program?

Al


Willy Denoyette said:
Make sure the exported C function is not mangled (has C linkage), make also
sure the calling convention is cdecl (CallingConvention.Cdecl)]).

Willy.

Al the programmer said:
I have a dll that consists of unmanaged C++ routines. I can call a
function
in the dll from a C++ executable, but not from a C# executable. I am
calling
an independent function, not a class. C# gives me a runtime exception
saying
the entry point to the dll cannot be found.

I declare the function in C# with the following lines:
[DllImport("simpleDLL.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int fnsimpleDLL(int i);

I then call the function with the line:
fnsimpleDLL(1);

Any ideas?
 
G

Guest

Thanks for the info. The docs don't talk about importing a class, only
functions, but I know where to start looking in the future. For my current
project I need to access C functions, which I can do.

Al
 
W

Willy Denoyette [MVP]

Al the programmer said:
Thanks for the info. The docs don't talk about importing a class, only
functions, but I know where to start looking in the future. For my
current
project I need to access C functions, which I can do.

Al

It doesn't talk about importing a class because it isn't possible to access
unmanaged classes cross-language.
If you need this, your only option is to create a wrapper class using
managed C++ (or better the upcoming C++/CLI)., this wrapper simply delegates
the accesses from managed code into unmanaged C++.

Willy.
 

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