473,698 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EXPERT help URGENTLY NEEDED on InterOp (Managed C++?) issue

Hi,

I have a C++ DLL that I want to use from a C# project. I am actually
usng a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::string, std::vector and std::map quite extensively in my C++ DLL API.

However, I can simplify the API so as to make it easier to use from C#.
Below, is a very simple "proof of concept" C++ DLL. I would be extremely
grateful if someone could show me how I can use this Dll from C#:

Here is the code:

/* C++ code (Header)
(trivial proof of concept DLL */

#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dlle xport)
#else
#define CCONV __declspec(dlli mport)
#endif

typedef enum {
ONE ,
TWO ,
THREE
} myEnum;

typedef struct mystruct_ {
int x ;
float y ;
char z[8] ;
} myStruct ;

typedef int (*INT_FPTR)(con st char*, const MyStruct*) ;

class CCONV MyClass {
public:
MyClass();
MyClass(const MyClass&);
MyClass& operator = (const MyClass&);
~MyClass();

private:
//some private variables here ..
};

class CCONV DClass : public MyClass {
public :
DClass();
DClass(const DClass&);
DClass& operator = (const DClass&);
~DClass();

const char* foo(void);
int barney(int, myEnum, const myStruct*); //throws an exception
void register_callba ck( INT_FPTR ) ;

private:
INT_FPTR cb ;
};
Many Thanks

Nov 17 '05 #1
10 1436
ET I am no expert, but VS2005 managed C++ supports templates and the STL
while emitting MSIL.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2

"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
Hi,

I have a C++ DLL that I want to use from a C# project. I am actually usng
a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::string, std::vector and std::map quite extensively in my C++ DLL API.

However, I can simplify the API so as to make it easier to use from C#.
Below, is a very simple "proof of concept" C++ DLL. I would be extremely
grateful if someone could show me how I can use this Dll from C#:

Here is the code:

/* C++ code (Header)
(trivial proof of concept DLL */

#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dlle xport)
#else
#define CCONV __declspec(dlli mport)
#endif

typedef enum {
ONE ,
TWO ,
THREE
} myEnum;

typedef struct mystruct_ {
int x ;
float y ;
char z[8] ;
} myStruct ;

typedef int (*INT_FPTR)(con st char*, const MyStruct*) ;

class CCONV MyClass {
public:
MyClass();
MyClass(const MyClass&);
MyClass& operator = (const MyClass&);
~MyClass();

private:
//some private variables here ..
};

class CCONV DClass : public MyClass {
public :
DClass();
DClass(const DClass&);
DClass& operator = (const DClass&);
~DClass();

const char* foo(void);
int barney(int, myEnum, const myStruct*); //throws an exception
void register_callba ck( INT_FPTR ) ;

private:
INT_FPTR cb ;
};
Many Thanks


