【发布时间】:2017-08-05 02:36:24
【问题描述】:
以上是我的文件夹结构。我有一个 Cordova 应用程序和一个 Windows 运行时组件 - IBscanUltimate。包含文件夹具有调用非托管 IBscanUltimate.dll 的 C# 代码。 Class1.cs是这样的:
using System;
namespace IBscanUltimate
{
public sealed class Class1
{
public static String getSDK()
{
IBscanUltimate.DLL.IBSU_SdkVersion a = new DLL.IBSU_SdkVersion();
IBscanUltimate.DLL._IBSU_GetSDKVersion(ref a);
return "SDKVersion: " + a;
}
}
IBScanUltimateApi.cs 和 _IBSU_GetSDKVersion 看起来像这样:
internal partial class DLL
{
[DllImport("IBScanUltimate.DLL")]
private static extern int IBSU_GetSDKVersion(ref IBSU_SdkVersion pVerinfo);
public static int _IBSU_GetSDKVersion(ref IBSU_SdkVersion pVerinfo)
{
int nRc = IBSU_STATUS_OK;
nRc = IBSU_GetSDKVersion(ref pVerinfo);
return nRc;
}
}
我已将 DLL 放置在许多位置以查看它是否会被拾取,并且它们都具有上述属性。但是当我尝试运行我的应用程序时,它说无法找到 IBScanUltimate.DLL
输出是这样的:
我不确定我做错了什么以及为什么 DLLImport 找不到我的 dll。感谢您的帮助。
确切的错误是:
System.DllNotFoundException: Unable to load DLL 'IBScanUltimate.DLL': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
更新 #1:
我遇到过https://msdn.microsoft.com/en-us/library/windows/desktop/hh447159(v=vs.85).aspx 这篇文章是解释LoadPackagedLibrary 函数可以用来加载dll。我没有看到任何关于如何在 C# 中使用它的示例。
更新 #2:
Specify the search path for DllImport in .NET 提到可以使用SetDllDirectory 或AddDllDirectory。他有一个SetDllDirectory 的代码 sn-p,但参数是 string[] paths。我将如何指定相对参数?
更新 #3:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
public static bool setPath(String path)
{
//Windows.Storage.
//return SetDllDirectory("ms-appx:///");
return SetDllDirectory(path);
}
我尝试使用我的应用程序应该可以访问的多个位置调用 SetDllDirectory(path) 方法,但我一直在“错误”。我尝试过的几个例子:
NativeMethods.setPath(Package.Current.InstalledLocation.Path.ToString());
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder folder = Windows.Storage.KnownFolders.MusicLibrary;
这是我的应用程序的安装位置:
C:\Users\AAA\App\hello\platforms\windows\build\windows\Debug\x64\AppX
我可以看到我的 DLL 在那里。但是我仍然遇到无法找到 DLL 的异常。我必须在清单上写一些关于这个的东西吗?
更新 #4: 我在 DLL 上运行了一个 dumpbin,我在 dumpbin 中看到了以下 DLL:
WINUSB.DLL
mfc90.dll
MSVCR90.dll
KERNEL32.dll
USER32.dll
GDI32.dll
VERSION.dll
MSVCP90.dll
SETUPAPI.dll
我想我想分别检查上面的每个 dll,看看我的 Windows 运行时是否可以选择它?其中之一可能是未加载的罪魁祸首?
更新 #5: 在看到 Peter Torr - MSFT 的答案并在 Google 上搜索 MFC 后,我发现了这篇文章 https://msdn.microsoft.com/en-us/library/d06h2x6e.aspx,其中指出:
The MFC classes and their members cannot be used in applications that execute in the Windows Runtime.
我想现在结束这场疯狂的狩猎。我将关闭它,因为我尝试加载的库依赖于 Windows 运行时不可用的库。 我有这种感觉,因为 Windows 窗体应用程序会运行,但转换为 Windows 运行时的代码会给出找不到 DLL 的错误。感谢彼得指导正确的方向。
【问题讨论】:
-
更改了 C# 项目。我不明白你关于文件名的问题。如果这是您想要的,我也粘贴了属性屏幕截图。它仍然给我同样的错误;找不到 DLL。
-
你说的是关闭的项目:IBscanUltimateWRT?
-
看到您关于名称冲突的评论后,我摆脱了该项目,创建了一个名为 IBsWRT 的新 Windows 运行时项目。
-
@Norman:你捕获了“devenv.exe”。那不是你的应用程序,那是 Visual Studio。抱歉,我帮不了你。
-
@Noman 你检查过这些链接吗 - stackoverflow.com/questions/28620082/… stackoverflow.com/questions/39626809/…
标签: c# windows cordova windows-runtime windows-8.1