[Programming]修改Windows环境变量生效

作者:fangzhzh
声明:允许未经作者的同意进行非商业目的的转载,但必须保证原文完整性

原文点我

不少程序需要添加各自的环境变量,方便定制性使用。用得最多的是用户指定目录如JAVA_HOME等变量,程序中可以根据获取变量%JAVA_HOME%,来获取对应设置的字符串。

一般做法是安装的时候就指定,或者程序中设定。

用批处理临时设置环境变量就不提了,这里讲让环境变量始终生效。

一般做法是修改环境变量注册表。

整个Windows都有效的环境变量在
HKEY_LOCAL_MACHINESYSTEMControlSet001ControlSession ManagerEnvironment
中设置

对当前用户有效的环境变量在
HKEY_CURRENT_USEREnvironment
中设置

其实只是简单的元数据

但往往修改玩后无法即时生效,往往需要重启系统。

要解决即时生效的问题,可以再在我的电脑属性中设置环境变量中确定一下即可。

依此看来,这个过程肯定是读取了注册表,再调用一个系统函数来更新整个系统的变量。一定可以编程解决的。

可惜网海茫茫,收不到。

一不做二不休,直接下载打开innosetup的代码,搜索Environment,果真找到了。

很简单:

SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE,
0,LPARAM(“Environment”), SMTO_ABORTIFHUNG, 5000, &MsgResult);

发一个全局的广播,等待各自相应后,立即生效。

{ Note: We originally used SendNotifyMessage to broadcast the message but it
turned out that while it worked fine on NT 4 and 2000 it didn’t work on XP
— the string “Environment” in lParam would be garbled on the receiving
end (why I’m not exactly sure). We now use SendMessageTimeout as directed
in the KB article 104011. It isn’t as elegant since it could cause us to
be delayed if another app is hung, but it’ll have to do. }

Log It,以备后来者检索。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Jans/archive/2006/06/15/800663.aspx

 




[Programming]python 项目转化到可执行文件

作者:fangzhzh
声明:允许未经作者的同意进行非商业目的的转载,但必须保证原文完整性
本文永久链接地址:http://fangzhzh.info/blog/2010/4/python-2-exe

对于这个问题,自己也尝试了不少的安装方法,现将成功的一条记录如下,供参
考。


PyQt项目转化为WIN下的可执行程序:pyqt+py2exe


下载必备

  1. 先下载与安装:Microsoft Visual C++ 2008 Redistributable Package
    (x86), 从这里:Microsoft Visual C++ 2008 Redistributable Package
    或者将msvcp90.dll直接放到如下目录内:C:\Python26\DLLs。


例子

2.写一个最简单的例子如下,命名为:test.py

[code lang="python"]

from PyQt4 import QtCore
import sys

class Form(QtGui.QDialog):

    def __init__(self, parent = None):
        super(Form, self).__init__(parent)
        #~ layout = QtGui.QVBoxLayout()
        #~ self.setLayout(layout)

def main():
    """
    The Pragram main port
    """
    app = QtGui.QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

[/code]

3.写一个安装脚本如下,命名为:setup.py

[code lang="python"]
from distutils.core import setup
import py2exe
import sys

# If run without args, build executables, in quiet mode.
if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("--includes")
    sys.argv.append("sip")

class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the versioninfo resources
        self.version = "0.5.0"
        self.company_name = "No Company"
        self.copyright = "no copyright"
        self.name = "py2exe sample files"

test = Target(
    # used for the versioninfo resource
    description = "A sample GUI app",

    # what to build
    script = "test.py",
    #~ other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog="test_wx"))],
    icon_resources = [(1, "editra.ico")]
    #~ dest_base = "test_wx"
    )
setup(
    # The first three parameters are not required, if at least a
    # 'version' is given, then a versioninfo resource is built from
    # them and added to the executables.
    #~ version = "0.5.0",
    #~ description = "py2exe sample script",
    #~ name = "py2exe samples",

    # targets to build
    windows = [test]
    #~ console = ["hello.py"],
    )
[/code]

4.执行你的setup.py脚本,看是否生成了你想要的东西。
据说(我没有测试)发布是不要忘记了文件:msvcp90.dll。

© 2012 Tao of Programmer Suffusion theme by Sayontan Sinha