...................................trydll2.h.......................
#ifdef TRYDLL2_EXPORTS
#define TRYDLL2_API __declspec(dllexport)
#else
#define TRYDLL2_API __declspec(dllimport)
#endif
// This class is exported from the trydll2.dll
class TRYDLL2_API CTrydll2 {
public:
CTrydll2(void);
void diplay();
int fnp(int i);
// TODO: add your methods here.
};
extern TRYDLL2_API int nTrydll2;
extern "C"
{
TRYDLL2_API CTrydll2 * GetInstance();
TRYDLL2_API int fnTrydll2(void);
TRYDLL2_API int fnp(int para);
};
、、。。。。。。。。。trydll2.cpp。。。。。。。。。。。。。。。。
#include "stdafx.h"
#include "trydll2.h"
//..............................
#include <iostream>
using namespace std;
///...............................
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
TRYDLL2_API int nTrydll2=0;
// This is an example of an exported function.
TRYDLL2_API int fnTrydll2(void)
{
cout<<"fnTrydll2 /n";
return 42;
}
TRYDLL2_API CTrydll2 * GetInstance()
{
return new CTrydll2;
}
TRYDLL2_API int fnp(int para)
{
cout<<"fnp:"<<para<<endl;
return para=1;
}
// This is the constructor of a class that has been exported.
// see trydll2.h for the class definition
CTrydll2::CTrydll2()
{
return;
}
void CTrydll2::diplay()
{
cout<<"CTrydll2::diplay"<<endl;
}
int CTrydll2::fnp(int i)
{
cout<<"CTrydll2::fnp:" <<i<<endl;
i++;
return i;
}
//
#include "stdafx.h"
#include "../trydll2.h" //必须加进来,因为要用类CTrydll2//事实上式没有用的
#include "windows.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
HINSTANCE hi=::LoadLibrary("E://lianxi//DLL_LIB_DCOM_COM//trydll2//Debug//trydll2.dll");
if( hi == NULL )
{
cout<<"LoadLibrary error/n";
return 0;
}
//fnTrydll2:别忘了C++默认编译器会对函数进行改名
//所以你应该强制什么声明不应该改名
//但是这样的话 重载就有麻烦了。。因为重载就是应用了改名的方法
//.......fn.................
FARPROC lpfn= ::GetProcAddress( hi , "fnTrydll2" );
if( lpfn == NULL )
{
cout<<"GetProcAddress fnTrydll2 error/n";
return 0;
}
lpfn();
//FARPROC == int (*lpfn)(void )!!!!!!!!!!!!!!!!!!!!
typedef int (*LPFNP)(int );
LPFNP lpfnP=(LPFNP)::GetProcAddress( hi , "fnp" );
if ( lpfnP == NULL)
{
cout<<"GetProcAddress fnp error/n";
}
else
{
lpfnP(15);
}
//......variable...........我也不知道怎么使用导出的全局变量.....
int *pi = (int *)::GetProcAddress(hi,"nTrydll2");
if( pi==NULL)
{
cout<<"cann't find nTrydll2 error/n";
//return 0;
}
//.............class........
CTrydll2 *pt = NULL;
FARPROC lpfn2 = ::GetProcAddress(hi ,"GetInstance");
if ( lpfn2 == NULL )
{
cout<<"GetProcAddress GetInstance error/n";
return 0;
}
pt = (CTrydll2 *)lpfn2();
pt->diplay(); //连接时错误,本质上显示利用dll导出的东西,使不应该对此进行链接的
pt->fnp(10); 连接时错误
//fnTrydll2();
/*
怎么使用导出的类或者变量呢??
*/
::FreeLibrary( hi );
return 0;
}
//以上的代码显示了如何使用导出的函数 ,也显示了 错误的使用导出类 和变量的方式
这里我想说明的式 如果dll导出局部变量式没有意义的,所以。。呵呵 。最好只导出全局变量
可以我不知道怎么使用