根据我对vc的试验得出的结论如下:
vc对vatabled的布局是如下确定的
// This class is exported from the trydll2.dll
class __declspec(dllexport) CTrydll2 {
public:
CTrydll2(void);
virtual void diplay();
virtual int fnp(int i);
// TODO: add your methods here.
};
vtbale布局如下:
display()//第零个元素
fnp(int)//第一个元素
、。。也就是根据声明的顺序来决定他们在 vatable中的顺序。
如果要在客户端中正确使用则生民顺需要一致!!!
一下是我得错误代码:
///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);
virtual void diplay();
virtual int fnp(int i);
// TODO: add your methods here.
};
//extern TRYDLL2_API int nTrydll2;//·ÅÔÚÕâÀï²»»áÖض¨Òå!!
extern "C"
{
TRYDLL2_API int nTrydll2;
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;//½«µ¼ÖÂÖض¨Òå
extern "C" 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()
{
CTrydll2 *p=new CTrydll2;
return p;
}
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"
class __declspec(dllimport) CTrydll2
{
public:
CTrydll2(void);
virtual int fnp(int i);//请注意这里的声明顺序和dll不一样
virtual void diplay();
//virtual int fnp(int i);
// TODO: add your methods here.
};
//__declspec(dllimport) int nTrydll2;
//end¡£¡£¡£..
#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;
}
//.......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中的fnp(int )但由于参数不正确所以出现运行时错误
pt->fnp(10); //执行时这里实际上调用的是dll中的display()但由于参数不正确所以出现运行时错误
/*之所以会出现上面的错误是由于编译main时它确定的vtable和dll的vatble不同。编译main时使用的是
main中声明的 vtable,但是在运行时pt所指向的是dll的vtable所以出现了调用的错误
*/
::FreeLibrary( hi );
return 0;
}
//关于这一点。。。