473,664 Members | 2,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2509
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************* ********@g44g20 00...legro ups.com...
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\\QuickTime Streamer\\Quick TimeWrapper\\De bug\\QuickTimeW rapper.dll")]
private static extern int fnQuickTimeWrap per();

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\\QuickTime Streamer\\Quick TimeWrapper\\De bug\\QuickT
imeWrapper.dll" )]
private static extern int fnQuickTimeWrap per(); 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******** *************@o 13g2000cwo.goog legroups.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\\QuickTime Streamer\\Quick TimeWrapper\\De bug\\QuickTimeW rapper.dll")]
| private static extern int fnQuickTimeWrap per();
|
| 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******** *************@o 13g2000cwo.goog legroups.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\\QuickTime Streamer\\Quick TimeWrapper\\De bug\\QuickTimeW rapper.dll")]
| private static extern int fnQuickTimeWrap per();
|
| 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
2820
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 "enumerate" (in programming context) other than just sounding technical. i.e. is it more than just making a list of items? Are they the same thing? Please excuse my 'dah' factor, but it is really bugging me. Willam Roberts Joy to the world
31
2070
by: CYBER | last post by:
Hello Is there any other way under python to create blocks ?? instead of def sth(x): return x
2
1450
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, AppendChild, making width grow? Hi,
7
3122
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
4378
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 immutable instances? -- \ "Love is the triumph of imagination over intelligence." -- |
34
3689
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 thinking of making all these pimpl class declarations public. Reasoning is that since only the code within the ..cc file will need to ever access them, why protect them in ways that would make access to them more difficult and obfuscated? What...
351
12955
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 do) occur in any language. These assumptions are generally made because of familiarity with the language. As a non-code example, consider the idea that the faulty code is written by blackguards bent on foulling the language. The term...
10
3433
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 dynamic type of 'input' is Derived1, then 'output' should become a reference to 'input'. Otherwise 'output' should become a reference to the (temporary) result of the member function 'input.to_der1()' which returns a Derived1 object by value.
7
2302
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 WindowsXP SP2 What I have is a table in a subform that tracks dollars spent per project There is sub sub form that breaks down each dollar amount per fund. For example: sewer/water/streets/parks. In the sub sub form the fields I'm having...
50
4471
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.