Import C++ DLL into C#

M

Michael Tissington

I have a DLL written in C++. A number of the functions both expect a string
(BSTR) and also return a string (BSTR)

How do I define this in C# ?
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

Is this a regular C++ dll, or is it a COM dll? If it is, then you can
use the dll through COM interop (just adding the reference in VS.NET will do
it for you).

If it is not a COM dll, but rather a regular DLL with exported
functions, then you can declare the functions in C# code and call it through
the P/Invoke layer.

If neither of these cases apply, then you will need to change the (or
create another) DLL which will expose the functionality in those ways, or
create a managed code wrapper using C++/CLI.
 
M

Michael Tissington

Its a regular DLL with exported functions ...
Where do I find information about the P/Invoke layer ?
 
T

Tobias Funke

Michael,

Here's a quick view at how P/Invoke works in C#:

http://msdn2.microsoft.com/en-us/library/aa287781(vs.71).aspx

But also I suggest you get some hands-on experience with P/Invoke by running
a few samples (such as from the Windows API). Here are some sample tutorials
that call unmanaged functions:

http://www.geekpedia.com/tutorial219_Extracting-Icons-from-Files.html
http://www.geekpedia.com/tutorial218_Emptying-the-Recycle-Bin-using-Csharp.html
http://www.geekpedia.com/tutorial174_Opening-and-closing-the-CD-tray-in-.NET.html

Do a couple of these cause once you learn how to use P/Invoke in C# you'll
never forget.

Regards,
Mr. T.
 
M

Michael Tissington

Thanks - it turns out that I just needed to marshall the return value.
Something like ... AS <MarshalAs(UnmanagedType.AnsiBStr)> String
 

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