P/使用运行时文件名调用本机DLL [英] P/Invoking to Native DLL with Runtime Filename

查看:85
本文介绍了P/使用运行时文件名调用本机DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于版本控制的原因,能够在本机C ++ dll上使用p/invoke对我来说将是有益的,其文件名将在运行时确定,例如:

For versioning reasons it would be beneficial for me to be able to use p/invoke on a native C++ dll whose filename will be determined at runtime, as such:

[DllImport(myDllFilename)]
private static extern void MyInvokedMethod();

不幸的是,[DllImport]是一个属性,需要一个常量表达式.还有其他解决此错误的方法吗?

Unfortunately [DllImport] is an attribute and demands a constant expression. Are there any other options for getting around this error?

我知道我可以在不同的文件夹中使用相同名称的文件,并在运行时加载正确的文件,但是能够使用不同名称的文件将是我的理想选择.

I am aware I can use identically named files in different folders and load the right one at runtime, but being able to use differently named files would be ideal for my case.

推荐答案

要实现所需的功能,您需要PInvoke几件事(LoadLibraryGetProcAddress-请参见下面的链接)...这称为后期绑定"本机代码".使用此代码并不容易/建议,但有可能...

To achieve what you want you need to PInvoke several things (LoadLibrary and GetProcAddress - see links below)... it is called "late binding native code"... using this is not easy/recommended but it is possible...

一些具有说明,示例和源代码的相关链接:

Some relevant links with explanations, samples and source code:

  • http://blogs.msdn.com/b/junfeng/archive/2004/07/14/181932.aspx
  • http://www.codeproject.com/KB/cs/dyninvok.aspx
  • http://pinvoke.net/default.aspx/kernel32/GetPRocAddress.html
  • http://www.pinvoke.net/default.aspx/kernel32.loadlibrary

这篇关于P/使用运行时文件名调用本机DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