Re: How to use a C++ class in .NET



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" <hsaal@xxxxxxxx> wrote in message
news:ug7UXwSjFHA.3316@xxxxxxxxxxxxxxxxxxxxxxx
> 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
>


.



Relevant Pages

  • Re: Replacing fgets
    ... Even if u_int8_t is a typedef for unsigned char, ... Didn't your compiler complain here. ... offset is changed ... offset needs to be an int. ...
    (comp.lang.c)
  • Re: How to use a C++ class in .NET
    ... > absolutely compiler dependant. ... > public ref class MyClass ... > int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char* ...
    (microsoft.public.dotnet.framework)
  • Re: querry related to structure padding
    ... char B; ... The compiler is still free to insert padding between B and C, ...
    (comp.lang.c)
  • Re: Error in Passing char pointer
    ... int main ... Always ensure that the compiler sees a prototype for each function before calling it. ... Why is ea unsigned char when everything else is signed char? ... Also your pointer types disagree with the pointers you are passing, unsigned char* and char* are not the same. ...
    (comp.lang.c)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)