Can't build WMI/C++ example in Visual Studio

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I downloaded Visual C++ 2005 Express Edition Beta and Im trying to compile an example WMI program that I got from:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__getting_wmi_data_from_a_remote_computer.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__getting_wmi_data_from_a_remote_computer.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__getting_wmi_data_from_a_remote_computer.asp

I copied & pasted the example into a file I named wmi.cpp. Then I created a WMI project in VC++ and tried to build it. It compiles OK, but I get two errors during linking. Heres the output:

<font size="1 Linking... WMI.obj : error LNK2019: unresolved external symbol __imp_??$?6DU?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z referenced in function _main WMI.obj : error LNK2019: unresolved external symbol "wchar_t * __stdcall _com_util::ConvertStringToBSTR(char const *)" (?ConvertStringToBSTR@_com_util@@YGPA_WPBD@Z) referenced in function "public: __thiscall _bstr_t::Data_t::Data_t(char const *)" ( mailto:??0Data_t@_bstr_t@@QAE@PBD@Z mailto:??0Data_t@_bstr_t@@QAE@PBD@Z ??0Data_t@_bstr_t@@QAE@PBD@Z ) </font>Does anybody know how to solve this? Many thanks in advance. Id ideally like to be able to compile this from a command line, but using VC++ would be great for now.

The source code is copied here:

