C# uses C++ managed DLL

  • Thread starter Thread starter Falk
  • Start date Start date
F

Falk

Is it possible to use / integrate a C++ DLL from C# code
like it is possible with JNI at Java?
Is there any technology like that?
 
You can use the DllImportAttribute to enable you to call methods in the C++
DLL. All you need to know is the signature of the method you wish to call.

For example..

(C++ DLL)

int MyMethod(int foo, short bar);

(C#)

class dllWrapper
{
[DllImport("mydll.dll")]
public static external Int32 MyMethod(Int32 foo, Int16 bar);
}

class MyCSharpClass
{

public void UsesDLL()
{
Int32 x = dllWrapper.MyMethod(10,100); // calls C++ DLL
}
}

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com
 
There are a couple ways you can do this.

If the C++ DLL merely has global functions (ie no class objects), you can
use PInvoke in C# to call them directly.

If you need to deal with C++ objects, you'll need to use Managed C++ to wrap
your C++ class.

Try these articles:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp12192002.asp

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top