[英]How to reference a dll to Visual Studio without lib file
我需要在我的項目中添加一個第三方庫,他們只提供一個 .dll 文件(沒有 .lib)
我已通過轉到 Common Properties -> References -> Add New Reference 下的項目屬性頁將 dll 添加到項目中
我可以在解決方案資源管理器的 External Dependencies 文件夾下看到 dll,所以我猜它被正確包含了。
但是我如何引用dll? 當我嘗試添加實例變量(例如,MCC::iPort::ASCII iPort)來訪問 dll 類時,出現錯誤:名稱后跟“::”必須是類或命名空間名稱,但我知道那是我可以在外部依賴項下的 dll 信息中看到它的類名。
訪問沒有 .lib 文件的裸 DLL 的唯一方法是使用LoadLibrary()
顯式加載 DLL,使用GetProcAddress()
獲取指向要訪問的導出函數的指針,然后將這些指針轉換為正確的函數簽名。 如果庫導出 C++ 函數,則必須傳遞給GetProcAddress()
將被修改。 您可以使用dumpbin /exports your.dll
列出導出的名稱。
extern "C" {
typedef int (*the_func_ptr)( int param1, float param2 );
}
int main()
{
auto hdl = LoadLibraryA( "SomeLibrary.dll" );
if (hdl)
{
auto the_func = reinterpret_cast< the_func_ptr >( GetProcAddress( hdl, "the_func" ) );
if (the_func)
printf( "%d\n", the_func( 17, 43.7f ) );
else
printf( "no function\n" );
FreeLibrary( hdl );
}
else
printf( "no library\n" );
return 0;
}
正如其他人所指出的,可以創建 LIB 文件。 從dumpbin /exports your.dll
獲取導出函數的列表:
ordinal hint RVA name
1 0 00001000 adler32
2 1 00001350 adler32_combine
3 2 00001510 compress
(etc.)
將名稱放入 DEF 文件中:
EXPORTS
adler32
adler32_combine
compress
(etc.)
現在制作 LIB 文件:
lib /def:your.def /OUT:your.lib
對於名稱已被修飾的情況,無論是通過 C++ 名稱修改還是 32 位stdcall
調用約定,只需復制並粘貼dumpbin
報告的任何名稱、修改和所有名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.