"找不到指定的程序" .NET 4出错

时间:2013-02-15 22:01:34

标签: .net dllimport dll

我正在使用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.dllieshims.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框上也会出现相同的错误。

帮助!

1 个答案:

答案 0 :(得分:6)

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

  1. 此错误并不一定意味着它无法找到过程X - 相反,它可能意味着它无法从{{1}调用的另一个dll中找到函数Y }。
  2. 确保以“发布”模式编译DLL,因为C ++可再发行组件不包含调试DLL。
  3. 从shell函数开始,逐个添加片段。
  4. 在上面的测试示例中,问题是我正在编译为Debug版本。

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