Solved

char to BSTR

Posted on 2004-05-03
3
1,393 Views
Last Modified: 2007-12-19
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*)javaMcName);
*****************************************************

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(bstrKey1, 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.
0
Comment
Question by:vassim
3 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 10976394
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10976464
#include <comdef.h>


BSTR McName = SysAllocString((_bstr_t)javaMcName);

0
 

Author Comment

by:vassim
ID: 10976630
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(BSTR 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_IMPERSONATE, NULL,
                                     0x0, 0);
      
    // Get the WMI locator pointer.

    hRes = CoCreateInstance(CLSID_WbemLocator, NULL,
                            CLSCTX_INPROC_SERVER,IID_IWbemLocator,
                            (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(bstrNamespace, bstrUName, bstrPWord,
                                   0L, 0L, NULL, NULL, &IWbemSer);

    SysFreeString(bstrNamespace);
      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
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question