vassim
asked on
char to BSTR
How can I read a string from a text file and then store it as a BSTR
so far I have:
************************** ********** ********** *******
char javaMcName[20];
ifstream input("machineDetails.dat" );
if (input.fail()){
printf("Cannot open machinDetails.dat");
exit(1);
}
input.getline(javaMcName, 20, '\n');
BSTR McName = SysAllocString((OLECHAR*)j avaMcName) ;
************************** ********** ********** *******
but I think this is giving me and exception when I call the method GetObject
I use BSTR McName to create IWbemLocator & IWbemServices pointers (WmiWrapper)
and then I call the GetObject method like so:
************************** ********** ********** *******
IWbemClassObject **pInstance
BSTR bstrKey1;
hRes = IWbemSer->GetObject(bstrKe y1, 0, NULL, pInstance, NULL);
************************** ********** ********** *******
This is the exception it keeps giving me:
http://www.81x.com/Authors/roadragedave/exception2.jpg
It might be something stupid, but it is very urgent, strings were never my strong point.
so far I have:
**************************
char javaMcName[20];
ifstream input("machineDetails.dat"
if (input.fail()){
printf("Cannot open machinDetails.dat");
exit(1);
}
input.getline(javaMcName, 20, '\n');
BSTR McName = SysAllocString((OLECHAR*)j
**************************
but I think this is giving me and exception when I call the method GetObject
I use BSTR McName to create IWbemLocator & IWbemServices pointers (WmiWrapper)
and then I call the GetObject method like so:
**************************
IWbemClassObject **pInstance
BSTR bstrKey1;
hRes = IWbemSer->GetObject(bstrKe
**************************
This is the exception it keeps giving me:
http://www.81x.com/Authors/roadragedave/exception2.jpg
It might be something stupid, but it is very urgent, strings were never my strong point.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Mabey it isnt the casting of the string thats the problem so,
because I still get the exception in both cases above.
However if I call the WmiWrapper constructor with NULL BSTR values (parameters are the ip address of a machine, Username & password) then it works fine because NULL parameters are defaulted to the local machine.
This is the constructor:
************************** ********** ********** ********** ***
WmiWrapper::WmiWrapper(BST R bstrMachine, BSTR bstrUName, BSTR bstrPWord)
{
HRESULT hRes = S_OK;
TCHAR sz[MAX_PATH];
IWbemLoc = NULL;
IWbemSer = NULL;
// Initialize the COM security context.
CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONAT E, NULL,
0x0, 0);
// Get the WMI locator pointer.
hRes = CoCreateInstance(CLSID_Wbe mLocator, NULL,
CLSCTX_INPROC_SERVER,IID_I WbemLocato r,
(LPVOID *)&IWbemLoc);
if (FAILED(hRes))
return;
// Using the locator, connect to CIMOM in the given namespace.
if (bstrMachine == NULL)
{
TCHAR localName[MAX_PATH];
ULONG ulSize = MAX_PATH;
GetComputerName(localName, &ulSize);
wsprintf(sz, TEXT("\\\\%s\\root\\cimv2" ), localName);
}
else
wsprintf(sz, TEXT("\\\\%s\\root\\cimv2" ), bstrMachine);
BSTR bstrNamespace = SysAllocString(sz);
hRes = IWbemLoc->ConnectServer(bs trNamespac e, bstrUName, bstrPWord,
0L, 0L, NULL, NULL, &IWbemSer);
SysFreeString(bstrNamespac e);
SysFreeString(bstrUName);
SysFreeString(bstrPWord);
if (FAILED(hRes))
{
IWbemLoc->Release();
IWbemLoc = NULL;
return;
}
printf("Success");
return;
}
************************** ********** ********** ********** *********
Then I use the IWbemServices pointer to call the GetObject method
because I still get the exception in both cases above.
However if I call the WmiWrapper constructor with NULL BSTR values (parameters are the ip address of a machine, Username & password) then it works fine because NULL parameters are defaulted to the local machine.
This is the constructor:
**************************
WmiWrapper::WmiWrapper(BST
{
HRESULT hRes = S_OK;
TCHAR sz[MAX_PATH];
IWbemLoc = NULL;
IWbemSer = NULL;
// Initialize the COM security context.
CoInitializeSecurity(NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONAT
0x0, 0);
// Get the WMI locator pointer.
hRes = CoCreateInstance(CLSID_Wbe
CLSCTX_INPROC_SERVER,IID_I
(LPVOID *)&IWbemLoc);
if (FAILED(hRes))
return;
// Using the locator, connect to CIMOM in the given namespace.
if (bstrMachine == NULL)
{
TCHAR localName[MAX_PATH];
ULONG ulSize = MAX_PATH;
GetComputerName(localName,
wsprintf(sz, TEXT("\\\\%s\\root\\cimv2"
}
else
wsprintf(sz, TEXT("\\\\%s\\root\\cimv2"
BSTR bstrNamespace = SysAllocString(sz);
hRes = IWbemLoc->ConnectServer(bs
0L, 0L, NULL, NULL, &IWbemSer);
SysFreeString(bstrNamespac
SysFreeString(bstrUName);
SysFreeString(bstrPWord);
if (FAILED(hRes))
{
IWbemLoc->Release();
IWbemLoc = NULL;
return;
}
printf("Success");
return;
}
**************************
Then I use the IWbemServices pointer to call the GetObject method
https://www.experts-exchange.com/questions/20455812/How-to-convert-char-to-BSTR-in-VC.html