Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert from LPCSTR to LPCWSTR in c++

Tags:

c++

winapi

additional info im building an application which use the WinHttpOpenRequest Api which requires LPCWSTR for the object name and im using visual studio 2008

like image 996
andrewmag Avatar asked Nov 08 '11 00:11

andrewmag


1 Answers

The simplest way is to use ATL:

#include <Windows.h>
#include <atlbase.h>
#include <iostream>

int main(int argc, char *argv[]) {
    USES_CONVERSION;
    LPCSTR a = "hello";
    LPCWSTR w = A2W(a);
    std::wcout << w << std::endl;
    return 0;
}

Any memory allocated by A2W (ANSI to Wide) will be freed when the function exits.

like image 77
Ferruccio Avatar answered Nov 02 '22 06:11

Ferruccio