resolving network node using boost::asio and an std::wstring - c++

I am using Boost::Asio for network communication. The following code fails with the following compilation error.
Code:
std::wstring hostName(L"myserver");
std::wstring portName(L"myport");
auto query = boost::asio::ip::udp::resolver::query(boost::asio::ip::udp::v4(), hostName, portName);
Compilation error:
Error 1 error C2665: 'boost::asio::ip::basic_resolver_query<InternetProtocol>::basic_resolver_query' :
none of the 5 overloads could convert all the argument types
My questions:
Does Boost::Asio expect ANSI-strings (std::string) only?
Does this mean that hostnames with non-ANSI characters are not supported?
Or do I have to convert my wide strings to UTF8 first?
And in the latter case, does Boost::Asio provide standard methods to do this or can I use my own ANSI/Unicode conversion routines?
Currently running on Windows, using Visual Studio 2013.
Thanks in advance.

Does Boost::Asio expect ANSI-strings (std::string) only? It expects std::string, but not necessarily ANSI, since std::string can store UTF-8.
Does this mean that hostnames with non-ANSI characters are not supported? I haven't personally tried it, but since this is essentially a wrapper on top of the underlying OS networking code, I can't find any reason it wouldn't support using UTF-8.
Or do I have to convert my wide strings to UTF8 first? Most likely, yes, since the wide string format is only used by core Windows APIs, while UTF-8 is used by everyone else including Boost.
And in the latter case, does Boost::Asio provide standard methods to do this or can I use my own ANSI/Unicode conversion routines? Boost doesn't appear to support wide-char. As long as you are using Windows-specific character encoding, you can either use the COM interfaces that are provided by Windows for converting (MultiByteToWideChar, WideCharToMultiByte), or use your own routines. If your own routines are known to be reliable/correct, and you are not already using COM, you might want to stick with your own rather than adding a dependency on COM to your project.

Related

UTF8 conversion wxString::ToStdString()

I am writing application and I am using wxWidgets as GUI backend. Core part of app uses std::string and UTF8 as encoding. I need sane way to convert between wxString and std::string. I know about wxString::ToUTF8() but it is somewhat awkward to use (and inefficient I think, as it return some proxy object). There is a better method wxString::ToStdString() but, if I understood properly, it uses current locale encoding. Is there a way to configure wxWidgets globally in such a way that it uses UTF8 encoding when converting between wxString and narrow char (const char*, std::string)?
No, there is no way to do this, you will have to write your own helper function using ToUTF8() or equivalent utf8_str(). This will be inefficient in the sense that it will require a conversion from UTF-32 or UTF-16 every time it's called, but this is unlikely to be a bottleneck.

Writing unicode C++ source code

