Tutorial :Using a C++ header with .NET language



Question:

I trying to use a ".h" file from Windows SDK in a .NET language (maybe C#), but without success. This header exposes some Windows Media player functionality through COM. If I use Win32 C++, I can use it with no problems, so I thought that I could use Managed C++ as a "Bridge" to expose it to C#.

The header file is the subscriptionservices.h that comes with Windows Media Player SDK 11 (part of Windows SDK 6).

Is that possible? How could I use that header file in a .NET Application?

Thanks, Eduardo Cobuci


Solution:1

You can use PInvoke to Interop with Win32. If you are trying to use a COM object you should be able to add a reference to the project. Have you looked at this article?

More practically you need to understand the kind of work that you are doing. If you are going to be doing lots of pointer arithmetic then I would recommend managed c++. If not C#. Good luck.


Solution:2

If there is a particular snippet of code or types that you are looking to use from the header file, you can paste them into the PInvoke Interop Assistant and get the C# code generated for you.

http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120


Solution:3

Try PInvoke. I don't think you can use the actual header, but you can reference functions in external unmanaged DLLs.


Solution:4

Yes - it is possible. P/Invoke is your friend. Use this link to translate the headers.

Platform Invoke Data Types


Solution:5

This might give you a head start on the C++ path, if you go that way.

http://code.msdn.microsoft.com/wmpinterop

Note that the WMP interface has some tricky interfaces to P/Invoke into. For me, it's easier just to write a C++ wrapper.


Solution:6

If the API is simply you can use PInvoke, if it's complicated I would suggest using C++/CLI. This allows you to create a 'bridge' between the managed and the unmanaged world.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »