I need to take a url from a text control and pass it to a library that expects a std::string. I can get the url from the control using GetValue, but that gives me a wxString.
I have tried to use wxString::c_str(), but that gives me a compilation error:
Quote
error: conversion from `const wxChar*' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested
I am using wxWidgets 2.6.3 unicode and Codeblocks with Mingw.
The problem is that you are using unicode, and your API expects const char*.
You need to convert the string.
Here is a link to unicode:
http://wiki.codeblocks.org/index.php?title=Unicode_Standards (http://wiki.codeblocks.org/index.php?title=Unicode_Standards)
And in short, what you need is:
Add this section to one of your headers (a one that is included by all)
#ifdef wxUSE_UNICODE
#define _U(x) wxString((x),wxConvUTF8)
#define _UU(x,y) wxString((x),y)
#define _CC(x,y) (x).mb_str((y))
#else
#define _U(x) (x)
#define _UU(x,y) (x)
#define _CC(x,y) (x)
#endif
#define _C(x) _CC((x),wxConvUTF8)
And in your code, whenever your API is requiring a const char*, do this:
const wxCharBuffer pbuff = _C(wx_string); // convert wxString to wxCharBuffer
foo(pbuff.data()); // get the data as cnost char*
Eran