64,682
社区成员
发帖
与我相关
我的任务
分享
//文件前边内容略。
//
// Creation function
//
extern "C" IUnknown* CreateInstance()
{
IUnknown* pI = static_cast<IX*>(new CA) ;
pI->AddRef() ;
return pI ;
}
typedef IUnknown* (*CREATEFUNCPTR)() ;
IUnknown* CallCreateInstance(char* name)
{
// Load dynamic link library into process.
HINSTANCE hComponent = LoadLibrary(name) ;
if (hComponent == NULL)
{
cout << "CallCreateInstance:\tError: Cannot load component." << endl ;
return NULL ;
}
// Get address for CreateInstance function.
CREATEFUNCPTR CreateInstance
= (CREATEFUNCPTR)::GetProcAddress(hComponent, "CreateInstance") ;
if (CreateInstance == NULL)
{
cout << "CallCreateInstance:\tError: " //运行时直接代码走到这里,找不到函数
<< "Cannot find CreateInstance function."
<< endl ;
return NULL ;
}
return CreateInstance() ;
}
HINSTANCE hInst = NULL;
//test
hInst = LoadLibrary("..\\lib\\CMPNT1.dll");
if (NULL == hInst)
{
std::cout << "error." << std::endl;
return -1;
}
typedef IUnknown* (*CREATEFUNCPTR)() ;
CREATEFUNCPTR CreateInstance = (CREATEFUNCPTR)::GetProcAddress(hInst, "CreateInstance") ;
if (NULL == hInst)
{
std::cout << "error." << std::endl; //代码没有走到这
return -1;
}
//代码走到这,说明找到了CreateInstance函数了。
cout << "success" << endl;
return 0;
// Load dynamic link library into process.
HINSTANCE hComponent = LoadLibrary(name) ;
if (hComponent == NULL)
{
cout << "CallCreateInstance:\tError: Cannot load component." << endl ;
return NULL ;
}