2013-02-03 81 views
0

我用這個編譯器:http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev7.7z儘管返回true,SymInitialize將GetLastError設置爲2,這意味着什麼?

代碼:

#include <windows.h> 
#include <iostream> 
#include <imagehlp.h> 

int main() { 
    HANDLE process = GetCurrentProcess(); 
    if (GetLastError()) { 
    printf("GetCurrentProcess failed: %d\n", GetLastError()); 
    return 1; 
    } 
    if (!SymInitialize(process, NULL, TRUE)) { 
    printf("SymInitialize failed: %d\n", GetLastError()); 
    return 1; 
    } else if (GetLastError()) { 
    printf("SymInitialize returned true but failed nonetheless: %d\n", GetLastError()); 
    return 1; 
    } 
} 

它返回: 「該系統找不到指定的文件」 SymInitialize returned true but failed nonetheless: 2

錯誤沒有2種手段什麼文件,我不能把它放在這裏的任何環境。 我從文檔中瞭解到,如果此函數成功GetLastError 必須爲零。

+2

在文檔中,它說GetLastError *必須返回零,如果函數成功?據我所知,它只是說如果函數失敗,GetLastError會給你更多關於錯誤的信息。所以最有可能的是,如果函數成功,函數不會清除錯誤代碼。 – jalf

回答

3

功能在成功時不會重置錯誤是很常見的。 「SymInitialize」在內部完全有可能調用「在dir1查找文件,在dir2查找文件,...」以及「我無法在dir1中找到它」的錯誤爲2。現在,您必須實際調用SetLastError(0)來清除錯誤代碼,這可能是疏忽或故意SymInitialize中的代碼沒有設置它。你需要檢查SymInitialize的結果來判斷它是否成功。如果它不是成功的,你看看錯誤代碼。但GetLastError()就是這樣 - 發生的最後一個錯誤,而不是「我的最後一個函數成功了!」