Computing.Net > Forums > OpenVMS > WMI Script Crahses the Windows 200

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

WMI Script Crahses the Windows 200

Reply to Message Icon

Name: Rammohan Alampally
Date: November 17, 2006 at 07:34:15 Pacific
OS: Windows 2003
CPU/Ram: Pentium-III 868MHZ / 1GB
Product: Compaq
Comment:

// test1.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#define _WIN32_DCOM
#include <comdef.h>
#include <comutil.h>
#include <Wbemidl.h>
#include <io.h>
#include <vector>
#include <string>
using namespace std;
#include <ios>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <exception>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

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

int main(int argc, char *argv[])
{
printf("Starting the Main ..... \n\n");
std::string serialNumber;
HRESULT hres;
serialNumber="";

/--
// Initialize ------------------
printf("Begining of the Step#1 \n\n");

CoUninitialize();
hres = CoInitializeEx(0, COINIT_MULTITHREADED);

if (FAILED(hres))
{
//Use SM log here
printf("Failed to initialize COM library \n\n");
//std::cout << "Failed to initialize COM library. Error code = 0x"
// << hex << hres << endl;
printf("serialNumber == %s \n",serialNumber.c_str()); // Program has failed.
}

/--
// Set general COM security le--
// 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 CoInitializeSecu
printf("Begining of the Step#2 \n\n");
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))
{
printf("Failed to initialize security. \n\n");
//std::cout << "Failed to initialize security. Error code = 0x"
// << hex << hres << endl;
printf("Begining of the CoUninitialize() function Call \n\n");;
CoUninitialize();
printf("End of the CoUninitialize() function Call \n\n");;

printf("The serialNumber after Step-II funtion finished is == %s",serialNumber.c_str());

}
printf("Begining of the Step#3 \n\n");
/---
// Obtain the initial locator to-

IWbemLocator *pLoc = NULL;

hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);

if (FAILED(hres))
{
printf("Failed to create IWbemLocator object. \n\n");
std::cout << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << endl;

printf("Begining of the CoUninitialize() function Call \n\n");
CoUninitialize();
printf("End of the CoUninitialize() function Call \n\n");


printf("The serialNumber after Intial Locator WMI funtion finished is == %s",serialNumber.c_str());
}

// Ste
// Connect to WMI through the IWbemLocator::ConnectServer method
printf("Begining of the Step#4 \n\n");
IWbemServices *pSvc = NULL;

// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
printf("To make IWbemServices calls");
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);

if (FAILED(hres))
{
printf("Filaed to connect WMI Namespace \n\n");
printf("Could not connect to WMI namespace.\n ");
printf("WMI Namespace is not connected.Error Code is hex=%s and hres=%s",hex,hres);
pLoc->Release();
printf("Before CoUninitialize() function \n\n");
CoUninitialize();
printf("After CoUninitialize() function \n\n");
printf("The serialNumber in IWbemLocator funtion finished in WMI Namespace is == %s",serialNumber.c_str());
}

printf("Connected to ROOT\\CIMV2 WMI namespace \n\n");
printf("Begining of the Step#5 \n\n");
/--
// Set security levels on the p-
printf("Setting the Security level on the Proxy \n\n");
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))
{
printf("Failed to setting the Security Proxy Blanket \n\n");;
printf("Could not set proxy blanket. \n\n");
printf("Proxy Blanket is not set hex=%s and hres=%s",hex,hres);
pSvc->Release();
pLoc->Release();

printf("Before CoUninitialize() function \n\n");
CoUninitialize();
printf("After CoUninitialize() function \n\n");
printf("The serialNumber in Security Level Proxy function finished in WMI Namespace is == %s",serialNumber.c_str());
}

