This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / Windows Service doesn't pickup new System Variables?
-collapsar(笨笨和旦旦);
2004-7-19
{574}
(#1807963@0)
-
我想restart之外,还要每次restart之前把service explorer关掉试试,不行还得重新install一遍试试,更多地就不知道了
-vega_lee(小包子包的挺紧的);
2004-7-19
(#1808004@0)
-
Your ID sounds too yellow, no good for taste:)
-noproblem(大可以);
2004-7-19
(#1808112@0)
-
He became COLOR WOLF now.
-scottee(笑熬浆糊);
2004-7-19
(#1808165@0)
-
The modifications to the environment variables do not result in immediate change. To effect these changes, you need to log off and log on again.
-dundas888(登达寺);
2004-7-19
(#1808040@0)
-
Modifications to env vars can affect the prcocesses started after modification, but not existed processes. For service, I think re-logon won't have effectsince the service is running as system process (but I haven't tried it).
-xhis(枫下恩仇#13 xhis);
2004-7-19
{73}
(#1808061@0)
-
The funny fact is, sometimes, even reboot the PC doesn't help it, the service still getting the old variable values! Strange.
-collapsar(笨笨和旦旦);
2004-7-19
(#1808088@0)
-
Actually, it does immediately. For example, if you open a DOS window, type in "set", you will get the new variable.
-collapsar(笨笨和旦旦);
2004-7-19
(#1808087@0)
-
It's different, if you set env vars in DOS process, it only affect the process itself.
-xhis(枫下恩仇#13 xhis);
2004-7-19
(#1808159@0)
-
Right, so the new DOS process gets the new variable. And I assume, if I restart the service, I will get it too. But it's not the case. Even I reboot the PC, sometimes it still gets the old varable.
-collapsar(笨笨和旦旦);
2004-7-19
(#1808171@0)
-
FYI, from Microsoft Knowledge Base => How To Propagate Environment Variables to the System
-dundas888(登达寺);
2004-7-19
(#1808161@0)
-
Thanks, someone answered me the question from the Mircrosft Newsground exactly the same way.
-collapsar(笨笨和旦旦);
2004-7-19
(#1808168@0)
-
Here is my opinion after reviewing the articleThe article only tells you how to notify other applications by sending
out WM_SETTINGCHANGE.It doesn't tell us how to perform an update
when we get this message.
Plus, the article says we have to logoff and on to reflect the change of
variable, which is not the case: Idon't have to logoff / on to get the new
variable from my DOS Prompt.
I suspect ther service manager cache the settings? And sometimes
even reboot the machine doesn't help it the re-cache the env settings?
-collapsar(笨笨和旦旦);
2004-7-19
{491}
(#1808227@0)
-
I've done some research on this. Normally, a service is started as 'System', it's like the service process is forked from 'System' process.Since the 'System' process is kept running from the start of OS, and modifying env var can not affect existed processes, the 'System' process will always keep the env created when OS starts.
There might be some way to resolve this issue (such as, running service as a normal user). But I didn't want to wast time on it, istead, I made some work-around.
-xhis(枫下恩仇#13 xhis);
2004-7-19
{360}
(#1808056@0)
-
Thanks, but #1808088
-collapsar(笨笨和旦旦);
2004-7-19
(#1808091@0)
-
In my test, it always works. Something I can think of: 1) make sure the env vars are system-wide not user-specific; 2) check the env vars directly while starting service to see if it's really sth. about env vars.
-xhis(枫下恩仇#13 xhis);
2004-7-19
(#1808137@0)
-
Thanks!1. Yes, it is system wide, and the logon user is administrator. But it is not consistent, sometimes it works, sometimes it doesn't. So I am not surprised it works at your site.
2. I will double check it as what you suggested.
Plus, I have posted a question to Microsoft's Newsgroup, I will see what I can get.
-collapsar(笨笨和旦旦);
2004-7-19
{318}
(#1808146@0)
-
For further discussion, please describe the way you set env vars and the way you use env vars.
-xhis(枫下恩仇#13 xhis);
2004-7-19
(#1808188@0)
-
Sorry, I didn't make myself clear.Here was exactly what I did:
1. I made a service, inside the service, I printf out a System Variable, say A.
2. I modified the System Variable A by System->Properties->Advanced->Environment Variable.
3. I restarted the service, but printed out the old value of A.
4. Reboot the PC sometimes doesn't help, still getting the old value of A. But sometimes works...
-collapsar(笨笨和旦旦);
2004-7-19
{372}
(#1808198@0)
-
In this way, you can forget about the topic given above, as soon as you click the 'OK' in your step 2, OS will do everything describe in that topic for you. My suggestion:1. after you set up the env, start a new DOS window, check if the env is available;
2. after you reboot the machine, check again
3. if they are all ok, then problem is in your program.
-xhis(枫下恩仇#13 xhis);
2004-7-19
{186}
(#1808217@0)
-
Again, thanks.1. Yes
2. Yes
3. My program is just an empty framwork, which just print out the Env Variables only for the time being. But still it sometimes getts the right value, sometimes doesn't no matter whether I restart the service or reboot the PC.
That's why I found it werid.
-collapsar(笨笨和旦旦);
2004-7-19
{277}
(#1808224@0)
-
If possible, it would be helpful to post your code.
-xhis(枫下恩仇#13 xhis);
2004-7-19
(#1808243@0)
-
Like I said, I used VC6 wizard to build a service. Right in the startup code, I getenv(..) and print it out. Very simple.
-collapsar(笨笨和旦旦);
2004-7-19
(#1808253@0)
-
You might want to try GetEnvrionmentVariable(), reason: getenv() might not be working properly. here is sth. from MSDN:getenv operates only on the data structures accessible to the run-time library and not on the environment “segment” created for the process by the operating system. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.
-xhis(枫下恩仇#13 xhis);
2004-7-19
{264}
(#1808277@0)
-
I did, but still... Thanks
-collapsar(笨笨和旦旦);
2004-7-19
(#1808281@0)
-
I'm out of ideas, I guess it might have somthing to do with your code.
-xhis(枫下恩仇#13 xhis);
2004-7-19
(#1808373@0)
-
Come on, trust me! But there still one thing left I can do: Read the settings from Registry every time I need them, then use them to rebuild my Env Block of the service. Thanks a lot so far. : )
-collapsar(笨笨和旦旦);
2004-7-19
(#1808473@0)