What do you mean by throws an exception?, "barney" is a C++ member function,
you can't create instances of native C++ classes from C#, so you can't call
member functions.
Your only options are:
1. create a managed C++ class in a managed assembly (using vs2005 C++/CLI)
that wrap your unmanaged C++ class(es).
2. convert your public interfaces (exposed to C#) to managed C++ classes and
compile with C++/CLI (vs2005).

Willy.

Nov 17 '05 #3


Willy Denoyette [MVP] wrote:
"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
Hi,

I have a C++ DLL that I want to use from a C# project. I am actually usng
a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::string , std::vector and std::map quite extensively in my C++ DLL API.

However, I can simplify the API so as to make it easier to use from C#.
Below, is a very simple "proof of concept" C++ DLL. I would be extremely
grateful if someone could show me how I can use this Dll from C#:

Here is the code:

/* C++ code (Header)
(trivial proof of concept DLL */

#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dlle xport)
#else
#define CCONV __declspec(dlli mport)
#endif

typedef enum {
ONE ,
TWO ,
THREE
} myEnum;

typedef struct mystruct_ {
int x ;
float y ;
char z[8] ;
} myStruct ;

typedef int (*INT_FPTR)(con st char*, const MyStruct*) ;

class CCONV MyClass {
public:
MyClass();
MyClass(con st MyClass&);
MyClass& operator = (const MyClass&);
~MyClass();

private:
//some private variables here ..
};

class CCONV DClass : public MyClass {
public :
DClass();
DClass(cons t DClass&);
DClass& operator = (const DClass&);
~DClass();

const char* foo(void);
int barney(int, myEnum, const myStruct*); //throws an exception
void register_callba ck( INT_FPTR ) ;

private:
INT_FPTR cb ;
};
Many Thanks
What do you mean by throws an exception?, "barney" is a C++ member function,


What is so difficult to understand by this statement?. C++ methods *can*
throw exceptions - maybe managed C++/C# (or should that be C dumb)
classes can't ?
you can't create instances of native C++ classes from C#, so you can't call
member functions.
Your only options are:
1. create a managed C++ class in a managed assembly (using vs2005 C++/CLI)
that wrap your unmanaged C++ class(es).


<snip>
I already knew that. Everybody (including the MS site tells me I need to
write managed C++ wrapper classes around my C++ classes - but there is
no useful examples given anywhere - most of the examples are to do with
calling C functions from C#. So far its been all talk and no examples -
has anyone *actually* EVER called C++ classes from C#? If it can't be
done or it has never been tried out in the real world - just be man
enough to say it instad of issuing the blanket line : "write managed C++
classes around your C++ classes". No one as yet (and that included MS
itself) afaik, has actually provided an example of using C++ classes
exported from a C++ DLL - so come on : can it be done or NOT ?. If yes
(everyone tells me it can - but has no examples) - HOW the hell is it
done - does anyone actually even know?. I provided a simple no brainer
C++ DLL and expected at least some pseudocode to show how I can call the
2/3 functions from C# - apparently, no one really knows how to do this ....

Nov 17 '05 #4
chill man, we r all here to help others!
while ya cudnt create instance of c++ class in c#, its a bloody truth.
HOWEVER, this NOT so hard to get around.

//remove those lines out cuz ya dont need to export the whole
//class out from a c++ class
#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dlle xport)
#else
#define CCONV __declspec(dlli mport)
#endif

NO CHANGES for ya current classes, they r workin just fine
//add the following
extern "C" __declspec(dlle xport) DClass* CreateInstance( );
extern "C" __declspec(dlle xport) DClass* DestroyInstance (DClass* obj);

//create the methods to export
extern "C" __declspec(dlle xport) DClass* CreateInstance( )
{ return new DClass(); }

extern "C" __declspec(dlle xport) DClass* DestroyInstance (DClass* obj)
{ delete obj; }

/////////////////////////////////////
After ya hav done ALL above, im pretty sure ya kno how to deal with object
instances afterwards, dont ya?

////////////////////////////////////
I just roughly put some sample above, it may not workin extactly as ya wish
but ya hav got the idea.

hope this helps

Nov 17 '05 #5
1 more thing, mate
the thing im talkin about is PURELY c/c++ by itself.
im NOT sure that vs2005 introduces 'something' to 'managed c++' or not. ya
better check it out first.
BTW: to be honest, i HATE managed C++.
Nov 17 '05 #6


Ashura wrote:
chill man, we r all here to help others!
while ya cudnt create instance of c++ class in c#, its a bloody truth.
HOWEVER, this NOT so hard to get around.

//remove those lines out cuz ya dont need to export the whole
//class out from a c++ class
#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dlle xport)
#else
#define CCONV __declspec(dlli mport)
#endif

NO CHANGES for ya current classes, they r workin just fine
//add the following
extern "C" __declspec(dlle xport) DClass* CreateInstance( );
extern "C" __declspec(dlle xport) DClass* DestroyInstance (DClass* obj);

//create the methods to export
extern "C" __declspec(dlle xport) DClass* CreateInstance( )
{ return new DClass(); }

extern "C" __declspec(dlle xport) DClass* DestroyInstance (DClass* obj)
{ delete obj; }

/////////////////////////////////////
After ya hav done ALL above, im pretty sure ya kno how to deal with object
instances afterwards, dont ya?

////////////////////////////////////
I just roughly put some sample above, it may not workin extactly as ya wish
but ya hav got the idea.

hope this helps


Ok, now we are at least getting somewhere. However, your code (assuming
that this is the ONLY way forward) proves *precisely* what I had
suspected all along:

In order to use C++ classes in C#, you have to write a C API
(i.e.wrapper) around your C++ classes and *then* use the C API from
managed C++ - lol and lmao !!!, excuse me for my mirth - but one has to
admire MS for being such a dominat force in the industry despite the
fact that it has so many half-assed, half baked "technologi es" which are
so - erm f*@ked up and proprietary. C# appears to be nothing more than a
blatant plagiarism of Java (not to mention a few bits and pieces
"borrowed" from C and C++) and just enough proprietary crap to ensure
that it is not compatable with any of the "true" standard languages out
there.

This sucks BIG TIME !

PS: Thanks anyway Ashura - at least you tried. But this half cooked
language (if you can call it that) does not seem to have any saving graces.

Nov 17 '05 #7
"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
<snip>
dickhead ranting about how C# sucks, when he OBVIOUSLY hasn't given it a chance
</snip>
Dude,

If you want help, you might ATTEMPT to be civil!!
Obviously you wanted to hate C# from the start.

There are quite a few resources available for learning about Managed C++ wrapper classes. All you
need to do is LOOK for them!!

- There are a bunch of books available on Amazon(or your favorite bookseller)
- A simple GOOGLE query brought up a bunch of usable examples
- A search of GOOGLE Groups Brought up a bunch of good info

I just found them myself while writing this post.

Chill out dude
Nov 17 '05 #8

"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk******** **@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...


Willy Denoyette [MVP] wrote:
"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
Hi,

I have a C++ DLL that I want to use from a C# project. I am actually usng
a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::strin g, std::vector and std::map quite extensively in my C++ DLL
API.

However, I can simplify the API so as to make it easier to use from C#.
Below, is a very simple "proof of concept" C++ DLL. I would be extremely
grateful if someone could show me how I can use this Dll from C#:

Here is the code:

/* C++ code (Header)
(trivial proof of concept DLL */

#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dlle xport)
#else
#define CCONV __declspec(dlli mport)
#endif

typedef enum {
ONE ,
TWO ,
THREE
} myEnum;

typedef struct mystruct_ {
int x ;
float y ;
char z[8] ;
} myStruct ;

typedef int (*INT_FPTR)(con st char*, const MyStruct*) ;

class CCONV MyClass {
public:
MyClass();
MyClass(cons t MyClass&);
MyClass& operator = (const MyClass&);
~MyClass() ;

private:
//some private variables here ..
};

class CCONV DClass : public MyClass {
public :
DClass();
DClass(con st DClass&);
DClass& operator = (const DClass&);
~DClass();

const char* foo(void);
int barney(int, myEnum, const myStruct*); //throws an exception
void register_callba ck( INT_FPTR ) ;

private:
INT_FPTR cb ;
};
Many Thanks
What do you mean by throws an exception?, "barney" is a C++ member
function,


What is so difficult to understand by this statement?. C++ methods *can*
throw exceptions - maybe managed C++/C# (or should that be C dumb) classes
can't ?
Difficult to understand is, what exception could possibly be thrown by
something that you are not able to call from C#.

you can't create instances of native C++ classes from C#, so you can't
call member functions.
Your only options are:
1. create a managed C++ class in a managed assembly (using vs2005
C++/CLI) that wrap your unmanaged C++ class(es).


<snip>
I already knew that. Everybody (including the MS site tells me I need to
write managed C++ wrapper classes around my C++ classes - but there is no
useful examples given anywhere - most of the examples are to do with
calling C functions from C#. So far its been all talk and no examples -
has anyone *actually* EVER called C++ classes from C#?

That's exactly what we are telling here, YOU CAN'T directly call native C++
member functions from C#.

If it can't be done or it has never been tried out in the real world - just be man enough
to say it instad of issuing the blanket line : "write managed C++ classes
around your C++ classes".
What is so difficult to understand by this statement?. Write a wrapper
class using C++/CLI, MSDN contains samples how to do it.
No one as yet (and that included MS itself) afaik, has actually provided an example of using C++ classes
exported from a C++ DLL - so come on : can it be done or NOT ?. If yes
(everyone tells me it can - but has no examples) - HOW the hell is it
done - does anyone actually even know?. I provided a simple no brainer C++
DLL and expected at least some pseudocode to show how I can call the 2/3
functions from C# - apparently, no one really knows how to do this ....

Again search the MSDN docs, it contains sample and a lot more you need to
know.

Willy.
Nov 17 '05 #9

"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...


Ashura wrote:
chill man, we r all here to help others!
while ya cudnt create instance of c++ class in c#, its a bloody truth.
HOWEVER, this NOT so hard to get around.

//remove those lines out cuz ya dont need to export the whole
//class out from a c++ class
#ifdef TESTDLL_EXPORTS #define CCONV __declspec(dlle xport) #else #define
CCONV __declspec(dlli mport) #endif NO CHANGES for ya current classes,
they r workin just fine
//add the following
extern "C" __declspec(dlle xport) DClass* CreateInstance( );
extern "C" __declspec(dlle xport) DClass* DestroyInstance (DClass* obj);

//create the methods to export
extern "C" __declspec(dlle xport) DClass* CreateInstance( )
{ return new DClass(); }

extern "C" __declspec(dlle xport) DClass* DestroyInstance (DClass* obj)
{ delete obj; }

/////////////////////////////////////
After ya hav done ALL above, im pretty sure ya kno how to deal with
object instances afterwards, dont ya?

////////////////////////////////////
I just roughly put some sample above, it may not workin extactly as ya
wish but ya hav got the idea.

hope this helps
Ok, now we are at least getting somewhere. However, your code (assuming
that this is the ONLY way forward) proves *precisely* what I had suspected
all along:

In order to use C++ classes in C#, you have to write a C API (i.e.wrapper)
around your C++ classes and *then* use the C API from managed C++ - lol
and lmao !!!, excuse me for my mirth - but one has to admire MS for being
such a dominat force in the industry despite the fact that it has so many
half-assed, half baked "technologi es" which are so - erm f*@ked up and
proprietary. C# appears to be nothing more than a blatant plagiarism of
Java (not to mention a few bits and pieces "borrowed" from C and C++) and
just enough proprietary crap to ensure that it is not compatable with any
of the "true" standard languages out there.

WRONG - you need to write a (managed) wrapper class using C++/CLI, what is
shown in this sample is an alternative way.
This sucks BIG TIME !

PS: Thanks anyway Ashura - at least you tried. But this half cooked
language (if you can call it that) does not seem to have any saving
graces.

Willy.
Nov 17 '05 #10

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

Similar topics

1
2156
by: Matt Dinovo | last post by:
I have a remoting server that is on the same local box as a COM+ managed object with Library Activation. When I try to activate the object from the remoted assembly, I get the following error: "The specified activation could not occur in the client context as specified." The security context of the service application that is activating the object has the proper credentials. It's been years since I've had to deal with COM+ and I'm a bit...
1
1995
by: Nadav | last post by:
Hi, Introduction *************** I have a system build of a collection of 'Native COM objects' and '.NET COM interop' objects, all of the COM objects are managed through a 'Native COM' layer, this layer manage the underlying COM Objects and upon request, provide a pointer to those objects to the 'API Consumer', following is an illustration of the system: API Consumer ( Native C++/C# ) || ******************************************* * ...
26
4470
by: Lasse Edsvik | last post by:
Hello I'm trying to build a simple COM+ app in vs.net using C# and i cant register it in component manager..... what more is needed than this: using System; using System.EnterpriseServices;
2
1987
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen to the output of. Specify a filename to shove all of the data (into Excel), and start the whole thing going. I get that done no problem. The problem comes with the Data. The data is coming from a different application, and I am not 100% sure of...
2
1333
by: Jas Manghera | last post by:
Hello everyone, Im having a rather strange problem with my custom ASP.NET application. After deploying the application on a .NET v1.1 computer, the first time the application is launched it can call an external interop and excute commands with no problems. However, when the same system is restarted and my application is relauched the COM commands do not function (asp & managed code still works fine). When I alter the permissions of any...
2
1628
by: Bob S | last post by:
I am having trouble with the following scenario.... Assembly A Returns a managed implementation of COM IStream interface to the unmanaged world ============= /////////////////
3
1335
by: Amit_Basnak | last post by:
Dear friends I am in the process of creating the new server binary for Systinet Server for C++. I did make all and I am getting C++ error which Im posting below $ make all Error 419: "../interop/java/impl.h", line 14 # 'Barn_BarnServiceImpl' is used as a type, but has not been defined as a type. class BarnServerImpl : public Barn_BarnServiceImpl { ^^^^^^^^^^^^^^^^^^^^
5
1897
by: Yoavo | last post by:
Hi, In my application I have to use som external COM dll's. In the code: using MyDll when I build the project, I get (in addition to the .exe file) a Dll called Interop.MyDll.dll I want to send the .exe to the users without needing to send the dll. Is it possible to build my project without the accompanying dll ?
5
8439
by: muriwai | last post by:
Hi, I have a C# assembly project under Visual Stuio 2008 Pro on Windows Server 2008. I converted the project from VS 2005. The project references COM->Microsoft CDO for Windows 2000 Library 1.0, which adds two entries under References called CDO and ADODB. When I compile the solution, I get the following: Error 1 Assembly 'Interop.CDO, Version=1.0.0.0, Culture=neutral,
0
8674
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
8603
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
9157
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...
0
8861
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
6518
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
4369
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...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.