Win32::API and SetEnvironmentVariable

Discussion in 'Perl Misc' started by Shawn Campbell, May 4, 2004.

  1. I'm having a problem setting environment variables with a large
    string. According to the MSDN documentation, an environment variable
    can be up to 32k. I'm well under this value, but it's not being set.

    Following is the code I'm using.

    sub SetEnvVar
    {
    my($Variable)=shift(@_);
    my($VarValue)=shift(@_);

    # Load the function from the KERNEL32 library. If we can't, let it
    be
    # known...
    #
    $Win32_SetEnvVar = new Win32::API("kernel32",
    "SetEnvironmentVariable", [P, P], N);

    if (!(defined $Win32_SetEnvVar))
    {
    &StatusOut ("Unable to load SetEnvironmentVariable!\n");
    exit;
    }

    # Now let's make the call..
    #
    $rtn = $Win32_SetEnvVar->Call($Variable, $VarValue);

    return;
    }


    To test this I used a call to get the PATH variable and then write the
    path value to a new environment variable (FOO) and then did a
    GetEnvVar of FOO. This worked and I received the entire value back.

    Then I doubled the size of the path value ($Val="$Val;$Val") and wrote
    it out to FOO and did a GetEnvVar of FOO again. The second time it
    failed to write out the path value and I received nothing back.

    Is there some max limit on what Win32::API allows to be passed into
    it?

    -Shawn
     
    Shawn Campbell, May 4, 2004
    #1
    1. Advertising

  2. Shawn Campbell

    Ben Morrow Guest

    Quoth (Shawn Campbell):
    > I'm having a problem setting environment variables with a large
    > string. According to the MSDN documentation, an environment variable
    > can be up to 32k. I'm well under this value, but it's not being set.
    >
    > Following is the code I'm using.


    <snip Win32::API SetEnvironmentVar>

    Why do you not just use %ENV? (Is there something I'm missing?)

    Ben

    --
    For the last month, a large number of PSNs in the Arpa[Inter-]net have been
    reporting symptoms of congestion ... These reports have been accompanied by an
    increasing number of user complaints ... As of June,... the Arpanet contained
    47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
     
    Ben Morrow, May 4, 2004
    #2
    1. Advertising

  3. Shawn Campbell

    Eric Bohlman Guest

    Ben Morrow <> wrote in
    news:c791n4$5gm$:

    >
    > Quoth (Shawn Campbell):
    >> I'm having a problem setting environment variables with a large
    >> string. According to the MSDN documentation, an environment variable
    >> can be up to 32k. I'm well under this value, but it's not being set.
    >>
    >> Following is the code I'm using.

    >
    > <snip Win32::API SetEnvironmentVar>
    >
    > Why do you not just use %ENV? (Is there something I'm missing?)


    I presume he's doing it for the reasons listed by perldoc -q environment
     
    Eric Bohlman, May 4, 2004
    #3
  4. Eric Bohlman <> wrote in message news:<Xns94DFB77DFE349ebohlmanomsdevcom@130.133.1.4>...
    > Ben Morrow <> wrote in
    > news:c791n4$5gm$:
    >
    > >
    > > Quoth (Shawn Campbell):
    > >> I'm having a problem setting environment variables with a large
    > >> string. According to the MSDN documentation, an environment variable
    > >> can be up to 32k. I'm well under this value, but it's not being set.
    > >>
    > >> Following is the code I'm using.

    > >
    > > <snip Win32::API SetEnvironmentVar>
    > >
    > > Why do you not just use %ENV? (Is there something I'm missing?)

    >
    > I presume he's doing it for the reasons listed by perldoc -q environment


    Actually, it looks like the SetEnvVar sub I created is working fine,
    other than truncating the value at 1024. The problem was in the
    GetEnvVar sub I created. I didn't supply enough buffer size so it
    wasn't reading in the value, giving me the impression it was not set.

    I have another environment variable issue I am struggling with that I
    hope you could give me some insight on.

    I'm using ActivePerl to create an automated build process. Part of
    the process is setting the LIB, INCLUDE, PATH, SOURCE variables from
    within the Perl script and then using a system call to invoke
    MSDEV.EXE using the /USEENV option. It looks like the process the
    Perl script is running in is setting the variables I need, but MSDEV
    is not finding them. If I manually set the same values within the DOS
    console and then run my Perl script, MSDEV finds the variables.

    I'm working on Windows XP, so I'm not sure if this might be the issue,
    but it behaves as if MSDEV is not looking at the process that the Perl
    script is running in, but is looking at it's parent for the
    environment variables.

    Is there any way of setting environment variables within the 'parent'
    DOS process from the PErl script?

    Thanks for any help provided.

    -Shawn Campbell
     
    Shawn Campbell, May 5, 2004
    #4
  5. Shawn Campbell

    Ben Morrow Guest

    Quoth (Shawn Campbell):
    > Eric Bohlman <> wrote in message news:<Xns94DFB77DFE349ebohlmanomsdevcom@130.133.1.4>...
    > > Ben Morrow <> wrote in
    > > news:c791n4$5gm$:
    > >
    > > >
    > > > Quoth (Shawn Campbell):
    > > >> I'm having a problem setting environment variables with a large
    > > >> string. According to the MSDN documentation, an environment variable
    > > >> can be up to 32k. I'm well under this value, but it's not being set.
    > > >>
    > > >> Following is the code I'm using.
    > > >
    > > > <snip Win32::API SetEnvironmentVar>
    > > >
    > > > Why do you not just use %ENV? (Is there something I'm missing?)

    > >
    > > I presume he's doing it for the reasons listed by perldoc -q environment

    >
    > Actually, it looks like the SetEnvVar sub I created is working fine,
    > other than truncating the value at 1024. The problem was in the
    > GetEnvVar sub I created. I didn't supply enough buffer size so it
    > wasn't reading in the value, giving me the impression it was not set.


    Can you clarify: does SetEnvironmentVar simply set the var for the
    current process, or does it change the default environment in the
    registry? If it does the former, then there really is no reason not to
    simply use %ENV.

    > I have another environment variable issue I am struggling with that I
    > hope you could give me some insight on.
    >
    > I'm using ActivePerl to create an automated build process. Part of
    > the process is setting the LIB, INCLUDE, PATH, SOURCE variables from
    > within the Perl script and then using a system call to invoke
    > MSDEV.EXE using the /USEENV option. It looks like the process the
    > Perl script is running in is setting the variables I need, but MSDEV
    > is not finding them. If I manually set the same values within the DOS
    > console and then run my Perl script, MSDEV finds the variables.


    This makes me suspect the latter; which is definitely not what you want
    in this case. MSDEV.EXE will receive the environment given it by the
    perl process, rather than the default environment. In addition, new
    console windows you open *will* receive the default environment, so they
    will get the values for LIB etc. you have specified... not terribly
    useful. Try using %ENV.

    > I'm working on Windows XP, so I'm not sure if this might be the issue,
    > but it behaves as if MSDEV is not looking at the process that the Perl
    > script is running in, but is looking at it's parent for the

    GRR ^
    > environment variables.


    This is unlikely. Processes don't have parents under windows (except
    under certain restricted situations).

    > Is there any way of setting environment variables within the 'parent'
    > DOS process from the PErl script?


    No.

    Ben
     
    Ben Morrow, May 5, 2004
    #5
  6. Ben Morrow <> wrote in message news:<c7b60i$mu0$>...
    > Quoth (Shawn Campbell):
    > > Eric Bohlman <> wrote in message news:<Xns94DFB77DFE349ebohlmanomsdevcom@130.133.1.4>...
    > > > Ben Morrow <> wrote in
    > > > news:c791n4$5gm$:
    > > >
    > > > >
    > > > > Quoth (Shawn Campbell):
    > > > >> I'm having a problem setting environment variables with a large
    > > > >> string. According to the MSDN documentation, an environment variable
    > > > >> can be up to 32k. I'm well under this value, but it's not being set.
    > > > >>
    > > > >> Following is the code I'm using.
    > > > >
    > > > > <snip Win32::API SetEnvironmentVar>
    > > > >
    > > > > Why do you not just use %ENV? (Is there something I'm missing?)
    > > >
    > > > I presume he's doing it for the reasons listed by perldoc -q environment

    > >
    > > Actually, it looks like the SetEnvVar sub I created is working fine,
    > > other than truncating the value at 1024. The problem was in the
    > > GetEnvVar sub I created. I didn't supply enough buffer size so it
    > > wasn't reading in the value, giving me the impression it was not set.

    >
    > Can you clarify: does SetEnvironmentVar simply set the var for the
    > current process, or does it change the default environment in the
    > registry? If it does the former, then there really is no reason not to
    > simply use %ENV.
    >
    > > I have another environment variable issue I am struggling with that I
    > > hope you could give me some insight on.
    > >
    > > I'm using ActivePerl to create an automated build process. Part of
    > > the process is setting the LIB, INCLUDE, PATH, SOURCE variables from
    > > within the Perl script and then using a system call to invoke
    > > MSDEV.EXE using the /USEENV option. It looks like the process the
    > > Perl script is running in is setting the variables I need, but MSDEV
    > > is not finding them. If I manually set the same values within the DOS
    > > console and then run my Perl script, MSDEV finds the variables.

    >
    > This makes me suspect the latter; which is definitely not what you want
    > in this case. MSDEV.EXE will receive the environment given it by the
    > perl process, rather than the default environment. In addition, new
    > console windows you open *will* receive the default environment, so they
    > will get the values for LIB etc. you have specified... not terribly
    > useful. Try using %ENV.
    >
    > > I'm working on Windows XP, so I'm not sure if this might be the issue,
    > > but it behaves as if MSDEV is not looking at the process that the Perl
    > > script is running in, but is looking at it's parent for the

    > GRR ^
    > > environment variables.

    >
    > This is unlikely. Processes don't have parents under windows (except
    > under certain restricted situations).
    >
    > > Is there any way of setting environment variables within the 'parent'
    > > DOS process from the PErl script?

    >
    > No.
    >
    > Ben


    Using ENV worked. I still can't figure out why using the
    SetEnvironmentVariable function would not work, but at this point I
    don't care as long as I have a solution.

    Thanks for the help!

    Shawn Campbell
     
    Shawn Campbell, May 6, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Lyman
    Replies:
    0
    Views:
    688
    Alex Lyman
    Mar 7, 2004
  2. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    387
    Praveen, Tayal (IE10)
    Mar 17, 2005
  3. Thomas Thomassen
    Replies:
    2
    Views:
    162
    Thomas Thomassen
    Dec 29, 2010
  4. Michael Edmonson

    Win32::SAPI4 question (Win32 events and Perl)

    Michael Edmonson, Feb 28, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    198
    Michael Edmonson
    Feb 28, 2004
  5. jc
    Replies:
    1
    Views:
    151
    sisyphus
    Apr 18, 2009
Loading...

Share This Page