Making C++ DLL to use in C#

N

Nuno Magalhaes

Can I code all I want in a C++ DLL (say sockets or files) and then
import and use in C# using P/Invoke?
Are there any good samples on how to code a DLL (using .lib and .h
files) and then use in my C# application?
I checked www.pinvoke.net but I can't find any DLL source file written
in C++ that can be used in C#.
 
J

José Joye

My 2 cents:

Personally, I prefer to go with the managed C++ as a wrapper between the non
managed and the managed world. I find it easier if your interface is not
trivial.
Using the managed C++, you can interface the standard C/C++ the normal way.
From the C# side, you see your managed C++ dll as a normal .Net assembly.

José
 
N

Nuno Magalhaes

But I want to try from unmanaged C++ as a win32 application dll. I
create a simple DLL with the exports and the functions made by the
template should be well exported.
The problem is in C#:
[DllImport("C:\\Work\\QuickTimeStreamer\\QuickTimeWrapper\\Debug\\QuickTimeWrapper.dll")]
private static extern int fnQuickTimeWrapper();

It says that the entry point function was not found in the dll. Am I
missing something?
 
I

irnbru irnbru

I met your problem before, it is because c# doesn't recognise the name
of your exported function, go to dos and type:

dumpbin /exports yourdll.dll

you should see the name of your exported function. What happens is
visual c++ "decorated" the name of your fonction for example if your
function name is GetWindowTitle VC++ will export under the mangled name
: ?GetWindowTitle@@blahblah so C# is clueless...

msdn says :

The Microsoft C++ compilers encode the names of symbols in C++ programs
to include type information in the name. This is called "name
decoration", or "name mangling". The purpose of this is to ensure
type-safe linking. The C++ language allows function overloading where
functions with the same name are only distinguished from one another by
the data types of the arguments to the functions. Name decoration
enables the linker to distinguish between different versions of
overloaded functions because the names of the functions are encoded or
decorated differently.

ONE POSSIBLE SOLUTION:

All you have to do is to create and add a file : yourdll.def

you type in it :

; Yourdll.def - defines the exports for yourdll.dll

LIBRARY yourdll
DESCRIPTION 'A C++ dll that can be called from c#'

EXPORTS
YourFunction


Your recompile your dll project to create the updated dll

try again

dumpbin /exports yourdll.dll

You will see your function with the proper name ;)

What you did with the .def file is : you told C# that the name
associated with
[DllImport("C:\\Work\\QuickTimeStreamer\\QuickTimeWrapper\\Debug\\QuickT
imeWrapper.dll")]
private static extern int fnQuickTimeWrapper(); is YourFunction and not
the VC++ decorated name :)

Hope I could help you :) I am sure you will make it!
IRNBRU
 
W

Willy Denoyette [MVP]

| But I want to try from unmanaged C++ as a win32 application dll. I
| create a simple DLL with the exports and the functions made by the
| template should be well exported.
| The problem is in C#:
|
[DllImport("C:\\Work\\QuickTimeStreamer\\QuickTimeWrapper\\Debug\\QuickTimeWrapper.dll")]
| private static extern int fnQuickTimeWrapper();
|
| It says that the entry point function was not found in the dll. Am I
| missing something?
|

Make sure your functions are exported with C style bindings?

extern "C" __declspec( dllexport ) void SomeFunction(....);

Willy.
 
N

Nuno Magalhaes

It works now. But I didn't use the VS .NET 2003 template for DLL
because dependency walker wouldn't show the exported function. Maybe
declaring as you said would appear.
I can now have the functions exported and used in my C# application,
even the functions from other libraries like QuickTime.
 

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