I saw on the project properties on Visual Studio 2012 that you can select the Character set for your application.
I use Unicode Character set.
What is the problem with Multi-byte character set? Or better, why should I use the Unicode?
Take for example this piece of code from a DLL that I am doing
RECORD_API int startRecording(
char *cam_name, // Friendly video device name
char *time, // Max time for recording
char *f_width, // Frame width
char *f_height, // Frame height
char *file_path) // Complete output file path
{
...
}
A lot of Unicode functions from Windows.h header use wchar_t parameters; should I use wchar_t also for my functions parameters?
Should I always explicit the W functions (example: ShellExecuteW) ?
First, regardless of what the interface says, the question isn't
Unicode or not, but UTF-16 or UTF-8. Practically speaking, for
external data, you should only use UTF-8. Internally, it
depends on what you are doing. Conversion of UTF-8 to UTF-16 is
an added complication, but for more complex operations, it may
be easier to work in UTF-16. (Although the differences between
UTF-8 and UTF-16 aren't enormous. To reap any real benefits,
you'd have to use UTF-32, and even then...)
In practice, I would avoid the W functions completely, and
always use char const* at the system interface level. But
again, it depends on what you are doing. This is just a general
guideline. For the rest, I'd stick with std::string unless
there was some strong reason not to.
You don't need to explicitly call, the ..W version of a function as this should already be covered by the include files and the settings that you use. So if you compile for Unicodesupport, then the W version of your system call will be used, otherwise the A.
Personally I would only compile for Unicode if you can really test it. At least you shouldn't assume that your application really can work properly in all cases, just because you compiled for it. Compiling for it is only the first step, but of course, you must consequently use the appropriate types and test your code, to be sure that there are no effects you may not have noticed.

What type of string is best to use for Win32 and DirectX?

I am in the process of developing a small game in DirectX 10 and C++ and I'm finding it hell with the various different types of strings that are required for the various different directx / win32 function calls.
Can anyone recommend which of the various strings are available are the best to use, ideally it would be one type of string that gives a good cast to the other types (LPCWSTR and LPCSTR mostly). Thus far I have been using std::string and std::wstring and doing the .c_str() function to get it in to the correct format.
I would like to get just 1 type of string that I use to pass in to all functions and then doing the cast inside the function.
Use std::wstring with c_str() exactly as you have been doing. I see no reason to use std::string on Windows, your code may as well always use the native UTF-16 encoding.
I would stick to std::wstring as well. If you really need to pass std::string somewhere, you can still convert it on the fly:
std::string s = "Hello, World";
std::wstring ws(s.begin(), s.end());
It works the other way around as well.
If you're using Native COM (the stuff of #import <type_library>), then _bstr_t. It natively typecasts to both LPCWSTR and LPCSTR, and it meshes nicely with COM's memory allocation model. No need for .c_str() calls.

Boost.Locale - Unicode string in C++

Can I make all std::string in my application support Unicode with Boost.Locale? After reading the documentation I can say yes. But I don't understand how it works. The main question is can I still use boost string algorithms library or Boost.Lexical_Cast libraries? If yes, why I need boost::locale::to_upper and similar format methods, if I have these methods in boost string algorithm library.
Yes, you can make all strings in your application Unicode encoded with Boost.Locale.
To make it work you imbue the locale into the string, or set the default global locale to your new unicode-based locale (generated by Boost.Locale).
See here for how to do that: http://www.boost.org/libs/locale/doc/html/locale_gen.html
and http://www.boost.org/libs/locale/doc/html/faq.html
The string manipulation APIs in Boost.Locale are different to the ones provided in the Boost string algorithm library.
See here for why the Boost.Locale functions are better: http://www.boost.org/libs/locale/doc/html/conversions.html
You can still use boost::lexical_cast, provided you set the global locale correctly (as, if I recall correctly, you can't explicitly pass a locale object to Boost.LexicalCast).
Keep in mind however that this will 'break' some cases, for example, when converting an integer to a string, instead of using the C locale (as was probably your previous default), it will use a different one, which may insert separators etc. When doing conversions that are NOT displayed to the user, you may wish to use std::stringstream et al directly to avoid these unwanted formatting changes.
I highly suggest you read the Boost.Locale documentation in full, as it should address most of your concerns (especially the FAQ, generation backend information, etc.).

how many types of strings in visual c++

How many types of string classes are there in visual c++. I got confused when i was going through the msdn center.
I found this type under the namespace system
http://msdn.microsoft.com/en-us/library/system.string(v=VS.71).aspx
and then in the headers section, i found the string header definitions. This seemed different from the above. One thing i noticed, this one comes under the STL.
(pls see the comment for the link, i cant post two links in the same post)
which one is normally used? I'm finding a hard time getting around with the different string classes
Thanks in advance :)
Different libraries come with different string types:
In plain old C you would use char*, the C++ standard library provides std::string which is widely used in C++ development. (string is defined as typedef basic_string<char> string;)
Microsoft created the MFC CString class which is (was?) used in MFC style programming, Qt has its QString which is used in Qt programs. What you're mentioning with System.String is a .net string class which can only be used in Managed code (with .net).
I'd suggest to stick with std::string (#include <string>) if you're new to C++. It's standard and platform independent.
String types in common use in Microsoft code are char*, wchar_t*, LPSTR, LPTSTR, LPWSTR, LPCSTR, LPCTSTR, LPCWSTR, BSTR, OLESTR, UNICODE_STRING, String, string, wstring, _bstr_t, CString
The last 5 are classes. You pick the one that gives you the least conversion headaches, depending on what API you need to use:
std::string and wstring, standard C++ library
System::String, the string type for managed code
_bstr_t, wrapper for a BSTR, used in COM automation
CString, string type for the ATL and MFC libraries.
You're likely to encounter additional string types when you work with other APIs.