<font color="#0000ff" size="2 <font color="#0000ff" size="2 #define</font><font size="2 <font color="#000000 _WIN32_DCOM</font> </font><font color="#0000ff" size="2 #include</font><font color="#000000" size="2 </font><font color="#800000" size="2 "stdafx.h" </font><font color="#0000ff" size="2 #include</font><font color="#000000" size="2 </font><font color="#800000" size="2 <iostream> </font><font color="#0000ff" size="2 using</font><font color="#000000" size="2 </font><font color="#0000ff" size="2 namespace</font><font size="2 <font color="#000000 std;</font> </font><font color="#0000ff" size="2 #include</font><font color="#000000" size="2 </font><font color="#800000" size="2 <comdef.h> </font><font color="#0000ff" size="2 #include</font><font color="#000000" size="2 </font><font color="#800000" size="2 <Wbemidl.h> </font><font color="#0000ff" size="2 # pragma</font><font color="#000000" size="2 </font><font color="#0000ff" size="2 comment</font><font color="#000000" size="2 (</font><font color="#0000ff" size="2 lib</font><font color="#000000" size="2 , </font><font color="#800000" size="2 "wbemuuid.lib"</font><font size="2 <font color="#000000 )</font> </font><font color="#0000ff" size="2 # pragma</font><font color="#000000" size="2 </font><font color="#0000ff" size="2 comment</font><font color="#000000" size="2 (</font><font color="#0000ff" size="2 lib</font><font color="#000000" size="2 , </font><font color="#800000" size="2 "credui.lib"</font><font size="2 <font color="#000000 )</font> </font><font color="#0000ff" size="2 #include</font><font color="#000000" size="2 </font><font color="#800000" size="2 <wincred.h> </font><font color="#0000ff" size="2 #include</font><font color="#000000" size="2 </font><font color="#800000" size="2 "ObjBase.h" </font><font color="#0000ff" size="2 int</font><font color="#000000" size="2 main(</font><font color="#0000ff" size="2 int</font><font color="#000000" size="2 argc, </font><font color="#0000ff" size="2 char</font><font size="2 <font color="#000000 **argv)</font> { HRESULT hres; </font><font color="#008000" size="2 // Step 1: -------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // Initialize COM. ------------------------------------------ </font><font size="2 hres = CoInitializeEx(0, COINIT_MULTITHREADED); </font><font color="#0000ff" size="2 if</font><font size="2 (FAILED(hres)) { cout << </font><font color="#800000" size="2 "Failed to initialize COM library. Error code = 0x"</font><font size="2 << hex << hres << endl; </font><font color="#0000ff" size="2 return</font><font size="2 1; </font><font color="#008000" size="2 // Program has failed. </font><font size="2 } </font><font color="#008000" size="2 // Step 2: -------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // Set general COM security levels -------------------------- </font><font size="2 </font><font color="#008000" size="2 // Note: If you are using Windows 2000, you need to specify - </font><font size="2 </font><font color="#008000" size="2 // the default authentication credentials for a user by using </font><font size="2 </font><font color="#008000" size="2 // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ---- </font><font size="2 </font><font color="#008000" size="2 // parameter of CoInitializeSecurity ------------------------ </font><font size="2 hres = CoInitializeSecurity( NULL, -1, </font><font color="#008000" size="2 // COM authentication </font><font size="2 NULL, </font><font color="#008000" size="2 // Authentication services </font><font size="2 NULL, </font><font color="#008000" size="2 // Reserved </font><font size="2 RPC_C_AUTHN_LEVEL_DEFAULT, </font><font color="#008000" size="2 // Default authentication </font><font size="2 RPC_C_IMP_LEVEL_IMPERSONATE, </font><font color="#008000" size="2 // Default Impersonation </font><font size="2 NULL, </font><font color="#008000" size="2 // Authentication info </font><font size="2 EOAC_NONE, </font><font color="#008000" size="2 // Additional capabilities </font><font size="2 NULL </font><font color="#008000" size="2 // Reserved </font><font size="2 ); </font><font color="#0000ff" size="2 if</font><font size="2 (FAILED(hres)) { cout << </font><font color="#800000" size="2 "Failed to initialize security. Error code = 0x"</font><font size="2 << hex << hres << endl; CoUninitialize(); </font><font color="#0000ff" size="2 return</font><font size="2 1; </font><font color="#008000" size="2 // Program has failed. </font><font size="2 } </font><font color="#008000" size="2 // Step 3: --------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // Obtain the initial locator to WMI ------------------------- </font><font size="2 IWbemLocator *pLoc = NULL; hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); </font><font color="#0000ff" size="2 if</font><font size="2 (FAILED(hres)) { cout << </font><font color="#800000" size="2 "Failed to create IWbemLocator object." </font><font size="2 << </font><font color="#800000" size="2 " Err code = 0x" </font><font size="2 << hex << hres << endl; CoUninitialize(); </font><font color="#0000ff" size="2 return</font><font size="2 1; </font><font color="#008000" size="2 // Program has failed. </font><font size="2 } </font><font color="#008000" size="2 // Step 4: ----------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // Connect to WMI through the IWbemLocator::ConnectServer method </font><font size="2 IWbemServices *pSvc = NULL; </font><font color="#008000" size="2 // Get the user name and password for the remote computer </font><font size="2 CREDUI_INFO cui; TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH+1]; TCHAR pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1]; BOOL fSave; DWORD dwErr; cui.cbSize = </font><font color="#0000ff" size="2 sizeof</font><font size="2 (CREDUI_INFO); cui.hwndParent = NULL; </font><font color="#008000" size="2 // Ensure that MessageText and CaptionText identify </font><font size="2 </font><font color="#008000" size="2 // what credentials to use and which application requires them. </font><font size="2 cui.pszMessageText = TEXT(</font><font color="#800000" size="2 "Remote computer account information"</font><font size="2 ); cui.pszCaptionText = TEXT(</font><font color="#800000" size="2 "Enter Account Information"</font><font size="2 ); cui.hbmBanner = NULL; fSave = FALSE; dwErr = CredUIPromptForCredentials( &cui, </font><font color="#008000" size="2 // CREDUI_INFO structure </font><font size="2 TEXT(</font><font color="#800000" size="2 ""</font><font size="2 ), </font><font color="#008000" size="2 // Target for credentials </font><font size="2 NULL, </font><font color="#008000" size="2 // Reserved </font><font size="2 0, </font><font color="#008000" size="2 // Reason </font><font size="2 pszName, </font><font color="#008000" size="2 // User name </font><font size="2 CREDUI_MAX_USERNAME_LENGTH+1, </font><font color="#008000" size="2 // Max number for user name </font><font size="2 pszPwd, </font><font color="#008000" size="2 // Password </font><font size="2 CREDUI_MAX_PASSWORD_LENGTH+1, </font><font color="#008000" size="2 // Max number for password </font><font size="2 &fSave, </font><font color="#008000" size="2 // State of save check box </font><font size="2 CREDUI_FLAGS_GENERIC_CREDENTIALS | </font><font color="#008000" size="2 // flags </font><font size="2 CREDUI_FLAGS_ALWAYS_SHOW_UI | CREDUI_FLAGS_DO_NOT_PERSIST); </font><font color="#0000ff" size="2 if</font><font size="2 (dwErr) { cout << </font><font color="#800000" size="2 "Did not get credentials."</font><font size="2 << endl; pLoc->Release(); CoUninitialize(); </font><font color="#0000ff" size="2 return</font><font size="2 1; } </font><font color="#008000" size="2 // Connect to the remote rootcimv2 namespace </font><font size="2 </font><font color="#008000" size="2 // and obtain pointer pSvc to make IWbemServices calls. </font><font size="2 </font><font color="#008000" size="2 //--------------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // change the computerName and domain </font><font size="2 </font><font color="#008000" size="2 // strings below to the full computer name and domain </font><font size="2 </font><font color="#008000" size="2 // of the remote computer </font><font size="2 hres = pLoc->ConnectServer( _bstr_t(L</font><font color="#800000" size="2 "\\computerName\root\cimv2"</font><font size="2 ), _bstr_t(pszName), </font><font color="#008000" size="2 // User name </font><font size="2 _bstr_t(pszPwd), </font><font color="#008000" size="2 // User password </font><font size="2 _bstr_t(L</font><font color="#800000" size="2 "MS_409"</font><font size="2 ), </font><font color="#008000" size="2 // Locale </font><font size="2 NULL, </font><font color="#008000" size="2 // Security flags </font><font size="2 _bstr_t(L</font><font color="#800000" size="2 "ntlmdomain:domain"</font><font size="2 ), </font><font color="#008000" size="2 // Authority </font><font size="2 0, </font><font color="#008000" size="2 // Context object </font><font size="2 &pSvc </font><font color="#008000" size="2 // IWbemServices proxy </font><font size="2 ); </font><font color="#008000" size="2 // When you have finished using the credentials, </font><font size="2 </font><font color="#008000" size="2 // erase them from memory. </font><font size="2 SecureZeroMemory(pszName, </font><font color="#0000ff" size="2 sizeof</font><font size="2 (pszName)); SecureZeroMemory(pszPwd, </font><font color="#0000ff" size="2 sizeof</font><font size="2 (pszPwd)); </font><font color="#0000ff" size="2 if</font><font size="2 (FAILED(hres)) { cout << </font><font color="#800000" size="2 "Could not connect. Error code = 0x"</font><font size="2 << hex << hres << endl; pLoc->Release(); CoUninitialize(); </font><font color="#0000ff" size="2 return</font><font size="2 1; </font><font color="#008000" size="2 // Program has failed. </font><font size="2 } cout << </font><font color="#800000" size="2 "Connected to ROOT\CIMV2 WMI namespace"</font><font size="2 << endl; </font><font color="#008000" size="2 // Step 5: -------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // Set security levels on a WMI connection ------------------ </font><font size="2 hres = CoSetProxyBlanket( pSvc, </font><font color="#008000" size="2 // Indicates the proxy to set </font><font size="2 RPC_C_AUTHN_WINNT, </font><font color="#008000" size="2 // RPC_C_AUTHN_xxx </font><font size="2 RPC_C_AUTHZ_NONE, </font><font color="#008000" size="2 // RPC_C_AUTHZ_xxx </font><font size="2 NULL, </font><font color="#008000" size="2 // Server principal name </font><font size="2 RPC_C_AUTHN_LEVEL_CALL, </font><font color="#008000" size="2 // RPC_C_AUTHN_LEVEL_xxx </font><font size="2 RPC_C_IMP_LEVEL_IMPERSONATE, </font><font color="#008000" size="2 // RPC_C_IMP_LEVEL_xxx </font><font size="2 NULL, </font><font color="#008000" size="2 // client identity </font><font size="2 EOAC_NONE </font><font color="#008000" size="2 // proxy capabilities </font><font size="2 ); </font><font color="#0000ff" size="2 if</font><font size="2 (FAILED(hres)) { cout << </font><font color="#800000" size="2 "Could not set proxy blanket. Error code = 0x"</font><font size="2 << hex << hres << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); </font><font color="#0000ff" size="2 return</font><font size="2 1; </font><font color="#008000" size="2 // Program has failed. </font><font size="2 } </font><font color="#008000" size="2 // Step 6: -------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // Use the IWbemServices pointer to make requests of WMI ---- </font><font size="2 </font><font color="#008000" size="2 // For example, get the name of the operating system </font><font size="2 IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery( bstr_t(</font><font color="#800000" size="2 "WQL"</font><font size="2 ), bstr_t(</font><font color="#800000" size="2 "Select * from Win32_OperatingSystem"</font><font size="2 ), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); </font><font color="#0000ff" size="2 if</font><font size="2 (FAILED(hres)) { cout << </font><font color="#800000" size="2 "Query for operating system name failed." </font><font size="2 << </font><font color="#800000" size="2 " Error code = 0x"</font><font size="2 << hex << hres << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); </font><font color="#0000ff" size="2 return</font><font size="2 1; </font><font color="#008000" size="2 // Program has failed. </font><font size="2 } </font><font color="#008000" size="2 // Step 7: ------------------------------------------------- </font><font size="2 </font><font color="#008000" size="2 // Get the data from the query in step 6 ------------------- </font><font size="2 IWbemClassObject *pclsObj; ULONG uReturn = 0; </font><font color="#0000ff" size="2 while</font><font size="2 (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); </font><font color="#0000ff" size="2 if</font><font size="2 (0 == uReturn) { </font><font color="#0000ff" size="2 break</font><font size="2 ; } VARIANT vtProp; VariantInit(&vtProp); </font><font color="#008000" size="2 // Get the value of the Name property </font><font size="2 hr = pclsObj->Get(L</font><font color="#800000" size="2 "Name"</font><font size="2 , 0, &vtProp, 0, 0); wcout << </font><font color="#800000" size="2 " OS Name : "</font><font size="2 << vtProp.bstrVal << endl; </font><font color="#008000" size="2 // Get the value of the FreePhysicalMemory property </font><font size="2 hr = pclsObj->Get(L</font><font color="#800000" size="2 "FreePhysicalMemory"</font><font size="2 , 0, &vtProp, 0, 0); wcout << </font><font color="#800000" size="2 " Free physical memory (in kilobytes): " </font><font size="2 << vtProp.uintVal << endl; VariantClear(&vtProp); } </font><font color="#008000" size="2 // Cleanup </font><font size="2 </font><font color="#008000" size="2 // ======== </font><font size="2 pSvc->Release(); pLoc->Release(); pEnumerator->Release(); pclsObj->Release(); CoUninitialize(); </font><font color="#0000ff" size="2 return</font><font size="2 0; </font><font color="#008000" size="2 // Program successfully completed. </font><font size="2 } </font></font>

View the full article
 
Back
Top