HINSTANCE CreateInstanceFromDll(LPCTSTR lpDllName, REFCLSID rclsid, REFIID riid, LPVOID * ppv)
{
(*ppv) = NULL;
HINSTANCE hDll = LoadLibrary( lpDllName );
if ( NULL == hDll )
{
return NULL;
}
typedef HRESULT (__stdcall *GETCLASS_PROC)(REFCLSID,REFIID,LPVOID*);
GETCLASS_PROC procGetClassObject = (GETCLASS_PROC)GetProcAddress( hDll, "DllGetClassObject" );
if( procGetClassObject )
{
IClassFactory* pFactory = NULL;
HRESULT hr = procGetClassObject(rclsid, IID_IClassFactory, (void**)&pFactory);
if( pFactory )
{
hr = pFactory->CreateInstance( NULL, riid, ppv);
pFactory->Release();
pFactory = NULL;
if( NULL == *ppv )
{
FreeLibrary( hDll );
return NULL;
}
}
else
{
FreeLibrary( hDll );
return NULL;
}
}
else
{
FreeLibrary( hDll );
return NULL;
}
return hDll;
}