Solved

GetLocationURL - string conversion

Posted on 2003-11-24
10
3,207 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 415
mixing C++ & C# in Vis Studio 2013 7 170
Socket Programming (Unix) 8 125
Best book to learn C++ 4 78
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

805 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