?
Solved

char to BSTR

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Simple Linear Regression
Six Sigma Control Plans
Suggested Courses

777 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