URGENT: How to use C++ APIs in C# application?

J

Jon Skeet

Mustafa Rabie said:
I am writing and application using C# and i want to use some APIs from an
SDK. I want to know how can i incorperate the libs, .h etc to the C# file
and use the apis.

Look up "interoperation with unmanaged code" in the index of MSDN.
There's a *lot* of information there - exactly how you'll do it will
depend on the type of API etc.
 
M

Mustafa Rabie

Hi
I am writing and application using C# and i want to use some APIs from an
SDK. I want to know how can i incorperate the libs, .h etc to the C# file
and use the apis.

Thanks
Mustafa
 
J

Jon Skeet

Mustafa Rabie said:
thx for the info, the functions i have are in .h files and a .lib file
so do i treat this as a DLL or what exactly?

Hmm... not sure what you can do with a .lib file from .NET, to be
honest. Hopefully an interop expert will pop up and help you...
 
I

irfan

i too had a similar situation. I then converted my C++
apis into a COM DLL and referenced the same within C#
project..
 
M

Mustafa Rabie

thx for the info, the functions i have are in .h files and a .lib file
so do i treat this as a DLL or what exactly?

Thanks
Mustafa
 
N

Nicholas Paldino [.NET/C# MVP]

Mustafa,

If the function is only in a lib file (and not exported through a dll),
then what you will have to do is create an unmanaged project where the
functions are exported from a DLL (you can reference the lib from the DLL
project and then just create wrapper functions for the functions in the
lib). Once you have that DLL, you can make calls to it from .NET through
the P/Invoke layer.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mustafa Rabie said:
ok thanks a lot for your help really appreciated...
HOPE SOMEONE POPS UP!! lets keep our fingers crossed
 
M

Miha Markic

vuola.

You can't do a thing with .lib files in C#.
You should wrap the files with:
a) C++ into DLL
b) (MS) C++.NET to managed class
c) As irfan suggested into COM object.

HTH,
Miha
 
M

Mustafa Rabie

So what i should do is use normal C++ DLL add the functions and write
function wrappers and then use this DLL with C#... correct?

Thanks for your help
Mustafa

Nicholas Paldino said:
Mustafa,

If the function is only in a lib file (and not exported through a dll),
then what you will have to do is create an unmanaged project where the
functions are exported from a DLL (you can reference the lib from the DLL
project and then just create wrapper functions for the functions in the
lib). Once you have that DLL, you can make calls to it from .NET through
the P/Invoke layer.

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