簡體   English   中英

從Win32控制台應用程序調用dll [C]

[英]Calling dll from a Win32 Console Application [C]

大家好,謝謝您的寶貴時間。 這是在Visual Studio 2012中創建的,而我使用的是標准Windows庫。

我正在嘗試顯式調用DLL函數,並且我相信所編寫的代碼是正確的; 但是,我收到一個錯誤。 我不確定在小型C控制台應用程序中或從無法訪問其內部工作原理的DLL中編寫的某些內容是否出錯。

//global area

HINSTANCE _createInstance;
typedef UINT (CALLBACK* LPFNDLLFUNCLOOKUP)(AccuInput*, AccuOut*);
LPFNDLLFUNCLOOKUP lpfnDllFuncCASSLookup;
typedef UINT (CALLBACK* LPFNDLLFUNCINIT)(BSTR);
LPFNDLLFUNCINIT lpfnDllFuncInit;
typedef UINT (CALLBACK* LPFNDLLFUNCCLOSE)(); 
LPFNDLLFUNCCLOSE lpfnDllFuncClose;
HMODULE unmanagedLib;

這是我的主要功能:

int main() {

// Load Library
BSTR configFile;
unmanagedLib = LoadLibraryA((LPCSTR) "AccuAddressUnMgd.dll");
// Initialize AccuAddress COM dll
lpfnDllFuncInit = (LPFNDLLFUNCINIT)GetProcAddress(unmanagedLib, (LPCSTR)"Init");
// This function will lookup the address
lpfnDllFuncCASSLookup = (LPFNDLLFUNCLOOKUP)GetProcAddress(unmanagedLib, (LPCSTR)"AccuCassLookup");
// This function will call AccuAddress COM DLL Close function
lpfnDllFuncClose = (LPFNDLLFUNCCLOSE)GetProcAddress(unmanagedLib, (LPCSTR)"Close");
// Append “config.acu” file path.
configFile = SysAllocString(L"C:\PathTo\Config.acu");
printf("ConfigPath created");
lpfnDllFuncInit(configFile);
printf("ConfigFile consumed");
SysFreeString(configFile);
return 0;
}

這是我收到的錯誤:

Unhandled exception at at 0x75D4C54F in RDISample1.exe: Microsoft C++ exception: _com_error at memory location 0x001AFAC0.

錯誤發生在:

lpfnDllFuncInit(configFile);

因此,我想我的問題分為兩個部分。 基於代碼,我可以說出錯誤是在DLL函數中嗎?

第二個問題,當調用GetProcAddress時 ,將字符串封裝在LPCSTR中(如函數轉換還是類型轉換)的意義(如果有的話)是什么?

lpfnDllFuncClose = (LPFNDLLFUNCCLOSE)GetProcAddress(unmanagedLib, LPCSTR("Close"));

再次感謝您的幫助。 我一直在進行大量研究,但DLL仍然令人困惑。

最初的錯誤是由您正在使用的庫無法正確處理不存在的文件引起的。

您給出的路徑包含單斜杠\\ ,它們被當作轉義符,而不是路徑分隔符。 路徑分隔符必須轉義,即\\\\才能正確處理。

將字符串文字轉換為LPCSTR沒有意義。

至於肯定來自DLL的_com_error。 我建議將其包裝在:

try
{
  ...
} catch(_com_error const & e)
{
  wprintf(L"Caught a com error: %s\r\n", e.ErrorMessage());
}

然后,您也許可以找出問題所在。

暫無
暫無

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

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