我正在使用 Visual Studio 2012 (11.0.51106.01 Update 1) 在 64 位 Windows 7 机器上进行开发。
我有一个支持项目,可以将一些 C 代码编译成(32 位)DLL。在我的标题中,我有:
#define RET_TYPE(type) _declspec(dllexport) type __stdcall
RET_TYPE(int) test_dll_call(int invar);
在我的 C 文件中,我有:
RET_TYPE(int) test_dll_call(int invar)
{
int retVal = 4 * invar;
return retVal;
}
我还有一个(32 位)WPF C# 应用程序,它在类中加载 DLL,如下所示:
[DllImport("MyDll.dll", CharSet = CharSet.Ansi, BestFitMapping = true, ThrowOnUnmappableChar = true)]
public static extern int test_dll_call(int invar);
这被包装如下:
public void Wrap_TestDllCall()
{
try
{
int outTest = 0;
int invar = 3;
outTest = test_dll_call(invar);
}
catch (Exception ex)
{
dllError = ex.ToString();
}
}
当我在我的开发盒上的调试器中运行它时,这工作正常。如果我将所有相关文件复制到一个单独的文件夹并从那里运行它,它工作正常。
如果我将所有必需的文件夹复制到另一台运行 32 位 Windows XP SP3 的计算机,我会收到以下错误:
System.DllNotFoundException: Unable to load DLL 'MyDll.dll': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)
at MyNamespace.MyClass.test_dll_call(Int32 invar)
at MyNamespace.MyClass.Wrap_TestDllCall()
我在编译的 exe 和 dll 上都使用了依赖项walker;它找到的唯一丢失的 dll 是
wer.dll
和 ieshims.dll
,根据我的研究,XP 不需要。我已经安装了 VS2012 C++ Redistributable、.NET 4 和 .NET 4.0.3 更新。仍然没有运气。
编辑 正如 Hans 所指出的,这似乎是应用程序在 DLL 中找不到程序;我也试过:
[DllImport("MyDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int test_dll_call(int invar);
和
__declspec(dllexport) int __cdecl test_dll_call(int invar);
这在我的开发箱上也能正常工作,但在 WinXP 箱上出现相同的错误。
帮助!
最佳答案
问题解决了。在我执行故障排除步骤时需要注意的一些事项,以供将来偶然发现的人使用。
X
-- 更确切地说,这可能意味着它找不到函数 Y
,来自另一个 dll,由 X
调用. 在我上面的测试示例中,问题是我编译为 Debug 版本。
但是,在我的完整功能中,该更改并没有解决问题。事实证明我错过了一些依赖步行者没有捕捉到的 DLL。
关于.net - .NET 4 出现 "The specified procedure could not be found"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904080/