Currently I'm trying to compile a C/C++ library for S开发者_高级运维ilverlight Project in order to re-use a c-based codec - FFMpeg, within the silverlight In-Browser Application.
Approaches Tried :
1) Try to include a C++ Class within Silverlight DLL project by declaring c function which is exported in dll as - __declspec(dllexport)
The compilation works but when I tried to reference the C method from within Silverlight Project results in the Native call outside CLR issue.
url ref. - http://social.msdn.microsoft.com/Forums/en-US/clr/thread/339afacb-c9f2-46a4-9feb-8df793a65155
So I've realized after further reading that the silverlight error is becuase of the CLR being able to allow Managed Assemblies only and not Native Code.
Conclusion : One cannot include C++/C code directly within silverlight dll.
2) Next, I've been trying to create a C++ based Managed Assembly with the goal to incorporate a simple C++ class / C function within a Silverlight Managed Assembly.
Steps -
- Create a C++ CLR Class Lib project providing a managed assembly
- Create a C#/.Net Wrapper Class Lib project which calls the C++ CLR Lib which is also a managed assembly
- Convert the C#/.Net managed assembly Wrapper dll into a silverlight managed assembly dll
- Consume the silverlight dll within a silverlight application
url ref. - http://www.netfxharmonics.com/2008/12/Reusing-NET-Assemblies-in-Silverlight
Conclusion : Not sure about the possiblility since I'm still trying get success with the conversion steps which is a bit complex to follow.
Is there a tested approach to be able to re-use C code within Silverlight Lib ?
TIA, Anthony
Basically you want to access a COM Interop assembly from Silverlight.
This is only possible in a trusted application (http://msdn.microsoft.com/en-us/library/ee721083(v=vs.95).aspx)
Trusted applications in SL 4 are only allowed in OOB apps, if you want to use a COM Interop assembly from a Web application in Silverlight you'll have to use SL5.
Here's a bit of extra info on how to use COM Interop from a trusted application:
http://www.uxmagic.com/blog/post/2010/03/09/COM-InterOp-for-Trusted-Applications-in-Silverlight.aspx
Hope this helps you.
In Silverlight 4 you can only access native code via COM interop. It doesn't help to wrap the native code in a mixed-mode (C++/CLI) assembly.
The next version of Silverlight will allow you to access native code via P/Invoke. Don't know if it will support mixed-mode assemblies.
Your current options are:
- Create a COM wrapper for your library
- Wait for Silverlight 5 and use P/Invoke
AFAIK both solutions will require the native code libraries to be installed on the clients before starting the silverlight app.
精彩评论