returns a PCSTR data type.
But I need to feed the WriteEventViewerLog with header as a
LPCWSTR, since one of the functions that I use inside the methods
only accepts the string in that format.
First, let's clarify the meaning of these "obscure" Windows API string typedefs:
PCSTR: const char *
LPCWSTR: const wchar_t *
The difference is that PCSTR points to a char-based string; LPCWSTR points to a wchar_t-based string.
char-based strings can be of several "forms" (or encodings), e.g. simple ASCII, or Unicode UTF-8, or other "multi-byte" encodings.
In case of your header string, I assume it can be simple ASCII, or UTF-8 (note that ASCII is a proper subset of UTF-8).
wchar_t-based strings in Visual C++ are Unicode UTF-16 strings (and this is the "native" Unicode encoding used by most Win32 APIs).
So, what you have to do is to convert from a char-based string to a wchar_t-based one. Assuming that your char-based string represents a Unicode UTF-8 string (of which pure ASCII is a proper subset), you can use the MultiByteToWideChar() Win32 API to do the conversion.
Or you can use some helper classes to simplify the conversion task, for example ATL conversion helpers. In particular, the CA2W helper with the CP_UTF8 conversion flag can come in handy in your case:
#include <atlconv.h> // for CA2W
// Get the header string in Unicode UTF-8
PCSTR header = pHttpContext->GetRequest()->GetHeader("Accept", nullptr);
// Convert the header string from UTF-8 to Unicode UTF-16
WriteEventViewerLog( CA2W(header, CP_UTF8) );
The use of CA2W solved the issue. I just had to #include <atlbase.h> #include <atlstr.h> in addition to atlconv.h to be able to use it.