Getting environment variables?

Stewart Gordon smjg_1998 at yahoo.com
Sun Nov 23 05:54:40 PST 2008


"novice2" <sorry at noem.ail> wrote in message 
news:ggbd96$2drl$1 at digitalmars.com...
>>     homeDrive = toString(getenv("HOMEDRIVE")).dup;
>>     homePath = toString(getenv("HOMEPATH")).dup;
>
> don't forget, that D char[] is utf8 and windows char* is 8-bit chars, not 
> utf8.
> so you should import std.windows.charset and use toMBSz() as D->WindowsAPI 
> and fromMBSz as WindowsAPI->D string converter. for example:
> homeDrive = fromMBSz(getenv("HOMEDRIVE")).dup;

Under what setups can the drive letter be a non-ASCII character?

But according to my experiments, getenv works in the OEM encoding rather 
than the Windows encoding, so you'd need

    homePath = fromMBSz(getenv("HOMEPATH"), 1).dup;

If you want to make sure all Unicode characters are preserved, you're best 
off using the Windows API

    wchar[] wpath;
    wpath.length = GetEnvironmentVariableW("HOMEPATH", null, 0);
    GetEnvironmentVariableW("HOMEPATH", wpath.ptr, wpath.length);

Stewart.

-- 
My e-mail address is valid but not my primary mailbox.  Please keep replies 
on the 'group where everybody may benefit. 



More information about the Digitalmars-d-learn mailing list