473,378 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Making C++ DLL to use in C#

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#.

Feb 11 '06 #1
6 2499
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é
"Nuno Magalhaes" <nu************@hotmail.com> a écrit dans le message de
news: 11*********************@g44g2000cwa.googlegroups.c om...
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#.

Feb 11 '06 #2
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\\QuickTimeW rapper\\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?

Feb 11 '06 #3


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\\QuickTimeW rapper\\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

*** Sent via Developersdex http://www.developersdex.com ***
Feb 12 '06 #4

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
| 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\\QuickTimeW rapper\\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.

Feb 12 '06 #5
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.

Willy Denoyette [MVP] wrote:
"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
| 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\\QuickTimeW rapper\\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.


Feb 12 '06 #6
This may help others with a similar problem:

http://www.geocities.com/Jeff_Louie/...op/pinvoke.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Feb 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Willam Roberts | last post by:
To me, when you enumerate, you make a list of specific items, like enumerating controls in a form. Am I missing something? I mean is there something more to the meaning of the term "enumeration" or...
31
by: CYBER | last post by:
Hello Is there any other way under python to create blocks ?? instead of def sth(x): return x
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
7
by: redneon | last post by:
Does anyone have any good links to information on how it's possible to make a library in C++? I can't seem to find anything.
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
7
by: MarkNeumann | last post by:
I'm coming from a Corel paradox background and moving into an Access environment. So I'm struggling with something that I think is probably way simpler than I'm making it out to be. Access 2007...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.