使用DLL而不导入LIB [英] using DLL without importing the LIB
本文介绍了使用DLL而不导入LIB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个用VC ++ 2005编写的应用程序,我创建了一个DLL(导出一个函数)
我的DLL中的代码如下:
Hi all,
I have an application written in VC++ 2005 and i create a DLL (exporting a single function)
The Code in my DLL is as below :
#include <stdio.h>
#include <atlstr.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL()
{
CString str;
printf ("Hello from DLL !\n");
}
}
问题是我想通过导入生成的LIB从我的DLL导入函数我不知道如何在VC2005中这样做。
Plz有人帮助我或给我一个线索。 Thx
The prob is i wanna import the Function from my DLL withour Importing the generated LIB and i dont know how to do that in VC2005.
Plz somebody help me or guive me a clue. Thx
推荐答案
这是通过加载库,获取所需函数的地址并调用它们来完成的(另请参阅GetProcAddress
[ MSDN中的^ ]:
This is done by loading the library, getting the adresses of the required functions, and call them (see alsoGetProcAddress
[^] in the MSDN):
typedef VOID (CALLBACK* LPFN_DHFDLL)();
HINSTANCE hInst = ::LoadLibrary(_T("MyLib.dll"));
if (NULL != hInst)
{
LPFN_DHFDLL pDisplayHello =
(LPFN_DHFDLL)::GetProcAddress(hInst, "DisplayHelloFromDLL");
if (pDisplayHello)
pDisplayHello();
::FreeLibrary(hInst);
}
查看此内容。
当你说你没有使用import lib时,我假设你没有生成它。因此不需要使用__declspec(dllexport)来导出函数,而是可以使用def文件:
DefFile.def
================
图书馆MyDLL
出口
DisplayHelloFromDLL >
================
现在来访问这个。我们所要做的就是将MyDLL映射到我们的exe地址空间。为此我们可以使用'LoadLibrary'。检查一下:
Hi,
Check this.
When you say that you are not using import lib, I assume that you are not generating it. So there is not need to use __declspec(dllexport) to export a function, rather you can use a def file:
DefFile.def
================
LIBRARY "MyDLL"
EXPORTS
DisplayHelloFromDLL
================
Now coming to accessing this. All we have to do is to map that "MyDLL" into our exe address space. For this we can use using 'LoadLibrary'. Check this:
#include <windows.h>
// Declaring Function pointer for DisplayHelloFromDLL
typedef void ( WINAPIV* LPFN_DISPLAYHELLO ) ( );
void main()
{
// Loading DLL
HINSTANCE hMyDLL = LoadLibraryA("Dll_New.dll");
// Getting address of the DLL exported function
LPFN_DISPLAYHELLO fnDisplayName = (LPFN_DISPLAYHELLO)GetProcAddress(
hMyDLL,
"DisplayHelloFromDLL" );
// Actual function call
fnDisplayName();
// Releasing DLL
FreeLibrary( hMyDLL );
}
谢谢你们俩。这正是我想要的。我找到了很多教程,但大多数都使用生成的Lib,我发现每次更改DLL时都不适合使用我必须使用该DLL重新编译项目。
再次感谢你。你为我节省了很多时间。
Thank you both. that was exactly what i was looking for. I found many tutorials but most of them use the generated Lib and i found not not suitable coz everytime i change the DLL i have to recompile the projects using that DLL.
thank you again. You saved me a lot of time.
这篇关于使用DLL而不导入LIB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文