[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

char to BSTR

Posted on 2004-05-03
3
Medium Priority
?
1,415 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Progress

831 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