Jayan
Jayan

Reputation: 18458

_wgetenv gets ?? mark values when value contains Unicode characters (japanese)

(Title updated to indicate that problem was with _wgetenv)

The following program fails with error 123 when the variable has path that contains japanese text. The same program works fine when compiled with Visual Studio 2005.

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
int  main  (int argc, char  **argv ) {
        wchar_t *wcharVarName= L"TEMP_PATH";
        wchar_t *longTempPath= _wgetenv(wcharVarName);

        MessageBoxW( NULL,  longTempPath, longTempPath, MB_OK | MB_ICONERROR);

        int wlength = GetShortPathNameW(longTempPath,0,0);
        int error= GetLastError();
        if ( wlength == 0 ) {
                char buf[1000];
                sprintf ( buf, "error code %d",error);
                MessageBox( NULL, "FAIL", buf, MB_OK | MB_ICONERROR);
        }
        else {
             MessageBoxW( NULL, longTempPath, wcharVarName, MB_OK | MB_ICONERROR);
        }

}

Is there way to make above program works with Visual Studio 6?

Edit: The TEMP_PATH was C:\tmp\漢字. The _wgetenv shows...

enter image description here

Upvotes: 1

Views: 882

Answers (1)

David Heffernan
David Heffernan

Reputation: 613302

For some reason, the call to _wgetenv is not returning the desired value under VS6. The ? symbols indicate that an encoding conversion has failed.

A quick workaround is to use GetEnvironmentVariable instead.

Upvotes: 3

Related Questions