Re: charset issue on Windows help needed



On Saturday 13 September 2014 14:24:35 Fernando Rodriguez wrote:
On Saturday 13 September 2014 4:21:18 PM Geert Janssens wrote:
Thanks a lot !

I'll try to apply a similar approach in gnucash for the
home dir use case.

For my second case, anybody know how to read an
environment variable directly in win32 using wide char
functions ?


Geert
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

You'd use the GetEnvironmentVariable function. What compiler and IDE
are you using?

I'm using mingw32, gcc 4.8.1.

In Windows every API function that deals with text has two variants,
so theres GetEnvironmentVariableA and GetEnvironmentVariableW.

If you use Visual Studio there's an option on project properties to
select the encoding and it #defines a symbol that defines the macros
to call either variant and you would use the TCHAR (there's a bunch
of typedefs for dealing with strings for example LPTSTR, LPSTR,
LPWSTR but they all boil down to the same thing, in this case LPSTR
is typedef for char*, LPWSTR wchar_t*, and LPTSTR is char* for ansi
and wchar_t* for unicode) type to store the strings (it's a typedef
for char when using ANSI and wchar_t when using unicode). I'm not
sure what the symbol is called so if you're not using VS you can just
use the wide char variants directly or look at the headers and find
out what you need to define.

Thanks for the additional detail (for some reason my previous mail got truncated by the list 
software.

I have now written this function:

#ifdef G_OS_WIN32
#define BUFSIZE 4096
static gchar* get_env_utf8 (const gchar* var_name)
{
    LPWSTR val_win;
    gchar *val_utf8;
    guint32 retval;
    
    ENTER();
    val_win = (LPWSTR) malloc (BUFSIZE*sizeof(WCHAR));
    if (!val_win)
        return NULL; /* Out of memory... */
    
    retval = GetEnvironmentVariableW (g_utf8_to_utf16 (var_name, -1, NULL, NULL, NULL), 
val_win, BUFSIZE);
    if (0 == retval)
        return NULL;  /* Variable not set */
    
    if (BUFSIZE < retval)
    {
        PWARN("Value of environment variable GNC_DOT_DIR is longer than %d. "
              "The code can't handle this, so returning NULL instead.", BUFSIZE);
        return NULL; /* Woa, path is way to long... */
    }
    
    if ((val_utf8 = g_utf16_to_utf8 ((gunichar2*) val_win, -1, NULL, NULL, NULL)) != NULL)
        return val_utf8;
    else
        return NULL;
}
#endif

But in the end val_utf8 still doesn't keep the special characters.

If the environment variable GNC_DOT_DIR is set to "c:\gcdev\Ɓukasz", val_win is printed in 
gdb as L"c:\\gcdev\\Lukasz" and
val_utf8 as "c:\\gcdev\\Lukasz"

If I examine the individual characters using
print val_win[9] and print val_win[10] those result in
L'L' and L'u'. To me that looks as if there are no wide characters in the original string. :(

This is really puzzling me. What am I missing ?

Geert


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]