Solved

GetLocationURL - string conversion

Posted on 2003-11-24
10
3,215 Views
Last Modified: 2008-01-09
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->GetLocationURL();
      myFullCmd = "c:\\vbprint.exe " + myLocationURL;
      system( myFullCmd );
0
Comment
Question by:Kitsune
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 75 total points
ID: 9809437
If _UNICODE is defined, the last line is not compiled. In this case change the code by following way:

#include <atlbase.h>


USES_CONVERSION;
system(W2A((LPCTSTR)myFullCmd));    // W2A converts from wchar* to char*

0
 
LVL 32

Accepted Solution

by:
jhance earned 75 total points
ID: 9809495
And if you're not using MFC 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
);
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9809769
ATL is not MFC.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 32

Expert Comment

by:jhance
ID: 9809780
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
);
0
 
LVL 1

Author Comment

by:Kitsune
ID: 9813724
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.

0
 
LVL 1

Author Comment

by:Kitsune
ID: 9814460
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_LocationURL( myLocationURL );
myFullCmd = "c:\\vbprint.exe " + myLocationURL;
USES_CONVERSION;
system(W2A((LPCTSTR)myFullCmd));

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?
0
 
LVL 1

Author Comment

by:Kitsune
ID: 9814478
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.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9815787
In UNICODE configuration this should work:

CString sString;
BSTR sBSTR;

...

sString += sBSTR;

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10285772
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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