简体   繁体   English

LoadLibrary失败,代码为126,除非逐步执行代码

[英]LoadLibrary failing with code 126 except when stepping through the code

I'm loading opencv dlls dynamically using LoadLibrary. 我正在使用LoadLibrary动态加载opencv dll。 The operation fails frequently when letting the code run, but succeeds when stepping through the code. 在运行代码时,该操作经常失败,但在单步执行代码时,该操作成功。

Here is how I import the dlls 这是我导入dll的方法

[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("Kernel32.dll")]
static extern uint SetErrorMode(uint uMode);
[DllImport("Kernel32.dll")]
static extern uint GetLastError();

const uint SEM_FAILCRITICALERRORS = 0x0001;

Here is the code used to load the dlls. 这是用于加载dll的代码。

// called once
SetErrorMode(SEM_FAILCRITICALERRORS);

// called for each dll string name in a loop
var p = Path.Combine(opencvDirectory, filename);
LoadLibrary(p);
var error = GetLastError();
// repeat with next file

I have tried putting a Thread.Sleep(1000); 我试过放一个Thread.Sleep(1000); before each line, which does not help. 每行之前,这没有帮助。 If I break on, and step over LoadLibrary(p); 如果我继续LoadLibrary(p); ,然后越过LoadLibrary(p); , it succeeds every time. ,它每次都会成功。 It seems to make no difference how much time I wait before stepping over the line. 我等待多少时间才能越过线路似乎没有什么区别。

If it matters, I am loading 45 opencv dlls. 如果有关系,我正在加载45个opencv dll。 When I repeat the automatic loop, it loads the first 12 in the list, then the next 6, then it won't load any more on any subsequent run (27 remaining). 当我重复自动循环时,它将加载列表中的前12个,然后加载下一个6,然后在以后的任何运行中都不再加载(剩余27个)。 This appears to be consistent. 这似乎是一致的。 However if I step over the code at this point, file by file, I am able to load all the rest. 但是,如果此时我逐个文件地遍历代码,则可以加载其余所有代码。

I am running 64 bit Windows 7, and my application is targeting x86. 我正在运行64位Windows 7,我的应用程序的目标是x86。

Does anyone have an idea why this happens, and how to fix it? 有谁知道为什么会这样,以及如何解决?

I added a Thread.Sleep(50) for each iteration (dll), and an Application.DoEvents() before each API call like this 我为每个迭代(dll)添加了Thread.Sleep(50) ,并在每个API调用之前添加了Application.DoEvents()

Thread.Sleep(50);
Application.DoEvents();
LoadLibrary(p);
Application.DoEvents();
var error = GetLastError();

and now the dlls are all loaded after a few passes through consistently. 现在,经过几次连续一致的加载后,所有dll都被加载了。

The whole operation just takes a little longer than I would like. 整个操作只花了一点时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NHibernate只积极加载Dictionary <map> 单步执行代码时 - NHibernate only eagerly loads Dictionary <map> when stepping through code C# 仅在单步执行代码时出现调试错误 - C# Debugging Error Only When Stepping Through Code VS2005:单步执行C#代码时,是否可以跳过代码部分? - VS2005: when stepping through C# code, is there way to skip through sections of code? 在单元测试中单步调试和调试代码 - Stepping through and debugging code in Unit tests 更新集合时出现奇怪的DB上下文行为(在调试器中逐步执行代码时有效) - Strange DB-context behaviour when updating collection (Works when stepping through the code in debugger) 启用优化后,DotNet错误的代码步进 - DotNet Incorrect code stepping when optimisations enabled 执行代码时找不到 ExecutionTimer.cs - ExecutionTimer.cs not found when stepping into code 异步等待执行顺序-代码仅在逐步执行/调试时才有效 - async await execution order - code only actually works when stepping through/debugging 除非单步执行代码,否则Moq /单元测试无法捕获异常 - Moq / Unit Test Unable to Catch Exception unless stepping through code 返回私有变量后的StackOverflowException,仅在单步执行代码时 - StackOverflowException after returning private variable, only while stepping through code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM