Handle to Silverlight UserControl
Asked Answered
P

2

3

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.

Pragmatism answered 20/1, 2011 at 7:33 Comment(3)
Silverlight's UserControls don't have a Handle (same as WPF's). In the case of WPF, the main windows gets one, but even thats not the case in Silverlight, where the content is hosted by some other application (either the Browser, or sllauncher)Lowkey
@Nawaz : did you ever find a solution to your problem? I would need to do the same as you !Pucker
@Johannes: No. I didn't try that much, because I had an easy alternative: I deployed my player as such (i.e as winform application) and asked user to download it in order to play the video files.Pragmatism
W
2

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.

Wootten answered 20/1, 2011 at 7:42 Comment(0)
C
5

I've a DLL (Player.dll) written in C++ that internally uses win32 API

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.

Colotomy answered 20/1, 2011 at 7:35 Comment(20)
@Darin: I've updated my question. Please see the "Note" at the bottom!Pragmatism
+1 - Silverlight already provides lots of media support (see the MediaElement control) - so why do you want to mess with HWND anymore? Especially in a cross-platform plugin? Silverlight can even do hardware acceleration of some things using DirectX or OpenGL (depending on platform) which will outperform HWND-based rendering. Why not let the platform do what it does best and render things?Wootten
@Austin: Actually I need to call methods from DLL, for that I need handle to the window. I don't have any other options. C++ DLL is actual engine that does literally everything related to rendering. Do you have any solution for that?Pragmatism
@Nawaz, PInvoke works only in Out-Of-Browser mode. Is your application intended to run like this? If, yes why not consider using WPF?Colotomy
@Darin - P/Invoke is not available in Silverlight at all, only a small subset of it (interaction with COM components that implement IDispatch) is available.Wootten
@Nawaz - Silverlight is a rendering engine, so I'm very confused why you want to use Silverlight to feed items to your own rendering engine...Wootten
@Darin: Does it? I thought Silverlight does not PInvoke at all (at least until Version 5 arrives)Lowkey
@Jens, it does but as @Austin pointed out, only a very limited subset of it.Colotomy
@Darin: I've already done that in WPF. Also in WPF you can host winform controls, so I just reused my windform application code. But in silverlight you cannot do that. That is why long back I had started this topic : #4526672Pragmatism
@Nawaz - why not just use WPF? You're already talking about using Windows-specific APIs, so using Silverlight's "big brother" that has lots more hooks into Windows (and P/Invoke support, and so much more) seems like what you're really after.Wootten
@Austin: actually my DLL is not only rendering engine, it also interprets the packets that contain the color-bits, texts, events, and all.Pragmatism
@Darin - to be pedantic, no, P/Invoke is not supported at all. The dynamic COM support allows access to things in a way that is sort of like P/Invoke if you squint, but P/Invoke is a very specific thing ("DllImportAttribute" being involved), which Silverlight currently does not support.Wootten
@Austin: "why not just use WPF?". I've done that already. Please see my comment just before yours.Pragmatism
@Nawaz, So then what exactly are you trying to do? You've already got it in WPF, so...you should be done?Wootten
@Austin: Yeah, I'm done with WPF. But the requirement increased, and now I need to run my application on browser as well. For that, I need to convert my code into Silverlight, right?Pragmatism
@Nawaz - you could use an XBAP to run inside a browser using WPF. I think you're wanting Silverlight to be WPF, but it intentionally is not - after all, we already have WPF for that. Silverlight is meant to be cross-platform (and to play in the security sandbox of a browser, etc...) so this sort of support is rather intentionally not available in the platform.Wootten
@Nawaz, yes you need to convert your code into Silverlight. This conversion should take into account the specifics of the platform you are converting your code into. One of the specifics is that there is no interop support unless running in Out-Of-Browser mode and even in this case very limited.Colotomy
@Austin: " you could use an XBAP to run inside a browser using WPF". What? Can I run my WPF app in browser using XBAP? Is that really possible?Pragmatism
@Nawaz - yes, look up XBAP. It is a feature of WPF since WPF released in .NET 3.0 (before Silverlight was even called Silverlight!) that lets you run WPF apps inside a browser on Windows. It works in IE and Firefox now (originally only in IE).Wootten
@Nawaz - just to be clear, this is not XAP (the names are unfornately very similar) which is the Silverlight file extension for managed apps. Rather, it is XBAP (XAML Browser APplication). Here's the MSDN docs : msdn.microsoft.com/en-us/library/aa970060.aspxWootten
W
2

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.

Wootten answered 20/1, 2011 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.