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

  • Thread starter Thread starter Jon Skeet
  • Start date Start date
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.
 
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
 
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 too had a similar situation. I then converted my C++
apis into a COM DLL and referenced the same within C#
project..
 
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
 
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
 
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
 
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.
 
Back
Top