创建dll
1.win32 console application,dll,输入dll的名称,此例为dlltest
2.为dll写方法,例如 为dll增加一个相加的方法
extern "C" _declspec(dllexport) int Add(int,int); //注意了
int _declspec(dllexport) Add(int a, int b)
{
return a+b;
}
3.Build,生成dll
调用dll
1.创建一个win32 console application,名称为helloc,右键选择header files,添加已存在项目,浏览找到windows.h并添加到项目的头文件中
2.引入dll,将生成的dll复制到helloc.cpp所在的文件
3.引入window
#include <windows.h>
typedef int (* MYPROC)(int,int);
4.使用dll
int main()
{
int error;
MYPROC pFunc;
HMODULE hdll_lib =::LoadLibraryEx(TEXT("dlltest.dll"),NULL,0);
//
if(hdll_lib == NULL)
{
error = GetLastError();
cout<<error<<endl;
FreeLibrary(hdll_lib); return -1;
}
pFunc=(MYPROC)GetProcAddress(hdll_lib, "Add"); //
if(!pFunc)
{
error = GetLastError();
cout<<error<<endl;
FreeLibrary(hdll_lib); return -1;
}
int a=pFunc(1,2);//
cout<<a<<endl;
FreeLibrary(hdll_lib);
return 0;
}
1.win32 console application,dll,输入dll的名称,此例为dlltest
2.为dll写方法,例如 为dll增加一个相加的方法
extern "C" _declspec(dllexport) int Add(int,int); //注意了
int _declspec(dllexport) Add(int a, int b)
{
return a+b;
}
3.Build,生成dll
调用dll
1.创建一个win32 console application,名称为helloc,右键选择header files,添加已存在项目,浏览找到windows.h并添加到项目的头文件中
2.引入dll,将生成的dll复制到helloc.cpp所在的文件
3.引入window
#include <windows.h>
typedef int (* MYPROC)(int,int);
4.使用dll
int main()
{
int error;
MYPROC pFunc;
HMODULE hdll_lib =::LoadLibraryEx(TEXT("dlltest.dll"),NULL,0);
//
if(hdll_lib == NULL)
{
error = GetLastError();
cout<<error<<endl;
FreeLibrary(hdll_lib); return -1;
}
pFunc=(MYPROC)GetProcAddress(hdll_lib, "Add"); //
if(!pFunc)
{
error = GetLastError();
cout<<error<<endl;
FreeLibrary(hdll_lib); return -1;
}
int a=pFunc(1,2);//
cout<<a<<endl;
FreeLibrary(hdll_lib);
return 0;
}