×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Windows Service doesn't pickup new System Variables?

I have a Windows Services running in the Windows 2000 system.

1. When the Service started, it pickup the environment settings.
2. Then I modified a System Variable, say the Path.
3. I restarted the Service, hoping that it will pickup the new
System Variable
4. But it didn't.
5. Sometimes it did! Sometimes I had to reboot the PC to get it
to work.

I will have to dig into microsoft knowledge base to find out what
is going on behind the curtain.

BTW: If anybody knows the answer or can point out a direction,
it's really appreciated.

Thanks.
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Windows Service doesn't pickup new System Variables?
    I have a Windows Services running in the Windows 2000 system.

    1. When the Service started, it pickup the environment settings.
    2. Then I modified a System Variable, say the Path.
    3. I restarted the Service, hoping that it will pickup the new
    System Variable
    4. But it didn't.
    5. Sometimes it did! Sometimes I had to reboot the PC to get it
    to work.

    I will have to dig into microsoft knowledge base to find out what
    is going on behind the curtain.

    BTW: If anybody knows the answer or can point out a direction,
    it's really appreciated.

    Thanks.
    • 我想restart之外,还要每次restart之前把service explorer关掉试试,不行还得重新install一遍试试,更多地就不知道了
      • Your ID sounds too yellow, no good for taste:)
        • He became COLOR WOLF now.
    • 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.
      • Modifications to env vars can affect the prcocesses started after modification, but not existed processes. For service, I think re-logon won't have effect
        since the service is running as system process (but I haven't tried it).
        • The funny fact is, sometimes, even reboot the PC doesn't help it, the service still getting the old variable values! Strange.
      • Actually, it does immediately. For example, if you open a DOS window, type in "set", you will get the new variable.
        • It's different, if you set env vars in DOS process, it only affect the process itself.
          • 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.
        • FYI, from Microsoft Knowledge Base => How To Propagate Environment Variables to the System
          • Thanks, someone answered me the question from the Mircrosft Newsground exactly the same way.
          • Here is my opinion after reviewing the article
            The 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?
    • 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.
      • Thanks, but #1808088
        • 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.
          • 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.
    • For further discussion, please describe the way you set env vars and the way you use env vars.
      • 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...
        • 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.
          • 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.
            • If possible, it would be helpful to post your code.
              • Like I said, I used VC6 wizard to build a service. Right in the startup code, I getenv(..) and print it out. Very simple.
                • 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.
                  • I did, but still... Thanks
                    • I'm out of ideas, I guess it might have somthing to do with your code.
                      • 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. : )