santosh
santosh

Reputation: 504

Use of C or C++ APIs in C# Application

I have small doubt regarding windows mobile development ,is it possible to use c or C++ Apis in C# application and Vice versa ??. If it is possible how it has to be done ??

Upvotes: 1

Views: 202

Answers (4)

S.C. Madsen
S.C. Madsen

Reputation: 5256

When I first started using the P/Invoke mechanism, i found looking at examples really helpfull. Turns out that "P/Invoking" a lot of the Win32 C-API functions is described on pinvoke.net

Upvotes: 2

Bojan Resnik
Bojan Resnik

Reputation: 7378

If you have a native DLL with exported C functions, you can easily use them via P/Invoke mechanism.

C++ classes, however, are rather difficult and error prone to use from C++. For this, you would be much better off writing a managed assembly wrapper in C++/CLI. This will produce a .NET assembly which you can use from C#, and you will be able to use C++ libraries just as you would from a regular C++ project.

Upvotes: 1

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56707

You can use "normal" C-APIs through so called P/Invoke calls ("Platform Invoke"). This includes both the Windows API and other libraries that export normal function calls. I do not know about C++ DLLs that return objects, however. This might be harder...?

On Windows, you can always create your .NET DLL in a way that it can be registered as COM library. Then you can use the exposed .NET classes like any other COM object from any language that supports the use of COM objects (e.g. VB6, Delphi, C++, etc.).

That, however, does not seem to work in Windows Mobile!

So: Use C-APIs from C# on any platform - yes, no problem, learn how to P/Invoke Use C# DLLs from C++ on Windows - yes, need to create COM objects Use C# DLLs from C++ on Windows Mobile - AFAIK that does not work

Upvotes: 1

Yakeen
Yakeen

Reputation: 2215

C is very easy, C++ is more complicated due mangling. Read this description.

Upvotes: 0

Related Questions