簡體   English   中英

LoadLibrary失敗,代碼為126,除非逐步執行代碼

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

我正在使用LoadLibrary動態加載opencv dll。 在運行代碼時,該操作經常失敗,但在單步執行代碼時,該操作成功。

這是我導入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;

這是用於加載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

我試過放一個Thread.Sleep(1000); 每行之前,這沒有幫助。 如果我繼續LoadLibrary(p); ,然后越過LoadLibrary(p); ,它每次都會成功。 我等待多少時間才能越過線路似乎沒有什么區別。

如果有關系,我正在加載45個opencv dll。 當我重復自動循環時,它將加載列表中的前12個,然后加載下一個6,然后在以后的任何運行中都不再加載(剩余27個)。 這似乎是一致的。 但是,如果此時我逐個文件地遍歷代碼,則可以加載其余所有代碼。

我正在運行64位Windows 7,我的應用程序的目標是x86。

有誰知道為什么會這樣,以及如何解決?

我為每個迭代(dll)添加了Thread.Sleep(50) ,並在每個API調用之前添加了Application.DoEvents()

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

現在,經過幾次連續一致的加載后,所有dll都被加載了。

整個操作只花了一點時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM