How to instantiate a class for a C++ DLL in C#

  • Thread starter Jason Lopez via .NET 247
  • Start date
J

Jason Lopez via .NET 247

I'm having a lot of trouble trying to instantiate a C++ classfrom a DLL in a C# application. The DLL was written in C++(Visual Studio 6.0).

I have the full source code, as well as the compiled DLL. I washoping I'd be able to use it using the DllImport attribute, andcreating empty function declarations. I've done that before andit works well. The problem is that DllImport requires that allfunctions be static. This particular DLL requires that youcreate an instance of this object, then call methods on thatinstance. I haven't been able to figure out how you instantiatea class that is defined in a .DLL.

I suspect that it's possible to recompile the DLL in Managed C++,and then Visual Studio will be able to gather whateverinformation it needs for me to instantiate it like a normalclass - however, there's a bit of a learning curve to usingManaged C++ and I haven't been able to figure out how to makethat work yet.

Thanks!
Jason Lopez


Here is the header file for the DLL I'm trying to instantiate inC#:

#ifdef VIDVIEW_EXPORTS
#define DLL_ENTRY __declspec( dllexport )
#else
#define DLL_ENTRY __declspec( dllimport )
#endif

class DLL_ENTRY IVidView
{
public:
virtual ~IVidView() {};
virtual BOOL Retune() = 0;
virtual BOOL SetAddress(char* src_ip,unsigned short port, char*interface_ip) = 0;
virtual void Play() = 0;
virtual void Stop() = 0;
virtual void AssignVideo(unsigned short pid,int channel) = 0;
virtual void AssignAudio(unsigned short pid,int channel) = 0;
virtual void UnassignVideo(int channel = VV_ALL) = 0;
virtual void UnassignAudio(int channel = VV_ALL) = 0;
};

DLL_ENTRY IVidView* CreateVidView(HWND hWndParent,RECT*pRect); // Call delete after finished with pointer returned bythis call.
 
G

Guest

I'm having a lot of trouble trying to instantiate a C++ class from a DLL in
a C# application. The DLL was written in C++ (Visual Studio 6.0).If I understand correctly, you try to load a VC++ 6.0 dll into a C#
application?
If this is the case then I doubt it will work since VC++ 6.0 has no clue
about .NET (managed code).

Recompiling this VC++ 6.0 dll as a VC++ 2003 mixed mode managed/unmaned
dll and then adding a managed to unmanaged class wrapper should make it
possible to let a c# program access the unmanaged funtionality.

A little information about "managed" and "unmanaged" words:

"Unmanaged code" means that the binary execution code is pure old style code
that is fully understood by your main processor (Pentium, 80486,...).
"Managed code" means that the binary execution code is a virtual processor
code, that must be translated by the CLR into unmanaged code during the load
process of the .NET class.

In C# you have no chouce between unmanaged or managed code, everything is
managed code.
In the case of C++ you have a choice or even mix them.

I hope this helps?
 

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