Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2005

Tip: Looking for answers? Try searching our database.

How to use a C++ class in .NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Herbert Saal - 20 Jul 2005 14:08 GMT
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
Lloyd Dupont - 20 Jul 2005 14:27 GMT
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...

> Hello,
>
[quoted text clipped - 23 lines]
> Regards,
> Herbert
Herbert Saal - 20 Jul 2005 19:34 GMT
Thank u very much Lloyd!

> PInvoking C++ is not very common.
> For one thing you could ONLY PInvoke C++ dell compiled with Microsoft
[quoted text clipped - 72 lines]
>> Regards,
>> Herbert

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.