/--
// Use the IWbemServices pointer to make requests of WMI ----
printf("Begining of the Step#6 \n\n");
// For example, get the name of the operating system
printf("Getting the Name of the O/S \n\n");
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_BIOS"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
printf("Query for BIOS property, Serial Number failed.\n ");
printf("Failed to Get the serial number form the BIOS \n\n");
printf("O/S Name got failed hex=%s and hres=%s",hex,hres);
pSvc->Release();
pLoc->Release();

printf("Before CoUninitialize() function \n\n");
CoUninitialize();
printf("After CoUninitialize() function \n\n");
printf("The serialNumber in Finding OS funtion finished in WMI Namespace is == %s",serialNumber.c_str());
}

/-
// Get the data from the query in step 6 -------------------
printf("Begining of the Step#7 \n\n");
printf("Creating the IWbemClassObject \n\n");
IWbemClassObject *pclsObj;
printf("Assigning uReturn value to zero \n\n");
ULONG uReturn = 0;
printf("Created the IWbemClassObject \n\n");

while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn);

if(0 == uReturn)
{
break;
}

printf("Creating the VARIANT pointer vtProp \n\n");
VARIANT vtProp;
VariantInit(&vtProp);

// Get the value of the Name property
printf("Getting the Serial Number \n\n");
printf("Getting the Serial Number into hr \n\n");
hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
//wcout << " SN : " << vtProp.bstrVal << endl;
//char *c1 = new char[50];
//strcpy(c1,_bstr_t(vtProp).operator const char *());
//std::cout << " SNum c1: " << c1 << endl;
//std::cout << " SNum str: " << mystr.c_str() << endl;
printf(" \n\n");
printf(" \n\n");
printf("After getting the hr value \n\n");
printf(" \n\n");
//original
//serialNumber = _bstr_t(vtProp).operator const char *();

//From here modified for chekcing the errors

printf(" Assigning the Serial Number from _bstr_t function \n");
serialNumber = _bstr_t(vtProp);
printf("\n Just Before to printing the Serial Number \n\n");

//Till here modified for chekcing the errors
printf("WMI returned Serial Number: %s",serialNumber.c_str());
VariantClear(&vtProp);
printf("\n");
printf("Varient Pointer is cleared \n\n");
}

// Cleanup
// ========
printf("Begining Cleanup \n\n");
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
printf("Before CoUninitialize() function \n\n");
CoUninitialize();
printf("After CoUninitialize() function \n\n");
printf("The serialNumber after CoUninitialize() funtion finished in WMI Namespace is == %s",serialNumber.c_str());
printf("Ending of the Cleanup \n\n");
return 0;
}
____________________________________________

If I ran this code on my Windows2003 standard edition with service pack-1 crashes and I am getting the output is follows:

Starting the Main .....

Begining of the Step#1

Begining of the Step#2

Begining of the Step#3

Begining of the Step#4

To make IWbemServices callsConnected to ROOT\CIMV2 WMI namespace

Begining of the Step#5

Setting the Security level on the Proxy

Begining of the Step#6

Getting the Name of the O/S

Begining of the Step#7

Creating the IWbemClassObject

Assigning uReturn value to zero

Created the IWbemClassObject

Creating the VARIANT pointer vtProp

Getting the Serial Number

Getting the Serial Number into hr

After getting the hr value

This application has requested the Runtime to terminate it in an unusual way.

Please contact the application's support team for more information.
____________________________________________

The same above code is working in other all windows o/s except on windows 2003.
Some one also got the same problme while using _bstr_ function and he got the problme because the system sends the serial number is ulong.Thats what the system is crashed...

Please send me any idea or workaround to my id.

rammohan@india.com

Tahnks in advance...


Thanks in advance...
Rammohan Alampally,
rammohan@india.com



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: WMI Script Crahses the Windows 200

FTP file from OpenVMS to Windows www.computing.net/answers/openvms/ftp-file-from-openvms-to-windows/439.html

Error Creating Space www.computing.net/answers/openvms/error-creating-space/146.html

OpenVMS and Tomcat www.computing.net/answers/openvms/openvms-and-tomcat/332.html