CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2008
    Posts
    8

    fail to get information from remote machine using WMI, vc 6

    Hi,everyone!

    i tried to get information from remote machine using WMI by vc6,
    i can connect with remote machine ,but i fail to get the infomation,
    and then i tried wbemtest tool, there is no problem between the two computers,

    here is my code ,Any suggestion will be really appreciated!
    Code:
    #define _WIN32_DCOM
    #include <iostream>
    #include <comdef.h>
    #include <Wbemidl.h>
    # pragma comment(lib, "wbemuuid.lib")
    #include   <atlbase.h>
    #include <windows.h>
    #include <wincred.h>
    # pragma comment(lib, "credui.lib")
    using namespace std;
    
    
    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]="user";
    TCHAR pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1]="pass";
    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("\\10.7.24.83"), // 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"\\\\10.10.13.48\\root\\cimv2"),
    _bstr_t("user"), // User name
    _bstr_t("pass"), // 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 ------------------
    
    SEC_WINNT_AUTH_IDENTITY_W* pAuthIdentity =
    new SEC_WINNT_AUTH_IDENTITY_W;
    ZeroMemory(pAuthIdentity, sizeof(SEC_WINNT_AUTH_IDENTITY_W));
    
    
    pAuthIdentity->User = new WCHAR[32];
    wcscpy(pAuthIdentity->User , L"user" );
    pAuthIdentity->UserLength = wcslen(pAuthIdentity->User);
    
    
    pAuthIdentity->Domain = new WCHAR[32];
    wcscpy(pAuthIdentity->Domain, L"\\10.10.13.48");
    pAuthIdentity->DomainLength = wcslen( pAuthIdentity->Domain);
    
    
    pAuthIdentity->Password = new WCHAR[32];
    wcscpy(pAuthIdentity->Password, L"pass" );
    pAuthIdentity->PasswordLength = wcslen( pAuthIdentity->Password);
    
    
    pAuthIdentity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
    
    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
    pAuthIdentity, // 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.ad
    }
    
    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------
    
    IWbemClassObject *pclsObj=NULL;
    ULONG uReturn = 0;
    
    while (pEnumerator)
    {
    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
    &pclsObj, &uReturn);
    
    if(0 == uReturn) //But uReturn =0 all the time!
    {
    break; // break every time!
    }
    
    VARIANT 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(); //often return "access deny"
    Sleep(1000);
    CoUninitialize();
    
    return 0; // Program successfully completed.
    
    }
    Last edited by TechCrazy; December 21st, 2008 at 09:05 PM.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: fail to get information from remote machine using WMI, vc 6

    Please go back and re-read the "Before you Post" announcement. It will show you how to properly post your code so that it is actually readable! You can then EDIT your existing post.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Dec 2008
    Posts
    8

    Re: fail to get information from remote machine using WMI, vc 6

    it was my fault, now i have modified it ~
    thank you all the same.

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: fail to get information from remote machine using WMI, vc 6

    Quote Originally Posted by TechCrazy View Post
    it was my fault, now i have modified it ~
    thank you all the same.
    No you made it worse...now you are expecting people (who are volunteeering their time..) to spend time downloading and opening a file.

    You should:

    a) Use Code Tags
    b) Enable Private messaging [so things like this do not cause permanent clutter to the threade.

    Please do not skim, or browse the FAQ's READ them CAREFULLY! Spend time practicing posting in the forum that is specifically set up for that.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Dec 2008
    Posts
    8

    Re: fail to get information from remote machine using WMI, vc 6

    Quote Originally Posted by TheCPUWizard View Post
    No you made it worse...now you are expecting people (who are volunteeering their time..) to spend time downloading and opening a file.

    You should:

    a) Use Code Tags
    b) Enable Private messaging [so things like this do not cause permanent clutter to the threade.

    Please do not skim, or browse the FAQ's READ them CAREFULLY! Spend time practicing posting in the forum that is specifically set up for that.
    i think it's ok this time , i am new here, thank you,
    but what exactly i need is the suggestion to my question~Anyone can help me ?

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: fail to get information from remote machine using WMI, vc 6

    Now that is it (almost) readable....

    Which call is failing; and what is the error message. I am semi suspicious about the ConnectServer call and the fact that you are not using a standard DQN..... but you have not provided sfficient information.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Dec 2008
    Posts
    8

    Re: fail to get information from remote machine using WMI, vc 6

    thank you for your reply!
    just "connect ..." was put out,everything seemed to be fine.
    the code i wrote is a sample from msdn ,but it didn't work fine.
    but after debug ,i found:

    if(0 == uReturn) //uReturn =0 all the time!
    {
    break; // break every time!
    }
    ....
    pclsObj->Release(); //often return "access deny"

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: fail to get information from remote machine using WMI, vc 6

    Code:
    pSvc->ExecQuery(
    is returning S_OK....and...
    Code:
    pEnumerator ///is or is not null...
    RIGHT AFTER THE CALL....
    Check this with the debugger (SINGLE STEP EACH LINE)...NOT diagnostic type printing. Examine the state of all variables as each system call occurs, to make sure there are no unexpected variable changes.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Dec 2008
    Posts
    8

    Re: fail to get information from remote machine using WMI, vc 6

    yes,i used the single step each line debugger
    Code:
     pSvc->ExecQuery
    return S_OK indeed,

    and

    Code:
     pEnumerator
    is not null, but i don't know what value it should be,

    Code:
     HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
    
    &pclsObj, &uReturn);
    if(0 == uReturn)
    
    {
    
    break;
    
    }
    after the call,uReturn returns 0 all the time,and "break" the "while" every time.
    i don't know why uReturn always return 0.

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: fail to get information from remote machine using WMI, vc 6

    Interesting you seem to be getting an enumerator (ie it is not null), but the very first call to Next returns a null. This indicates that there are no resources that you have permission to view...... Are you sure you usernames and passwords are correct????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Dec 2008
    Posts
    8

    Re: fail to get information from remote machine using WMI, vc 6

    i am sure username and password are correct,
    and i used wbemtest with the same username , password, everything went fine(connect, query)

  12. #12
    Join Date
    Dec 2008
    Posts
    8

    Re: fail to get information from remote machine using WMI, vc 6

    Code:
    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
    
    &pclsObj, &uReturn);
    
    if(FAILED(hr)){....}
    i found hr returned failed.....
    why did it happen?

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: fail to get information from remote machine using WMI, vc 6

    Quote Originally Posted by TechCrazy View Post
    Code:
    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
    
    &pclsObj, &uReturn);
    
    if(FAILED(hr)){....}
    i found hr returned failed.....
    why did it happen?
    So you weant me to GUESS what the error code returned was.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    Dec 2008
    Posts
    8

    Re: fail to get information from remote machine using WMI, vc 6

    right now ,i created a account in target machine ,the same username and password with my machine, and the code worked !!!!
    but why?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured