I've a DLL (Player.dll) written in C++ that internally uses Windows GDI. I've an application (basically a video player) written in Windows Forms, that internally calls APIs from Player.dll
to render the actual graphics on screen, using p/invoke technique:
public class ProxyPlayer
{
[DllImport("Player.dll", CharSet=CharSet.Unicode, EntryPoint="PlayVideo")]
public static extern void Play(int playerHandle, out TWResult result);
[DllImport("Player.dll", CharSet=CharSet.Unicode, EntryPoint="PauseVideo")]
public static extern void Pause(int playerHandle);
//other methods
}
It's working.
But now, I want to write the same application using Silverlight 4.0. As you know most Windows GDI works with HWND
to render graphics on screen, that is why I pass playerHandle
to ProxPlayer
methods, as you can see above yourself. Window Forms' UserControl
defines a public property called Handle
of type IntPtr
which is equivalent to HWND
, so I pass that to ProxyPlayer
methods. It solved my problem. But Silverlight's UserControl
doesn't have any such property.
So my question basically is, how would I get handle to my silverlight control? Because without it, I cannot call APIs from Player.dll
. But I've to call APS from it. I don't have any other options, as the DLL is actual engine that does literally everything related to interpreting a huge amount of data and then rendering them. So please suggest me solution that fits in with my requirement!
Note: Assume that my silverlight application will always run on Microsoft Windows. So I don't have problem pinvoking Windows GDI.
If you can expose your native DLL as a COM Server that implements IDispatch, you can access that from Silverlight (via the
AutomationFactory
class) if you're in an Out of Browser Trusted Application on Windows.I still recommend (per my comment to Darin's answer) that you take a good look at the platform, as your "PlayVideo"/"PauseVideo" example suggests you're trying to do things the platform can likely already do - and better yet, the platform can do it on MacOS, and in-browser, and without the ugliness of writing your own COM Server, and so on.
You can simply forget about PInvoking into something like this from Silverlight. Silverlight was intended to run cross browser/platform. So imagine your code under MacOS. So concentrate your energy into searching a managed equivalent of this code that could run from Silverlight or you are simply wasting your time.