Solved

GetLocationURL - string conversion

Posted on 2003-11-24
10
3,189 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now