How to P/Invoke to a native dll from Metro?

2019-04-28 16:28发布

I've got a library consisting of two parts - One .net assembly that P/Invokes to a native 3rd party dll. In desktop mode this works just fine: However, when referencing the assembly from a Metro style app and running it, it throws a System.DllNotFoundException on the P/Invoke complaining that "Unable to load DLL 'library': The specified module could not be found."

The native dll does not do anything special but only creates out-going TCP/IP connections to a server. The system cannot know this, but rather the native dll could do anything. This is why I suspect it might not be possible to do this.

Some dll search order is mentioned on msdn regarding Metro style apps, hinting about it is possible to load native dlls?

Also I found this link on msdn talking about loading "libraries" and mentions the PackageDependency in the manifest file, but to me it's still unclear how to add a native dll to the package in a way that it is possible to P/Invoke it.

2条回答
地球回转人心会变
2楼-- · 2019-04-28 17:10

Add it to your project (right click project, Add, Existing Item.) Check that in the properties of that item, Content is set to True (it is false by default.) Build.

查看更多
等我变得足够好
3楼-- · 2019-04-28 17:34

This is probably by design. Your problem might be caused by an error with error code 126 being returned from LoadLibrary. If your application runs in desktop mode you should target desktop mode for your application. You might use an earlier version of Windows as well.

查看更多
登录 后发表回答