How to use a C++ class in .NET

H

Herbert Saal

Hello,

I have a c++ class in a dll. like this one:

class __declspec(dllexport) MyClass
{
private:
public:
MyClass(void);

int funtion1(unsigned char * inBuffer, unsigned int inType, unsigned char
*outBuffer, int& out_Size);
int function2(void *inBuffer_a, void *inBuffer_b, unsigned long int
*outResult);
};

Note that the function parameters are prefixed with "out" and "in" depending
of the parameter.

I tried to use the class with the DLLImport functionality but it doesn't
works, i'm not sure if i have to use this with classes.
I'm working with c#.

Please help me.

Regards,
Herbert
 
L

Lloyd Dupont

PInvoking C++ is not very common.
For one thing you could ONLY PInvoke C++ dell compiled with Microsoft
compiler.
It's not MS fault at all, it's just due to C++ name mangling which is
absolutely compiler dependant.
It's the same issue that a C++ DLL is only usable with the same compiler.

Anyway you've got 3 solutions:

- it's a C++ dll compiled with CL (doesn't looks like it though, CL favor
def file over dllexport), you could theoritically call them directlry using
CallingConvention.ThisCall, however it's not very much documented, and I'm
not quite sure how it works pratically

- make a C wrapper and use standart interop

- make a Managed C++ wrapper. Managed C++ v2 beta 2 is adviced, much cleaner
than v1 and has been standardized with ECMA (unlike Managed C++ v1)
to make a Managed C++ wrapper you don't need to do much changes.
maybe all you have to do is rewrite your class like that:

public ref class MyClass
{
public:
int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char*
outbuf, int% out_size);
}
or much cleaner (because the above def would be seen as 'byte*" in C#)
public ref class MyClass
{
private:
int funtion1(unsigned char * inBuffer, unsigned int inType, unsigned char
*outBuffer, int& out_Size);
public:
int funtion1(array<unsigned char>^ inBuffer, unsigned inType,
array<unsigned char>^ outbuf, int% out_size)
{
pin_ptr<unsigned char> p_inbuf = &inBuffer[0];
pin_ptr<unsigned char> p_out = &outbuf[0];
pin_ptr<unsigned char> p_out_size = &out_size;
funtion((unsigned char*)p_inbuf, inType, (unsigned char)p_out,
*p_out_size);
}
}

I start to forget my managed C++ already so you better check it out on MS
web site first...
 
H

Herbert Saal

Thank u very much Lloyd!

Lloyd Dupont said:
PInvoking C++ is not very common.
For one thing you could ONLY PInvoke C++ dell compiled with Microsoft
compiler.
It's not MS fault at all, it's just due to C++ name mangling which is
absolutely compiler dependant.
It's the same issue that a C++ DLL is only usable with the same compiler.

Anyway you've got 3 solutions:

- it's a C++ dll compiled with CL (doesn't looks like it though, CL favor
def file over dllexport), you could theoritically call them directlry
using CallingConvention.ThisCall, however it's not very much documented,
and I'm not quite sure how it works pratically

- make a C wrapper and use standart interop

- make a Managed C++ wrapper. Managed C++ v2 beta 2 is adviced, much
cleaner than v1 and has been standardized with ECMA (unlike Managed C++
v1)
to make a Managed C++ wrapper you don't need to do much changes.
maybe all you have to do is rewrite your class like that:

public ref class MyClass
{
public:
int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char*
outbuf, int% out_size);
}
or much cleaner (because the above def would be seen as 'byte*" in C#)
public ref class MyClass
{
private:
int funtion1(unsigned char * inBuffer, unsigned int inType, unsigned char
*outBuffer, int& out_Size);
public:
int funtion1(array<unsigned char>^ inBuffer, unsigned inType,
array<unsigned char>^ outbuf, int% out_size)
{
pin_ptr<unsigned char> p_inbuf = &inBuffer[0];
pin_ptr<unsigned char> p_out = &outbuf[0];
pin_ptr<unsigned char> p_out_size = &out_size;
funtion((unsigned char*)p_inbuf, inType, (unsigned char)p_out,
*p_out_size);
}
}

I start to forget my managed C++ already so you better check it out on MS
web site first...

Herbert Saal said:
Hello,

I have a c++ class in a dll. like this one:

class __declspec(dllexport) MyClass
{
private:
public:
MyClass(void);

int funtion1(unsigned char * inBuffer, unsigned int inType, unsigned char
*outBuffer, int& out_Size);
int function2(void *inBuffer_a, void *inBuffer_b, unsigned long int
*outResult);
};

Note that the function parameters are prefixed with "out" and "in"
depending of the parameter.

I tried to use the class with the DLLImport functionality but it doesn't
works, i'm not sure if i have to use this with classes.
I'm working with c#.

Please help me.

Regards,
Herbert
 

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