2011-04-03 56 views
1

嗯,我有一些問題,我不明白的錯誤。DLL GetProcAddress問題 - 另一個運行時檢查失敗

這是兩個相關文件:

DLLExporter

#ifdef LOGGER_EXPORTS 
    #define LOGGER_API __declspec(dllexport) __stdcall 
#else 
    #define LOGGER_API __declspec(dllimport) __stdcall 
#endif 

typedef long (CALLBACK *LPFNTIMER)(void); 

// Exported Functions 
bool LOGGER_API Initialize(std::string filename, bool ShowConsole = true); 
bool LOGGER_API Release(void); 
bool LOGGER_API SetTimer(LPFNTIMER); 

bool LOGGER_API Initialize(std::string filename, bool ShowConsole) 
{ 
    [...] 
} 

bool LOGGER_API Release() 
{ 
    [...] 
} 

bool LOGGER_API SetTimer(LPFNTIMER fn) 
{ 
    [...] 
} 

DLLExporter.def

LIBRARY DLLExporter.dll 
EXPORTS 
    Initialize 
    Release 
    SetTimer 

DLLImporter

typedef long (__stdcall *LPFNTIMER)(void); 

typedef bool (__stdcall *LPFNINITIALIZER)(string, bool); 
typedef bool (__stdcall *LPFNTIMERSETUP)(LPFNTIMER); 
typedef bool (__stdcall *LPFNRELEASER)(void); 

[...] 


long __stdcall TimerFunc() 
{ 
    return 0; 
} 

[...] 

if (g_DLLExporter == NULL) 
    return false; 

LogInit = (LPFNINITIALIZER)GetProcAddress(g_DLLExporter, "Initialize"); 
LogRelease = (LPFNRELEASER)GetProcAddress(g_DLLExporter, "Release"); 
LogTimer = (LPFNTIMERSETUP)GetProcAddress(g_DLLExporter, "SetTimer"); 

if (LogInit == NULL || LogRelease == NULL || LogTimer == NULL) 
    return false; 

if (!LogInit("test.log", true)) 
    return false; 

if (!LogRelease()) 
    return false; 

if (!LogTimer(TimerFunc)) 
    return false; 

此代碼一直運行到LogTimer調用失敗,並帶有運行時檢查失敗#0消息。

運行時檢查失敗#0 - 值ESP的 未正確保存跨 函數調用。這通常是調用聲明 與用 不同調用約定聲明的 函數指針一個調用約定函數的 結果。

我已經注意到其他地方報道的__stdcall的問題,但你可以在上面看到我有,包括,我甚至被迫__stdcall調用項目屬性公約(2010 MSVS)。任何想法,爲什麼我只會在計時器函數上獲取錯誤,而不是在init或release函數上?

+1

這應該是這樣嗎? – Mehrdad 2011-04-04 00:56:37

+0

這是一個win32應用程序。 DLLExporter創建DLLExporter.dll DLLImporter創建DLLImporter.exe其中進口DLLExporter.dll在運行時 – 2011-04-04 01:17:51

回答

1

只是在想,也許如果調用LogRelease前LogTimer()()?

而且關於通過適當的參數初始化和SetTimer的是什麼?

+1

我已經做到了,沒有改變產生的變化。當我改變它LogTimer仍然搞砸併產生相同的錯誤,而LogRelease仍然正常工作。 – 2011-04-04 01:19:32

+1

以下鏈接是與相同問題相關的SO鏈接,指示我認爲它與函數命名和調用中的__stdcall有關,但我不認爲這完全是這種情況,因爲我已經明確地更改過一切都在使用__stdcall,以及隱式地通過項目設置。 – 2011-04-04 01:24:22

+0

http://stackoverflow.com/questions/696306/run-time-check-failure-0-loading-queryfullprocessimagename-from-kernel32-dl​​l http://stackoverflow.com/questions/5520441/run-time-check -failure-0-爲什麼-AM-I-獲得 - 這和乜呢,它均值 http://stackoverflow.com/questions/1465207/run-time-check-failure-0-the-value -of-esp-was-not-properly-saved-across-a-fun http:// stackoverflow。com/questions/2433950/c-dll-run-time-check-failure-0-the-value-of-esp-was-not-prop – 2011-04-04 01:26:39

0

好,

不完全知道爲什麼,但在編制發佈工作的罰款。我改變了函數的名稱,現在它在調試和釋放模式下正常工作=)!

謝謝大家!

+0

可能與http://support.microsoft.com/kb/822039有關 – 2011-04-04 02:16:56