I get an arrary of TCHAR* as parameter
TCHAR* env[]
How can I get the entry count in this array? No param tells me the count
value.
The Great Chairman Mao taught us that something does not come from nothing.
You got to provide another parameter, or maybe
this is a zero terminated string.
--pa
Sorry... the last line should be: NULL terminated array of pointers.
/* For the Great Chairman also taught to think twice and only then hit
the send key :( */
--pa
But he surely must have also taught that the smallest
measureable amount of time is that between hitting
the enter key and the "oh sh*t".
But this does not answer my question. In vc console app, the main entry is
in the form of
int _tmain(int argc, _TCHAR* argv[])
Many people do not know there is the 3rd parameter:
int _tmain(int argc, _TCHAR* argv[], _TCHAR* env[])
env points to the env variables passed from the os loader to this process.
It does not have the count info of the env variables. How can get the count?
One method I'm using is to have a pointer enumerate the env (pointer ++)
until it sees a NULL. In the enumeration, I count the entry number (count
++). Is there a better way? Can I use the sizeof method, say sizeof(the
whole array)/sizeof(each element)?
"Duane Hebert" <sp...@flarn.com> wrote in message
news:eswzwyw...@TK2MSFTNGP06.phx.gbl...
From MSDN:
envp
A pointer to an array of environment strings. It can be declared as an
array of pointers to char (char *envp[ ]) or as a pointer to pointers to
char (char **envp). If your program uses wmain instead of main, use the
wchar_t data type instead of char. The end of the array is indicated by
a NULL pointer. The environment block passed to main and wmain is a
“frozen” copy of the current environment. If you subsequently change the
environment via a call to putenv or _wputenv, the current environment
(as returned by getenv/_wgetenv and the _environ/ _wenviron variable)
will change, but the block pointed to by envp will not change. This
argument is ANSI compatible in C, but not in C++.
--
Norm
To reply, change domain to an adult feline.
"a" <a...@a.com> ??????:eCZPHnwH...@TK2MSFTNGP02.phx.gbl...
You could, if you knew sizeof(the whole array). You don't.
Scan until NULL is the only way, exactly like they way you process argv.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
That won't compile, because env is a TCHAR **, not a TCHAR *.
However, even if you substituted _tcslen(*env), that would only get you the
length of the first variable in the environment. The environment is not
passed as one long string. It's passed as a list of pointers to strings,
one variable per entry. You have to stroll through the whole list. There's
no shortcut.
Okay, and what is that supposed to be? The length in elements would be
size_t elements = _tcslen(str)+1;
including the null terminator while the length in bytes would be
size_t bytes = (_tcslen(str)+1) * (sizeof *str);
also including the trailing null. All of this of course assuming that 'str'
is a pointer to TCHAR. Further, it also doesn't take into account any
overallocation.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Except argv has exactly argc entries, you don't need to go looking for a
NULL pointer. So you process envp differently from argv.