Solved

char to BSTR

Posted on 2004-05-03
3
1,401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

695 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