WMI remote install

Joined
Feb 6, 2006
Messages
1
Reaction score
0
I have written a program in C++ for installing to a remote computer, but it fails without an error message. I can connect to the remote computer and get information about it using WQL, so I know that the connection works. The source code is below. Can anybody see what the error would be? Alternatively, could anybody tell me the vbscript equivalent of this code?

#define _WIN32_DCOM
#include
using namespace std;
#include
#include
# pragma comment(lib, "wbemuuid.lib")
# pragma comment(lib, "credui.lib")
#include

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;
system("PAUSE");
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();
system("PAUSE");
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();
system("PAUSE");
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();
system("PAUSE");
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
/* Win 2000 */
hres = pLoc->ConnectServer(
_bstr_t(L"\\\\remotemachine\\root\\cimv2"),
_bstr_t(pszName), // User name
_bstr_t(pszPwd), // User password
NULL, // Locale
NULL, // Security flags
_bstr_t(L"ntlmdomain:domainname"), // 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();
system("PAUSE");
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();
system("PAUSE");
return 1; // Program has failed.
}

//Initialise Win32_Product Class
// Set up to call the Win32_Product::Install method
BSTR MethodName = SysAllocString(L"Install");
BSTR ClassName = SysAllocString(L"Win32_Product");

IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);

IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
//Set the input parameters of the Install method
BSTR packageLoc = L"\\\\servername\\share\\EmptyInstaller1.msi";
// BSTR packageLoc = L"C:\\VCPPProjects\\Visual Studio Installer Projects\\EmptyInstaller2\\Output\\DISK_1\\EmptyInstaller2.msi";

VARIANT varCommand;
varCommand.vt = VT_BSTR;
varCommand.bstrVal = packageLoc;
//Store the package location value
hres = pClassInstance->Put(L"PackageLocation",0,&varCommand,0);

//Set the AllUsers parameter
VARIANT varCommand2;
varCommand2.boolVal = true;
//Store the AllUsers value
hres = pClassInstance->Put(L"AllUsers",0,&varCommand2,0);
// Set target location parameter - not used for Install
/* BSTR targetLoc = L"C:\\Program Files";
VARIANT varCommand2;
varCommand2.vt = VT_BSTR;
varCommand2.bstrVal = targetLoc;
hres = pClassInstance->Put(L"TargetLocation",0,&varCommand2,0);*/
//Execute the method
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
cout<<"ExecMethod called"<
wcout<<
wcout<<
if (FAILED(hres))
{
cout << "Could not execute method. Error code = 0x"
<< hex << hres << endl;
VariantClear(&varCommand);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pInParamsDefinition->Release();
pOutParams->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}


// Cleanup
// ========

pSvc->Release();
pLoc->Release();
// pEnumerator->Release();
// pclsObj->Release();
CoUninitialize();
system("PAUSE");
return 0; // Program successfully completed.

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top