开发者

VC++ / Dev-C++: How to include an DLL?

开发者 https://www.devze.com 2023-03-31 10:18 出处:网络
I have written an DLL in Delphi which exports functions. I would like to use these functi开发者_C百科ons in a C++ program without using dynamic Linking (LoadLibrary() API-Call).

I have written an DLL in Delphi which exports functions. I would like to use these functi开发者_C百科ons in a C++ program without using dynamic Linking (LoadLibrary() API-Call).

The "import" declaration would be

extern "C" int __stdcall getVersionNumber();

I mainly use Bloodshed Dev-C++ which creates Windows Executables. But I do not know how to tell the compiler that it should import the function "getVersionNumber" from "STATMONDLL32.dll".

After I spent many hours by googling the problem, without any result (there was only weird stuff written about .a files and .lib files, which I do not have compiled by Delphi...) I have also installed VC++, but even there, I could not find a way to tell the compiler to use a specific DLL.

I have created a DEF file for this DLL - how can I tell Dev-C++ and/or VC++ to use it? (Dev-C++ prefered)

// Edit: Delphi is creating UNDECORATED symbols. The symbol is exactly "getVersionNumber".

I have created following DEF file with an alias for decoration:

LIBRARY   STATMONDLL32
EXPORTS
  getVersionNumberA = _getVersionNumberA@0

I have created a *.lib file with VC++ "lib.exe":

lib.exe /DEF:StatMonDll32.def /OUT:StatMonDll32.lib

I have included the lib in VC++ linker settings.

But VC++ tells me that it cannot resolve the external symbol _getVersionNumberA@0 ! Please help!

// Edit: I have uploaded the DLL here: http://www.viathinksoft.de/temp/StatMonDll32.dll . Can you access the symbol getVersionNumberA with VC++ ? I am searching for a solution since 6 days now :'-(

Best regards

Daniel Marschall


You can use dynamic linking, it should work something along the lines of:

extern "C" typedef int (__stdcall *pfnadd)(int a, int b);
extern "C" typedef int (__stdcall *pfngetversion)(void);
HMODULE mod = LoadLibraryA("mydll.dll");
pfnadd add = (pfnadd)GetProcAddress(mod, "Add");
pfngetversion getVersionNumberA =
       (pfngetversion)GetProcAddress(mod, "getVersionNumberA");

and then you can just call using the function pointer:

add(1, 2);
std::cout << getVersionNumberA();

Although, it seems like your question has bits and pieces of two different functions!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号