Today try to write a simple c++dll, and call it with another CPP, do not say anything, first put the code
1.DLL (bubbling Algorithm)
extern"C"_declspec (dllexport)void Maopao (int *p,int count);
void Maopao (int *p, int count)
{int temp=0;
for (int i=1;i <count;i++)
{for (int J=count-1;j>=i;j--)
{if (P[j]>p[j-1])
{temp=p[j];
p[j]=p[j-1];
p[j-1]=TEMP;
}
}
}
2. Calling the DLL
#include <iostream>
#include <Windows.h>
#include <time.h>
typedefInt (*dllfun) (int *,int);
Usingnamespace Std;
int main ()
{Dllfun maopao1;
HINSTANCE hDLL;
Hdll=loadlibrary ("D:\\net Source \\maopaoa_dll\\Debug\\maopaoa_dll.dll");
if (hdll==null)
{FreeLibrary (hdll);
}
maopao1= (Dllfun) GetProcAddress (hDLL,"maopao ");
if (maopao1==null)
{freelibrary (hdll);
int a[10];
Srand (Time (0));
for (int i=0;i <10;i++)
a[i]=rand ()% 50;
MAOPAO1 (a,10);
for (int i= 0;i<10;i++)
cout<<a[i]<<endl;
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, I am here directly in the CPP write function declaration and definition, there is no separate header file, because in many cases the DLL is not with 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.
C + + DLL calls