Solved

char to BSTR

Posted on 2004-05-03
3
1,388 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
Comment Utility
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
Comment Utility
#include <comdef.h>


BSTR McName = SysAllocString((_bstr_t)javaMcName);

0
 

Author Comment

by:vassim
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now