c++lpwstr

Cannot convert CHAR to LPWSTR


I encountered a compilation error in Visual Studio 2015, that I am trying to convert char data to LPWSTR. Can I? Or does it work only with string types?

Here is a piece of my code :

    ⋮
FILE *sortie;
char fichier[256];//   <--- HERE s my char table

int main(int argc, char *argv[])
{
    //on masque 
    HWND hwnd = GetForegroundWindow();
    ShowWindow(hwnd, SW_HIDE);

    int i, lettre, result, lastresult, lastletter, compteur;

    GetCurrentDirectory(256, fichier); 
    strcat(fichier, "\\fichierlog.txt");

Before posting my question I was at:

  1. https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k%28C2664%29&rd=true

  2. C++ cannot convert from enum to LPCTSTR

  3. How to Convert char* to LPWSTR in VC++?

I didn't find my case :(


Solution

  • Instead of your current code:

    FILE *sortie;
    char fichier[256];//   <--- HERE s my char table
    
    int main(int argc, char *argv[])
    {
        //on masque 
        HWND hwnd = GetForegroundWindow();
        ShowWindow(hwnd, SW_HIDE);
    
        int i, lettre, result, lastresult, lastletter, compteur;
    
        GetCurrentDirectory(256, fichier); 
        strcat(fichier, "\\fichierlog.txt");
    

    do e.g.

    auto main() -> int
    {
        //on masque 
        HWND hwnd = GetForegroundWindow();
        ShowWindow(hwnd, SW_HIDE);
    
        int i, lettre, result, lastresult, lastletter, compteur;
    
        std::wstring fichier( MAX_PATH, L'\0' );//   <--- HERE s my char table
        const DWORD len = GetCurrentDirectory( fichier.size(), &fichier[0] );
        if( len == 0 || len >= fichier.size() ) { throw std::runtime_error( "GetCurrentDirectory failed." ); }
        fichier.resize( len );
        fichier += L"/fichierlog.txt";
    
        std::ifstream sortie( fichier );
    

    This should fix three issues:

    Note that the ifstream constructor that accepts a wide string is a Microsoft extension. It will however be practically required for Windows C++ compilers by the file system addition to the standard library in C++17.