P /调用动态DLL搜​​索路径 [英] P/Invoke dynamic DLL search path

查看:149
本文介绍了P /调用动态DLL搜​​索路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的应用程序,它的P /调用居住在同一个目录中的应用程序本身的DLL。

I have an existing app which P/Invokes to a DLL residing in the same directory as the app itself.

现在(由于佳能生产的crappiest的API之一左右的事实),我需要支持两个版本的API,并确定在运行时,哪一个我应该使用(旧的或新的)。由于DLL文件具有相同的名称(第一次加载其他DLL具有相同的名称,以便只重命名第一个不会帮我),我必须让他们在不同的目录。

Now (due to the fact that Canon produces one of the crappiest API's around) I need to support two versions of this API and determine at run-time which one I should use (old or new). Since the DLLs have the same name (the first one loads other DLLs with same names so just renaming the first one won't help me) I must keep them in different directories.

因此​​,我的问题:什么样的选择我一定要控制哪些目录中的DllImport声明中提到的DLL使用

Hence my question: what options do I have to control what directory the DLL given in a DllImport declaration uses?

我想我可以通过尝试任何这两种想法开始了:

I guess I can start out by trying any of these two ideas:

1)用SetDllDirectory会做第一的P / Invoke之前设置我需要的目录,然后再把重置。

1) Use "SetDllDirectory" to set my desired directory before doing the first P/Invoke and then reset it afterwards.

2)手动加载使用LoadLibraryEx所需的DLL和希望,这将这样的伎俩。

2) Load the desired DLL manually using "LoadLibraryEx" and hope that that will do the trick.

但是否有更多的.NET:十岁上下的方式?尝试先

But are there any more ".NET:ish way" to try out first?

更新:我知道我可以在东西两个独立的.NET程序集的所有访问的DLL,然后将他们每个人在一个单独的目录与相应的API文件。然后,我可以动态加载正确的.NET程序集和正确的DLL的加载自动对子级发生。任何原因,不应该工作?

UPDATE: I realize that I can stuff all access to the DLLs in two separate .Net assemblies and then place each one of them in a separate directory with the corresponding API files. Then I can load the proper .Net assembly dynamically and the loading of the correct DLL whould happen automatically. Any reason that shouldn't work?

我能想到的一个:我将如何去调试这个东西?它可以告诉Visual Studio中的组件(包含在我的解决方案)应放置在一个子目录和调试从那里?

I can think of one: how would I go about debugging this stuff? It is possible to tell Visual Studio that an assembly (contained in my solution) shall be placed in a subdirectory and debugged from there?

推荐答案

我的慰问,我见过的API之一,这确实是令人震惊的坏。更大的问题是,你必须能够说服Windows来查找DLL。他们不会在你的.exe文件目录,这样的默认值将不起作用。使用SetDllDirectory会()会的工作,采用Environment.CurrentDirectory也会做。调用LoadLibrary无法正常工作,在P / Invoke的编组将使用调用LoadLibrary本身。

My condolences, I've seen one of the APIs and it was indeed shockingly bad. The bigger problem is that you'll need to be able to convince Windows to find the DLL. They won't be in your .exe directory so the default won't work. Using SetDllDirectory() would work, using Environment.CurrentDirectory does too. LoadLibrary cannot work, the P/Invoke marshaller will use LoadLibrary itself.

如果这是在所有选项,你可以使用不同的名称为两个P / Invoke的声明,使用不同的参数为的DllImport()构造函数,并使用入口点属性。听起来不像是会飞翔。

If it is at all an option, you can use different names for the two P/Invoke declarations, using different arguments for the DllImport() constructor and using the EntryPoint attribute. Doesn't sound like that will fly.

这篇关于P /调用动态DLL搜​​索路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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