Using Run-Time Dynamic Linking You can use the same DLL in both load-time and run-time dynamic linking. The following source code produces the same output as the load-time example in the previous section. The program uses the LoadLibrary function to get a handle to MYPUTS.DLL. If LoadLibrary succeeds, the program uses the returned handle in the GetProcAddress function to get the address of the DLL's myPuts function. After calling the DLL function, the program calls the FreeLibrary function to unload the DLL. The following example illustrates an important difference between run-time and load-time dynamic linking. If the MYPUTS.DLL file is not available, the application using load-time dynamic linking simply terminates. The run-time dynamic linking example, however, can respond to the error. // File: RUNTIME.C // A simple program that uses LoadLibrary and // GetProcAddress to access myPuts from MYPUTS.DLL.
// If the handle is valid, try to get the function address.
if (hinstLib != NULL) { ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts");
// If the function address is valid, call the function.
if (fRunTimeLinkSuccess = (ProcAdd != NULL)) (ProcAdd) ("message via DLL function\n");
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib); }
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess) printf("message via alternative method\n"); } Because the program uses run-time dynamic linking, you should not link with the import library when creating the program module.
vb 里不是 reference 可以加入吗?我想vc里是不是也有类似的动作?
You can use the same DLL in both load-time and run-time dynamic linking. The following source code produces the same output as the load-time example in the previous section. The program uses the LoadLibrary function to get a handle to MYPUTS.DLL. If LoadLibrary succeeds, the program uses the returned handle in the GetProcAddress function to get the address of the DLL's myPuts function. After calling the DLL function, the program calls the FreeLibrary function to unload the DLL. The following example illustrates an important difference between run-time and load-time dynamic linking. If the MYPUTS.DLL file is not available, the application using load-time dynamic linking simply terminates. The run-time dynamic linking example, however, can respond to the error. // File: RUNTIME.C
// A simple program that uses LoadLibrary and
// GetProcAddress to access myPuts from MYPUTS.DLL.
#include <stdio.h>
#include <windows.h>
typedef VOID (*MYPROC)(LPTSTR);
VOID main(VOID)
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary("myputs");
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts");
// If the function address is valid, call the function.
if (fRunTimeLinkSuccess = (ProcAdd != NULL))
(ProcAdd) ("message via DLL function\n");
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("message via alternative method\n");
}
Because the program uses run-time dynamic linking, you should not link with the import library when creating the program module.
//第一步 定义函数指针就是你要调用的函数,参数必须一致,不然就内存泄露
typedef HANDLE (PASCAL * OPEN)(char *,int);
//第二步 定义本地函数
//一般前两步都在头文件里面声明
OPEN open;
//第三步 加载dll
HMODULE m_hLib;
m_hLib ::LoadLibrary"E:\\yewufuwu\\TERMINALDLL\\Debug\\TerminalDLL.dll");
//输入相应的路径
if(m_hLib == NULL )
{
MessageBox("动态连接库没有找到!!!","LoadLibrary",MB_OK);
return ;
}
//第四步
else
{
MessageBox("动态连接库成功打开...","LoadLibrary",MB_OK);
open=(OPEN)GetProcAddress(m_hLib, "OpenDevice");
}//第五步
long handle =open("EF",1);动态库下的函数如下
//*************打开终端*****************************************
HANDLE __declspec(dllexport) WINAPI
OpenDevice(char * szType,int nType)
{ hReader=theTerminal.OpenReader(szType,0);
if(hReader==0)
return 0;
DWORD ThreadID=0;
return hReader;
}
以前的帖子好多http://community.csdn.net/Expert/topic/3192/3192962.xml?temp=.6919367http://community.csdn.net/Expert/topic/2649/2649772.xml?temp=.1770441
method的名字我是输入的dll的帮助手册里写的method列表里的名字。怎么回事啊?
GetProcAddress(m_hLib, " method ");
是不一样的!你注意method前边或者后边是否有空格