Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GetLocationURL - string conversion

Posted on 2003-11-24
10
Medium Priority
?
3,242 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
[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
  • 3
  • 2
  • +1
10 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 300 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 300 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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