Solved

GetLocationURL - string conversion

Posted on 2003-11-24
10
3,198 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

895 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