C++ LPTSTR problem calling CreateProcessAsUser with getenv -


i trying call function createprocessasuser. passing constant string fine. trying pick environment variable using char* getenv(const char name) causing me problem.

if use following, notepad.exe run.

createprocessasuser(htokendup, _t("c:\\windows\\notepad.exe"),                      _t("c:\\windows\\notepad.exe"), null, null, false,                     dwcreationflag, penvironment, null, &si, &pi); 

however, if use following nothing runs.

createprocessasuser(htokendup, _t("myappname"),                      (lptstr)getenv("myenvvar"), null, null, false,                     dwcreationflag, penvironment, null, &si, &pi); 

have specified getenv , (lptstr) correctly?

i have tried using user , system environment vars containing c:\\windows\\notepad.exe , c:\windows\notepad.exe.

thanks!

the third parameter, lpcommandline lptstr means must writeable memory. need copy command line writeable string before calling createprocessasuser.

the documentation getenv states:

it not safe modify value of environment variable using returned pointer.

you therefore cannot pass lpcommandline parameter of createprocessasuser.

your first call createprocessasuser appears wrong since not passing writeable memory lpcommandline.

of course it's immediate problem mixing ansi , unicode. if app unicode need call _wgetenv, or _tgetenv if want target both ansi , unicode same source. make sure copy writeable buffer before passing on.

finally, adam commented, every time write cast, there strong possibility making mistake.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -