简体   繁体   English

如何将非托管C ++ DLL添加到Silverlight 5项目中

[英]How to add unmanaged C++ dll into a Silverlight 5 project

I need to use a my own unmanaged C++ dll into Silverlight 5 project. 我需要在Silverlight 5项目中使用我自己的非托管C ++ DLL。

As I am able to use it using dllImport ie 因为我能够使用dllImport即使用它

   [DllImport(@"D:\myLib\Debug\myLib.dll")]
    static extern int add(IntPtr ptr);

It works fine as I metioned a absoulte path in DllImport. 它工作正常,因为我在DllImport中提到了一个绝对的路径。 But my problem is, I want to use the relative path. 但我的问题是,我想使用相对路径。

As I need to add unmanaged C++ dll in my silverlight 5 project and specify path from there. 因为我需要在我的silverlight 5项目中添加非托管C ++ dll并从那里指定路径。 I am investigating from last two days I did not find any solution for this issue. 我正在调查过去两天我没有找到任何解决这个问题的方法。

Only I found one related link mentioned below but it also does not help. 只有我找到了下面提到的一个相关链接,但它也无济于事。

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.dllimportattribute.aspx http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.dllimportattribute.aspx

As It suggest how to embed your unmanaged dll into managed assembly. 因为它建议如何将您的非托管DLL嵌入到托管程序集中。 I tried this approach but I am getting "BadImageException". 我试过这种方法,但我得到“BadImageException”。

Please suggest to over come to this issue as it is really urgent for me. 请建议过来解决这个问题,因为这对我来说非常紧迫。

Thanks, 谢谢,

Vipin VIPIN

When Silverlight 5 Release Candidate was made public, I created some support methods for handling this issue of bundling unmanaged non-system DLL:s with your Silverlight 5 RC application. 当Silverlight 5 Release Candidate公开时,我创建了一些支持方法来处理将非托管非系统DLL与您的Silverlight 5 RC应用程序捆绑在一起的问题。 The solution is still valid, since Microsoft did not add any support to deal with the bundling issue in Silverlight 5 RTM. 该解决方案仍然有效,因为Microsoft没有添加任何支持来处理Silverlight 5 RTM中的捆绑问题。

What I do is basically to bundle the unmanaged DLL as a resource file. 我所做的基本上是将非托管DLL捆绑为资源文件。 Through a helper method manually invoked eg in the application startup event handler the resource is copied to a predefined location on disk. 通过例如在应用程序启动事件处理程序中手动调用的辅助方法,资源被复制到磁盘上的预定义位置。 This location is then also included in the system PATH during execution. 然后,该位置在执行期间也包含在系统PATH中。

Elevated trust is required (of course). 需要提升信任(当然)。

More details on this solution can be found in this blog post. 有关此解决方案的更多详细信息,请参阅博客文章。

All code associated with this solution, including sample tests, can be found on Github . 所有与此解决方案相关的代码(包括样本测试)都可以在Github上找到。

UPDATE UPDATE

As pointed out in the comment below by Simon Mourier, one option would also be to load into memory (and release upon application exit) the unmanaged DLL by calling the Kernel32 LoadLibrary function after the DLL has been unpacked from the resources. 正如Simon Mourier在下面的评论中所指出的,一个选项也可以是在从资源中解压缩DLL之后通过调用Kernel32 LoadLibrary函数来加载到内存中(并在应用程序退出时释放)非托管DLL。 This approach for Silverlight is described in more detail in this blog post. Silverlight的这种方法在博文中有更详细的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM