Kitsune
asked on
GetLocationURL - string conversion
I am struggling to convert strings correctly so that my toolbar can take the current URL and pass it as a string to another program as an argument. If I use a hard coded string it works fine but trying to get the actual URL dynamically is more difficult because the system function complains that it is not of type char *. This is a Unicode/BSTR to single byte char problem I think. m_pWebBrowser is merely a pointer to a iWebBrowser2 object declared as follows:
IWebBrowser2* m_pWebBrowser;
The part of the code I need help resolving is as follows:
CString myLocationURL;
CString myFullCmd;
myLocationURL = m_pWebBrowser->GetLocation URL();
myFullCmd = "c:\\vbprint.exe " + myLocationURL;
system( myFullCmd );
IWebBrowser2* m_pWebBrowser;
The part of the code I need help resolving is as follows:
CString myLocationURL;
CString myFullCmd;
myLocationURL = m_pWebBrowser->GetLocation
myFullCmd = "c:\\vbprint.exe " + myLocationURL;
system( myFullCmd );
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ATL is not MFC.
Sorry...
And if you're not using ATL there is the standard Windows API for converting from UNICODE to ANSI chars:
int WideCharToMultiByte(
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // wide-character string
int cchWideChar, // number of chars in string
LPSTR lpMultiByteStr, // buffer for new string
int cbMultiByte, // size of buffer
LPCSTR lpDefaultChar, // default for unmappable chars
LPBOOL lpUsedDefaultChar // set when default char used
);
And if you're not using ATL there is the standard Windows API for converting from UNICODE to ANSI chars:
int WideCharToMultiByte(
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // wide-character string
int cchWideChar, // number of chars in string
LPSTR lpMultiByteStr, // buffer for new string
int cbMultiByte, // size of buffer
LPCSTR lpDefaultChar, // default for unmappable chars
LPBOOL lpUsedDefaultChar // set when default char used
);
ASKER
I am using ATL in this case. The code is in Visual C++ 6 for a toolbar in Internet Explorer (which is working fine by the way).
I will try the first suggestion and post back.
I will try the first suggestion and post back.
ASKER
My apologies. I was so busy trying different things to make this work that I posted some code that I found on MSDN which was supposed to get the current URL. It didn't work because I am using ATL not MFC and an ActiveX control. Therefore, rather than the function GetLocationURL() which returns a CString, I need to use get_LocationURL and pass it a pointer to a BSTR. So my small bit of code would appear as follows:
BSTR *myLocationURL;
CString myFullCmd;
m_pWebBrowser->get_Locatio nURL( myLocationURL );
myFullCmd = "c:\\vbprint.exe " + myLocationURL;
USES_CONVERSION;
system(W2A((LPCTSTR)myFull Cmd));
I need to concatenate a known string with this BSTR pointer and somehow pass the result to system as a char *. Maybe myFullCmd could be something other than CString?
BSTR *myLocationURL;
CString myFullCmd;
m_pWebBrowser->get_Locatio
myFullCmd = "c:\\vbprint.exe " + myLocationURL;
USES_CONVERSION;
system(W2A((LPCTSTR)myFull
I need to concatenate a known string with this BSTR pointer and somehow pass the result to system as a char *. Maybe myFullCmd could be something other than CString?
ASKER
I have increased the points because what I am really after is both retrieving the current URL and then passing it as a string via the system command.
In UNICODE configuration this should work:
CString sString;
BSTR sBSTR;
...
sString += sBSTR;
CString sString;
BSTR sBSTR;
...
sString += sBSTR;
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Split: jhance {http:#9809495} & AlexFM {http:#9815787}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Split: jhance {http:#9809495} & AlexFM {http:#9815787}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer