"无法找到指定的过程"错误与.NET 4

Lon*_*erd 4 .net dll dllimport

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

在我的C文件中,我有:

RET_TYPE(int) test_dll_call(int invar)
{
   int retVal = 4 * invar;

   return retVal;
}
Run Code Online (Sandbox Code Playgroud)

我还有一个(32位)WPF C#应用程序,它将DLL加载到类中,如下所示:

[DllImport("MyDll.dll", CharSet = CharSet.Ansi, BestFitMapping = true, ThrowOnUnmappableChar = true)]
public static extern int test_dll_call(int invar);
Run Code Online (Sandbox Code Playgroud)

包装如下:

public void Wrap_TestDllCall()
{
   try
   {
      int outTest = 0;
      int invar = 3;

      outTest = test_dll_call(invar);
   }
   catch (Exception ex)
   {
      dllError = ex.ToString();
   }
}
Run Code Online (Sandbox Code Playgroud)

当我在我的开发框中的调试器中运行它时,这很好.如果我将所有相关文件复制到一个单独的文件夹并从那里运行它,它工作正常.

如果我将所有必要的文件夹复制到另一台运行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()
Run Code Online (Sandbox Code Playgroud)

我在编译的exe和dll上都使用了依赖walker; 它发现的唯一缺少的DLL wer.dllieshims.dll,从我的研究不需要在XP.

我已经安装了VS2012 C++ Redistributable,.NET 4和.NET 4.0.3更新.仍然没有运气.

编辑正如汉斯所指出的,这似乎是应用程序无法在DLL中找到程序; 我也尝试过:

[DllImport("MyDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int test_dll_call(int invar);
Run Code Online (Sandbox Code Playgroud)

__declspec(dllexport) int __cdecl test_dll_call(int invar);
Run Code Online (Sandbox Code Playgroud)

哪个也适用于我的开发盒,但在WinXP盒上给出了相同的错误.

救命!

Lon*_*erd 6

问题解决了.对于那些在将来遇到这种情况的人来说,在我进行故障排除步骤时需要注意的一些事项.

  1. 这个错误并不一定意味着它找不到程序X- 相反它可能意味着它无法Y从另一个dll中找到函数X.
  2. 确保以"发布"模式编译DLL,因为C++可再发行组件不包含调试DLL.
  3. 从shell函数开始,逐个添加片段.

在我上面的测试示例中,问题是我正在编译为Debug版本.

但是,在我的完整功能中,该更改并未解决问题.事实证明我错过了依赖沃克没有抓到的一些DLL.