Answer accepted
Main Topics
Browse All TopicsHow i will extract a char*(Not CString)
from BSTR.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: andlaPosted on 2000-06-16 at 06:29:14ID: 2963851
I give you two functions:
, 0, psz, -1, NULL, 0);
, 0, psz, -1, (LPWSTR)bstr, i);
********** ********** ********** ********** ********** ** ********** ********** ********** ********** ********** **
;
, 0, (LPWSTR)bstr, -1, NULL, 0, NULL, NULL);
, 0, (LPWSTR)bstr, -1, psz, i, NULL, NULL);
XED, cb*sizeof(TCHAR));
#include <tchar.h>
LPTSTR AllocLPTSTR (ULONG cb) ;
HRESULT LPTSTR_to_BSTR (BSTR *pbstr, LPCTSTR psz)
{
#ifndef UNICODE
BSTR bstr;
int i;
HRESULT hr;
// compute the length of the required BSTR
//
i = MultiByteToWideChar(CP_ACP
if (i <= 0)
{
return E_UNEXPECTED;
};
// allocate the widestr, +1 for terminating null
//
bstr = SysAllocStringLen(NULL, i-1); // SysAllocStringLen adds 1
if (bstr != NULL)
{
MultiByteToWideChar(CP_ACP
((LPWSTR)bstr)[i - 1] = 0;
*pbstr = bstr;
hr = S_OK;
}
else
{
hr = E_OUTOFMEMORY;
};
return hr;
#else
BSTR bstr;
bstr = SysAllocString(psz);
if (bstr != NULL)
{
*pbstr = bstr;
return S_OK;
}
else
{
return E_OUTOFMEMORY;
};
#endif // UNICODE
}
//************************
//
// HRESULT BSTR_to_LPTSTR (LPTSTR *ppsz, BSTR bstr)
//
//************************
HRESULT BSTR_to_LPTSTR ( BSTR bstr,LPTSTR *ppsz)
{
#ifndef UNICODE
LPTSTR psz;
int i;
HRESULT hr;
// Whoa... handle the NULL string first.
//
if(bstr == NULL) // if bstr is NULL we return E_UNEXPECTED and pass a default string
{
psz = AllocLPTSTR(lstrlen("")+1)
if(psz != NULL)
{
strcpy(psz, "");
*ppsz = psz;
}
return E_UNEXPECTED;
}
// compute the length of the required BSTR
//
i = WideCharToMultiByte(CP_ACP
if (i <= 0)
{
return E_UNEXPECTED;
};
// allocate the widestr, +1 for terminating null
//
psz = AllocLPTSTR(i);
if (psz != NULL)
{
WideCharToMultiByte(CP_ACP
*ppsz = psz;
hr = S_OK;
}
else
{
hr = E_OUTOFMEMORY;
};
return hr;
#else
LPTSTR psz = NULL;
HRESULT hr;
hr = SetName(&psz, (LPTSTR)bstr);
if (hr == S_OK)
{
*ppsz = psz;
};
return hr;
#endif // UNICODE
}
LPTSTR AllocLPTSTR (ULONG cb)
{
LPTSTR psz;
psz = (LPTSTR)LocalAlloc(LMEM_FI
return psz;
}
Regards
Andla