Silverlight 5 and dll
Silverlight 5 give us a great feature. That开发者_如何学JAVA's P/Invoke (Platform Invoke). The feature make it possible to call a function from system DLL's. But I need to use a ".NET DLL" that was not compiled for Silverlight. How can I do that through P/Invoke or by other way? P.S. My application is "trusted" "in-browser" application.
Update: I have a specific library for interaction with microphone's button. I use it in my .NET application as an usual ".NET DLL" (i.e. I add it to reference and use classes from it). When I've known about P/Invoke I decided to try to use it in Silverlight application, but I've not understood how to do that through P/Invoke.
It depends very much on specifics of your scenario. The only way to call into a full .NET component is via COM. If the .NET component exposes the API you need to COM then you can use the AutomationFactory
to get at it (you can do this in SL4 as well).
If it doesn't have a COM accessible API then perhaps you can install your own dll that creates a set for COM accessible classes around the original dll or if the dll in question is your own code you could modify it to be COM accessible. Of course this assumes you are in a position to install something on the client machine, if not then you are stymied.
I think I have experienced a similar problem to yours. I have solved it by bundling my native DLL as a resource in the Silverlight application XAP, and when starting the application I let the application copy the DLL to a local directory on my computer and add the local directory to the system path while the application is running.
I have described my solution more extensively in this blog post: http://cureos.blogspot.com/2011/09/pinvoke-bundling-native-dlls-in.html
I hope this solves your problem. Good luck!
Anders @ Cureos
I do not follow you.
Which problem do you have exactly?
If you want to use PInvoke to call a windows API, you can do something like this for example:
using System;
using System.Runtime.InteropServices;
namespace MySilverlightApplication
{
public class PlatformInvokeTest
{
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency, int duration);
public static void PlaySound(int frequency, int duration)
{
Beep(frequency, duration);
}
}
}
If you have a .NET DLL (assembly) which is not a SL project or SL control library you could try to PInvoke it but usually you expose its services via WCF or other channel. I understand being Out of Browser you may want to work disconnected... Can you tell us more about your specific needs?
See this article here btw: How to use PInvoke in Silverlight 5
精彩评论