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

I downloaded Visual C++ 2005 Express Edition Beta and I'm 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

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.  Here's the output:

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 *)" ( 0Data_t@_bstr_t@@QAE@PBD@Z)

Does anybody know how to solve this   Many thanks in advance.  I'd 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:

#define _WIN32_DCOM

#include "stdafx.h"

#include <iostream>

using namespace std;

#include <comdef.h>

#include <Wbemidl.h>

# pragma comment(lib, "wbemuuid.lib")

# pragma comment(lib, "credui.lib")

#include <wincred.h>

#include "ObjBase.h"

int main(int argc, char **argv)

{

HRESULT hres;

// Step 1: --------------------------------------------------

// Initialize COM. ------------------------------------------

hres = CoInitializeEx(0, COINIT_MULTITHREADED);

if (FAILED(hres))

{

cout << "Failed to initialize COM library. Error code = 0x"

<< hex << hres << endl;

return 1; // Program has failed.

}

// Step 2: --------------------------------------------------

// Set general COM security levels --------------------------

// Note: If you are using Windows 2000, you need to specify -

// the default authentication credentials for a user by using

// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----

// parameter of CoInitializeSecurity ------------------------

hres = CoInitializeSecurity(

NULL,

-1, // COM authentication

NULL, // Authentication services

NULL, // Reserved

RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication

RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation

NULL, // Authentication info

EOAC_NONE, // Additional capabilities

NULL // Reserved

);

if (FAILED(hres))

{

cout << "Failed to initialize security. Error code = 0x"

<< hex << hres << endl;

CoUninitialize();

return 1; // Program has failed.

}

// Step 3: ---------------------------------------------------

// Obtain the initial locator to WMI -------------------------

IWbemLocator *pLoc = NULL;

hres = CoCreateInstance(

CLSID_WbemLocator,

0,

CLSCTX_INPROC_SERVER,

IID_IWbemLocator, (LPVOID *) &pLoc);

if (FAILED(hres))

{

cout << "Failed to create IWbemLocator object."

<< " Err code = 0x"

<< hex << hres << endl;

CoUninitialize();

return 1; // Program has failed.

}

// Step 4: -----------------------------------------------------

// Connect to WMI through the IWbemLocator::ConnectServer method

IWbemServices *pSvc = NULL;

// Get the user name and password for the remote computer

CREDUI_INFO cui;

TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH+1];

TCHAR pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1];

BOOL fSave;

DWORD dwErr;

cui.cbSize = sizeof(CREDUI_INFO);

cui.hwndParent = NULL;

// Ensure that MessageText and CaptionText identify

// what credentials to use and which application requires them.

cui.pszMessageText = TEXT("Remote computer account information");

cui.pszCaptionText = TEXT("Enter Account Information");

cui.hbmBanner = NULL;

fSave = FALSE;

dwErr = CredUIPromptForCredentials(

&cui, // CREDUI_INFO structure

TEXT(""), // Target for credentials

NULL, // Reserved

0, // Reason

pszName, // User name

CREDUI_MAX_USERNAME_LENGTH+1, // Max number for user name

pszPwd, // Password

CREDUI_MAX_PASSWORD_LENGTH+1, // Max number for password

&fSave, // State of save check box

CREDUI_FLAGS_GENERIC_CREDENTIALS | // flags

CREDUI_FLAGS_ALWAYS_SHOW_UI |

CREDUI_FLAGS_DO_NOT_PERSIST);

if(dwErr)

{

cout << "Did not get credentials." << endl;

pLoc->Release();

CoUninitialize();

return 1;

}

// Connect to the remote root\cimv2 namespace

// and obtain pointer pSvc to make IWbemServices calls.

//---------------------------------------------------------

// change the computerName and domain

// strings below to the full computer name and domain

// of the remote computer

hres = pLoc->ConnectServer(

_bstr_t(L"\\\\computerName\\root\\cimv2"),

_bstr_t(pszName), // User name

_bstr_t(pszPwd), // User password

_bstr_t(L"MS_409"), // Locale

NULL, // Security flags

_bstr_t(L"ntlmdomain:domain"), // Authority

0, // Context object

&pSvc // IWbemServices proxy

);

// When you have finished using the credentials,

// erase them from memory.

SecureZeroMemory(pszName, sizeof(pszName));

SecureZeroMemory(pszPwd, sizeof(pszPwd));

if (FAILED(hres))

{

cout << "Could not connect. Error code = 0x"

<< hex << hres << endl;

pLoc->Release();

CoUninitialize();

return 1; // Program has failed.

}

cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;

 

// Step 5: --------------------------------------------------

// Set security levels on a WMI connection ------------------

hres = CoSetProxyBlanket(

pSvc, // Indicates the proxy to set

RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx

RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx

NULL, // Server principal name

RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx

RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx

NULL, // client identity

EOAC_NONE // proxy capabilities

);

if (FAILED(hres))

{

cout << "Could not set proxy blanket. Error code = 0x"

<< hex << hres << endl;

pSvc->Release();

pLoc->Release();

CoUninitialize();

return 1; // Program has failed.

}

// Step 6: --------------------------------------------------

// Use the IWbemServices pointer to make requests of WMI ----

// For example, get the name of the operating system

IEnumWbemClassObject* pEnumerator = NULL;

hres = pSvc->ExecQuery(

bstr_t("WQL"),

bstr_t("Select * from Win32_OperatingSystem"),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);

if (FAILED(hres))

