[英]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.