I have created a sample application based on the following MS link, https://msdn.microsoft.com/en-us/library/aa391769(v=vs.85).aspx
The only change I have done is putting the hole code inside a dll and loaded the particular dll in my sample win32 executable, using LoadLibrary call.
Dll code:
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
HRESULT hResult;
IWbemLocator* locPtr;
IWbemServices* servPtr;
hResult = CoInitializeEx( 0, COINIT_MULTITHREADED );
//hResult = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_ANONYMOUS, NULL, EOAC_NONE, NULL );
hResult = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &locPtr);
hResult = locPtr->ConnectServer( L"ROOT\\CIMV2", NULL, NULL, 0, 0 , 0, 0, &servPtr );
hResult = CoSetProxyBlanket( servPtr, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
And exe code:
int _tmain(int argc, _TCHAR* argv[])
{
::LoadLibrary(L"mydynamic.dll");
return 0;
}
Code executed properly, dll loaded ok, however code stuck at locPtr->ConnectServer() call.
Most strange thing is that if I write the complete COM code in the exe, it works fine. But not with dll.
Any suggestions?
Note: library connectivity is done with wbemuuid.lib