For Programmers: Free Programming Magazines  


Home > Archive > VC Language > March 2005 > How to use VC++ 6.0 dll by C#









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to use VC++ 6.0 dll by C#
modx

2005-03-22, 9:01 pm

Hi All,
I have a library(.dll). It's develop by VC++ 6.0. I would like to know
how C# can used my library? When I add reference at C# project it will show
my dll is not a valid assembly or COM component. How I fix this problem. I
don't want rewrite my dll because it's very big project.

Best Regards,
Mod X


Doug Harrison [MVP]

2005-03-23, 4:01 am

On Tue, 22 Mar 2005 15:59:31 -0800, modx wrote:

> Hi All,
> I have a library(.dll). It's develop by VC++ 6.0. I would like to know
> how C# can used my library? When I add reference at C# project it will show
> my dll is not a valid assembly or COM component. How I fix this problem. I
> don't want rewrite my dll because it's very big project.
>
> Best Regards,
> Mod X


Look up "Platform Invoke" or PInvoke.

--
Doug Harrison
Microsoft MVP - Visual C++
modx

2005-03-23, 4:01 am

Hi Doug,
Thanks for your help.
Everything inside my dll is class. Can I used Platform Invoke? I saw
every example I didn't see DllImport class. It's import only function.

Best Regards,
Mod X


"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:1rsmcklpm1dtb.cjt4lhwesvmj$.dlg@40tude.net...
> On Tue, 22 Mar 2005 15:59:31 -0800, modx wrote:
>
know[color=darkred]
show[color=darkred]
I[color=darkred]
>
> Look up "Platform Invoke" or PInvoke.
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++



Ajay Kalra

2005-03-23, 4:01 am

There are couple of ways:

- Wrap a COM server around your unmanaged functionality (not recommended)
- Use MC++ (much easier) and provide a managed wrapper around your unmanaged
functions. With MC++, you can have managed and unmanaged code coexist
happily. This is only possible in VC++. Keep in mind that your dll can be in
VC6.0 and your wrapper (thru MC++) could be in VC7.X.

--
Ajay Kalra [MVP - VC++]
ajaykalra@yahoo.com


"modx" <modxchat@hotmail.com> wrote in message
news:O7GPxszLFHA.1156@TK2MSFTNGP09.phx.gbl...
> Hi All,
> I have a library(.dll). It's develop by VC++ 6.0. I would like to

know
> how C# can used my library? When I add reference at C# project it will

show
> my dll is not a valid assembly or COM component. How I fix this problem. I
> don't want rewrite my dll because it's very big project.
>
> Best Regards,
> Mod X
>
>



Doug Harrison [MVP]

2005-03-23, 4:01 am

On Tue, 22 Mar 2005 17:11:03 -0800, modx wrote:

> Hi Doug,
> Thanks for your help.
> Everything inside my dll is class. Can I used Platform Invoke? I saw
> every example I didn't see DllImport class. It's import only function.


I've not done this, and you might want to ask about it in the
microsoft.public.dotnet.languages... groups, but here's my understanding of
the situation. First, you're right; PInvoke works function by function, and
it's reasonably OK for interfacing with C-like APIs such as Win32. Now, MS
provides for something called the "Runtime Callable Wrapper" that make it
easy to interface with COM objects from managed code. (There's also the
"COM Callable Wrapper" that goes the other way, exposing .NET types as COM
types to unmanaged code.) This along with PInvoke are the primary interop
mechanisms that allow distinct bits of managed/unmanaged code to use one
another. It sounds like PInvoke would be inconvenient in your case, so what
are your other options? If your native DLL isn't COM-based, you could
provide a Managed C++ shim DLL yourself that provides a managed code
interface to the native code. There are several ways you could approach
this. You could do it with a COM class and take advantage of the RCW. Or
you could simply duplicate your class's interface with a managed class. Or
you could expose a C-style, handle-based interface to your class. (The
latter would look something like, say, the Win32 HWND functions.) Inside
the C++ shim DLL, you would use your existing C++ class from managed code
through the IJW ("It Just Works") mechanism. The end result of this will be
an assembly that your C# program can use directly, avoiding PInvoke
altogether.

--
Doug Harrison
Microsoft MVP - Visual C++
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com