C + + DLL invocation-dynamic (Explicit)
DLL header file J_test.h
#pragmaOnceextern "C"_declspec (dllexport)voidMaopao (int*p,intcount);extern "C"_declspec (dllexport)intTestint*p,Char*count);extern "C"_declspec (dllexport)intSumintI1,inti2);classj_test{ Public: J_test (); ~j_test (); voidMaopao (int*p,intcount); intSumintI1,inti2); intTestint*p,Char*count);};
DLL implementation Class J_test.cpp
#include"j_test.h"j_test::j_test () {}j_test::~j_test () {}voidMaopao (int*p,intcount) { inttemp =0; for(inti =1; i<count; i++) { for(intj = Count-1; J >= I; j--) { if(P[j]>p[j-1]) {temp=P[j]; P[J]= P[j-1]; P[j-1] =temp; } } }}intTestint*p,Char*count) {count[0] = (Char) p[0]; count[1] = (Char) p[1]; return 0;}intSumintI1,intI2) { returnI1 +I2;}
Caller header File
Goo.h
#pragma onceclass goo{public: goo (); ~goo (); void Main (void);};
Goo.cpp
#include"goo.h"Goo::goo () {}goo::~goo () {} #include<iostream>#include<Windows.h>#include<time.h>typedefint(*dllfun) (int*,int); typedefint(*testfun) (int*,Char*); typedefint(*sumfun) (int,int );using namespacestd;intMain () {Dllfun maopao1; HINSTANCE hDLL; hDLL= LoadLibrary ("f:/workspaces/visual Studio 2013/projects/project-1/project1/debug/project1.dll"); if(hDLL = =NULL) {FreeLibrary (hdll); } maopao1= (Dllfun) GetProcAddress (hDLL,"Maopao"); if(Maopao1 = =NULL) {FreeLibrary (hdll); } inta[Ten]; Srand (Time (0)); for(inti =0; i<Ten; i++) A[i]= rand ()% -; Maopao1 (A,Ten); for(inti =0; i<Ten; i++) cout<< A[i] <<Endl; intb; Charcs[Ten]; Testfun Testfun= (Testfun) GetProcAddress (hDLL,"Test"); b=Testfun (A, CS); cout<<"cs = ... .. ....."<<Endl; for(inti =0; i<Ten; i++) cout<< Cs[i] <<Endl; cout<<"b="<< b <<Endl; cout<<"Sumfun = ... .. ....."<<Endl; Sumfun Sumfun= (Sumfun) GetProcAddress (hDLL,"sum"); b= Sumfun (3,4); cout<<"b="<< b <<Endl; CIN>>b; FreeLibrary (hDLL);}
C + + How to call the DLL, there are two, one is static, the other is dynamic, that is, by calling WindowsAPI to load and unload the DLL, the specific idea:
1. Write a DLL first, there can be no separate header file, because in many cases the DLL is not with the Lib and the header file.
2. Then create another project to invoke the DLL by:
1. declaration header file <windows.h>, stating I want to load and unload DLLs using the Windows32 method
2. Then use a typedef to define a pointer function type. typedef void (*FUN)//This pointer type, to be consistent with the function type and parameters you call, remember, is the pointer parameter is (int *,int)
3. Set a handle instance to take the instance address of the DLL. HINSTANCE hDLL;
The format is hdll=loadlibrary ("DLL Address"); Here the string type is LPSTR, which is not possible when the Unicode character set, so the default character set "Unicode" is changed to support multi-character extension in configuration-Properties-general.
4. Take the address to determine if the returned handle is empty, and if it is an invalid handle, release the memory used to load the DLL.
FreeLibrary (hDLL);
5. Then define a function pointer to get the address of the function you want to use.
First set a function pointer fun, and then through the getprocadress to get the address of the function, what is the function parameter?
The argument is the DLL's handle and the name of the function you want to call: fun= (fun) getprocadress (hdll, "sum");
It is also to be determined if the function pointer is empty, and if the requested function is not taken, the handle is disposed
FreeLibrary (hDLL);
6. The function is then called through a function pointer.
Fun (int *p,int count); you can't use a function name here because the DLL itself is not part of the current CPP. Instead, it is called through windows. Not declared or defined in this project, but exposes a head, to which the pointer obtains his address, which is invoked by a pointer.
When the last call finishes, the handle is disposed
FreeLibrary (hDLL);
This is only done by dynamic loading without involving static. This will be learned in the follow-up.
From: http://www.cnblogs.com/lhbssc/archive/2012/02/08/2342853.html
C + + DLL invocation-dynamic (Explicit)