{

cout << "Query for operating system name failed."

<< " Error code = 0x"

<< hex << hres << endl;

pSvc->Release();

pLoc->Release();

CoUninitialize();

return 1; // Program has failed.

}

// Step 7: -------------------------------------------------

// Get the data from the query in step 6 -------------------

IWbemClassObject *pclsObj;

ULONG uReturn = 0;

while (pEnumerator)

{

HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,

&pclsObj, &uReturn);

if(0 == uReturn)

{

break;

}

VARIANT vtProp;

VariantInit(&vtProp);

// Get the value of the Name property

hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);

wcout << " OS Name : " << vtProp.bstrVal << endl;

// Get the value of the FreePhysicalMemory property

hr = pclsObj->Get(L"FreePhysicalMemory",

0, &vtProp, 0, 0);

wcout << " Free physical memory (in kilobytes): "

<< vtProp.uintVal << endl;

VariantClear(&vtProp);

}

// Cleanup

// ========

pSvc->Release();

pLoc->Release();

pEnumerator->Release();

pclsObj->Release();

CoUninitialize();

return 0; // Program successfully completed.

}



Answer this question

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

  • Emrys Mydhrin

     

    try this :

    bstr_t(L"WQL"),

    bstr_t(L"Select * from Win32_OperatingSystem"),

    in place of :

    bstr_t("WQL"),

    bstr_t("Select * from Win32_OperatingSystem"),

    in the ExecQuery function.

    It made it for me in another example.

    regards

     

     


  • Marco Villagrana

    That's the first thing I looked at.  I added the directory that those libraries are in to my "Additional Library Directories" in Project->Properties->Linker->General, and it still doesn't seem to find them.  I'm assuming I don't have some property set right, but can't figure it out.


  • gudel

    Another thing to check is that you haven't got a wchar_t/unsigned short mismatch. With Visual C++ 2005 the default is for wchar_t to be a built-in type instead of a typedef for unsigned short.

    This doesn't look like the problem in the first case but in the second case you might want to link against comsuppw\[d\].lib instead of commsp\[d\].lib.

    Another alternative is to revert back to wchar_t being a typedef for unsigned short: the /Zc:wchar_t- will do this.



  • chuchi

    I think you need to check with the order in which you include directory on Include/Lib

    change the order of WBEM SDK include files folder to 1st and same for Lib.


    It should work.


    Regards,
    Nitin

  • serras

     

    i am also find same problem .

    1) not able to open winrced.h and file put into statarting after find  below error

    any one solve this problem . plz inform

     

    e:\code\wmiremote\wmilocal.cpp(150): error C2065: 'CREDUI_INFO' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(150): error C2146: syntax error : missing ';' before identifier 'cui'
    e:\code\wmiremote\wmilocal.cpp(150): error C2065: 'cui' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(151): error C2065: 'CREDUI_MAX_USERNAME_LENGTH' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(151): error C2057: expected constant expression
    e:\code\wmiremote\wmilocal.cpp(151): error C2466: cannot allocate an array of constant size 0
    e:\code\wmiremote\wmilocal.cpp(151): error C2133: 'pszName' : unknown size
    e:\code\wmiremote\wmilocal.cpp(152): error C2065: 'CREDUI_MAX_PASSWORD_LENGTH' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(152): error C2057: expected constant expression
    e:\code\wmiremote\wmilocal.cpp(152): error C2466: cannot allocate an array of constant size 0
    e:\code\wmiremote\wmilocal.cpp(152): error C2133: 'pszPwd' : unknown size
    e:\code\wmiremote\wmilocal.cpp(156): error C2228: left of '.cbSize' must have class/struct/union type
    e:\code\wmiremote\wmilocal.cpp(156): error C2070: ''unknown-type'': illegal sizeof operand
    e:\code\wmiremote\wmilocal.cpp(157): error C2228: left of '.hwndParent' must have class/struct/union type
    e:\code\wmiremote\wmilocal.cpp(160): error C2228: left of '.pszMessageText' must have class/struct/union type
    e:\code\wmiremote\wmilocal.cpp(161): error C2228: left of '.pszCaptionText' must have class/struct/union type
    e:\code\wmiremote\wmilocal.cpp(162): error C2228: left of '.hbmBanner' must have class/struct/union type
    e:\code\wmiremote\wmilocal.cpp(165): error C2065: 'CredUIPromptForCredentials' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(175): error C2065: 'CREDUI_FLAGS_GENERIC_CREDENTIALS' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(175): error C2065: 'CREDUI_FLAGS_ALWAYS_SHOW_UI' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(175): error C2296: '|' : illegal, left operand has type ''unknown-type''
    e:\code\wmiremote\wmilocal.cpp(175): error C2297: '|' : illegal, right operand has type ''unknown-type''
    e:\code\wmiremote\wmilocal.cpp(175): error C2065: 'CREDUI_FLAGS_DO_NOT_PERSIST' : undeclared identifier
    e:\code\wmiremote\wmilocal.cpp(175): error C2440: '=' : cannot convert from ''unknown-type'' to 'DWORD'

     

     

    regards,

    ramalingom


  • Hsien-Chun

    It looks like you are missing a couple of libraries at the link step:

    msvcprtDrinks.lib
    comsupp[wd].lib

  • n3sachde

    Same problem here.  Looks like no ones figured it out.
  • Jagjot

    I have been receiving the exact same error. 

    (Compiles OK, Linker chokes)

    Has anyone figured-out a resolution

    Thanks in advance!


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