• 如何为当前进程设置环境变量?


    最近在开发一个vss源码管理的插件,使用ss.exe命令行方式,操作源码管理库,其中需要设置ssdir环境变量,虽然可以通过,操作系统进行配置,但是不断录活,由于使用的是vs2003,.net1.1只提供获取系统变量的方法,没有提供设置系统变量的方法(.net2.0已提供),只好请出API

    Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As long

    注意需要把返回类型,更改为

    Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Integer

    以前方法只对当前进程有效。

    如何设置全局的呢,在发网上发现一个例子:(vb6.0)

    Windows 提供了API函数SetEnvironmentVariable,不过这个函数只能修改当前进程的环境变量,而不能修改其他进程和系统的变量。
        要修改系统的环境变量,需要修改注册表SYSTEM\CurrentControlSet\Control\Session Manager\Environment下的项,然后发送WM_SETTINGCHANGE消息。

    下面就是使用VB写成的例子(完整的版本请从http://www.vbrad.com/pf.asp?p=source/src_environment.htm下载):
    'Note: this piece of code has dependencies on cValuePair and cValuePairs classes
    Option Explicit
    Private mstrEnvironment As String
    Private Const REGISTRY_PATH = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    Private oRegistry As Registry
    Private Declare Function SendMessageTimeout Lib "user32" _
         Alias "SendMessageTimeoutA" (ByVal hwnd As Long, _
         ByVal msg As Long, ByVal wParam As Long, _
         ByVal lParam As String, ByVal fuFlags As Long, _
         ByVal uTimeout As Long, lpdwResult As Long) As Long
    Private Const HWND_BROADCAST As Long = &HFFFF&
    Private Const SMTO_ABORTIFHUNG As Long = &H2
    Private Const WM_SETTINGCHANGE As Long = &H1A
    Private mValuePairs As cValuePairs
    Private Sub EnumerateVariables()
    Dim sKeys() As String
    Dim iKeyCount As Long
    Dim x As Long
    Dim oValuePair As cValuePair
    Set mValuePairs = New cValuePairs
         oRegistry.EnumerateValues sKeys(), iKeyCount
    For x = 1 To iKeyCount
         oRegistry.ValueKey = sKeys(x)
    Set oValuePair = New cValuePair
         oValuePair.Variable = sKeys(x)
         oValuePair.Value = oRegistry.Value
         mValuePairs.Add oValuePair, oValuePair.Variable
    Set oValuePair = Nothing
    Next
    End Sub
    Public Property Get Environment(ByVal strName As String) As String
         oRegistry.ValueKey = strName
         Environment = oRegistry.Value
    End Property
    Public Property Let Environment(ByVal strName As String, ByVal strValue As String)
         oRegistry.ValueKey = strName
         oRegistry.ValueType = REG_SZ
         oRegistry.Value = strValue
         BroadcastEnvironmentChange
    End Property
    Private Sub BroadcastEnvironmentChange()
    Dim lngReturnValue As Long
         SendMessageTimeout HWND_BROADCAST, WM_SETTINGCHANGE, 0&, _
         "Environment", SMTO_ABORTIFHUNG, 5000&, lngReturnValue
    End Sub
    Public Property Get List() As cValuePairs
    If mValuePairs Is Nothing Then
         EnumerateVariables
    End If
    Set List = mValuePairs
    End Property
    Private Sub Class_Initialize()
    Set oRegistry = New Registry
         oRegistry.ClassKey = HKEY_LOCAL_MACHINE
         oRegistry.SectionKey = REGISTRY_PATH
    End Sub
    Private Sub Class_Terminate()
    Set oRegistry = Nothing
    Set mValuePairs = Nothing
    End Sub

  • 相关阅读:
    bzoj 5092: [Lydsy1711月赛]分割序列
    bzoj1173: [Balkan2007]Point
    bzoj1536: [POI2005]Akc- Special Forces Manoeuvres
    bzoj2178: 圆的面积并
    bzoj1043 下落的圆盘
    bzoj2674 Attack
    bzoj1201: [HNOI2005]数三角形
    bzoj3135: [Baltic2013]pipesd
    bzoj1760 [Baltic2009]Triangulation
    bzoj3136
  • 原文地址:https://www.cnblogs.com/zqonline/p/1449725.html
Copyright © 2020-2023  润新知