Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Session / Thread isolated environment variable


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 8 - Last Post: Mar 31, 2017 3:07 PM Last Post By: Chad Hower
Zane Leo

Posts: 55
Registered: 12/29/09
Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 12:33 AM
Has anyone used environment variables within an IW application?

Here is the issue. I must integrate with a government provided JAVA based API to communicate with a government portal. One of the setting is a "user based" environment variable used by the API to create per user (read IW session) report files in a folder specified by that "user based" environment variable.

I am successfully creating and setting the environment variable but, of course, the user is always the same user when running IW under IIS and thus the files (with same names are overwriting each or file-locking when multiple users attempt the same process calls.

Any suggestions as to how I can set an environment variable to be session / thread specific will be GREATLY welcomed.

In anticipation, thanks in advance.
Daniel Fields

Posts: 622
Registered: 11/29/04
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 2:11 AM   in response to: Zane Leo in response to: Zane Leo
What do you mean by "I am successfully creating and setting the environment variable"? Are you doing this through your IW application?

Can you show the current code? This would help to better understand the issue.
Zane Leo

Posts: 55
Registered: 12/29/09
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 7:34 AM   in response to: Daniel Fields in response to: Daniel Fields
Daniel Fields wrote:
What do you mean by "I am successfully creating and setting the environment variable"? Are you doing this through your IW application?

Can you show the current code? This would help to better understand the issue.

I was going to add the code in my post but decided to not... :( Here it is

procedure TIWUserSession.UpdateEnVars(ip_ExMode: string);
var
  lv_Prepend, Need_BROADCAST: boolean;
  lv_EASY_API_LOG, lv_S1: string;
 
  function lf_SetEnvVar(const Name, Value: string; const User: boolean = True): boolean;
  var
    lv_S, lv_S1, NewVal: string;
    lv_L, lv_P1: Integer;
  resourcestring
    REG_MACHINE_LOCATION = 'System\CurrentControlSet\Control\Session Manager\Environment';
    REG_USER_LOCATION = 'Environment';
  begin
    Result := False;
    NewVal := Value;
    if Pos('%', NewVal) > 0 then
    begin
      lv_S1 := NewVal;
      NewVal := '';
      while Pos('%', lv_S1) > 0 do
      begin
        NewVal := NewVal + Copy(lv_S1, 1, (Pos('%', lv_S1) - 1));
        lv_S1 := Copy(lv_S1, (Pos('%', lv_S1) + 1), Length(lv_S1));
        lv_S := '';
        if Pos('%', lv_S1) > 0 then
        begin
          lv_S := Copy(lv_S1, 1, (Pos('%', lv_S1) - 1));
          lv_S1 := Copy(lv_S1, (Pos('%', lv_S1) + 1), Length(lv_S1));
          if Length(Trim(lv_S)) > 0 then
            lv_S := GetEnvironmentVariable(lv_S);
        end;
        if Length(Trim(lv_S)) > 0 then
          NewVal := NewVal + lv_S;
      end;
      if Length(Trim(lv_S1)) > 0 then
        NewVal := NewVal + lv_S1;
      NewVal := ReplaceTokenWith(NewVal, '\\', '\');
    end;
    lv_S := GetEnvironmentVariable(Name);
    if lv_Prepend and (Trim(lv_S) <> Trim(NewVal)) and ((Pos('=', NewVal) > 0) or (Pos(NewVal, lv_S) = 0)) then
    begin
      if Pos('=', NewVal) < 1 then
        lv_S1 := NewVal + ';' + lv_S
    end
    else if (not lv_Prepend) and (Pos(Value, lv_S) > 0) then
    begin
      if Pos(Value, lv_S) > 0 then
      begin
        if Pos(Value + ';', lv_S) > 0 then
        begin
          lv_L := Length(Value) + 2;
          lv_P1 := Pos(Value + ';', lv_S) - 1;
        end
        else if Pos(';' + Value, lv_S) > 0 then
        begin
          lv_L := Length(Value) + 1;
          lv_P1 := Pos(';' + Value, lv_S);
        end
        else
        begin
          lv_L := Length(Value);
          lv_P1 := Pos(Value, lv_S);
        end;
        lv_S1 := Copy(lv_S, 1, lv_P1) + Copy(lv_S, lv_P1 + lv_L, Length(lv_S));
      end;
    end
    else
      Exit;
 
    with TRegistry.Create do
      try
        if User then { User Environment Variable }
          Result := OpenKey(REG_USER_LOCATION, True)
        else { System Environment Variable }
        begin
          RootKey := HKEY_LOCAL_MACHINE;
          Result := OpenKey(REG_MACHINE_LOCATION, True);
        end;
        if Result then
        begin
          WriteString(Name, Value); { Write Registry for Global Environment }
          { Update Current Process Environment Variable }
          SetEnvironmentVariable(PWideChar(Name), PWideChar(NewVal));
        end;
      finally
        Free;
      end;
  end; { SetGlobalEnvironment }
 
begin
 
  lv_Prepend := (ip_ExMode = 'Prepend');
 
  lv_EASY_API_LOG := gv_User.MoLLogDir + 'easycDLL.log';
  Need_BROADCAST := (Trim(lv_EASY_API_LOG) > '') and lf_SetEnvVar('EASY_API_LOG', lv_EASY_API_LOG) or Need_BROADCAST;
 
  if Need_BROADCAST then
    PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, Integer(PAnsiChar('Environment')));
 
end;
 
Eitan Arbel

Posts: 508
Registered: 2/24/13
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 4:55 AM   in response to: Zane Leo in response to: Zane Leo
just a thought : could it be something with the AllowMulitpleSessionsPerUser if it's True ?
Zane Leo

Posts: 55
Registered: 12/29/09
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 7:37 AM   in response to: Eitan Arbel in response to: Eitan Arbel
Eitan Arbel wrote:
just a thought : could it be something with the AllowMulitpleSessionsPerUser if it's True ?

AllowMulitpleSessionsPerUser is False for this application.
Chad Hower

Posts: 613
Registered: 3/2/07
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 8:26 AM   in response to: Zane Leo in response to: Zane Leo
On 3/31/2017 3:33 AM, Zane Leo wrote:
Has anyone used environment variables within an IW application?

Here is the issue. I must integrate with a government provided JAVA
based API to communicate with a government portal. One of the setting
is a "user based" environment variable used by the API to create per
user (read IW session) report files in a folder specified by that
"user based" environment variable.

Environment variables are global to a Windows user and no way that I can
think of to do what you want without involving a lot of windows user
accounts + a lot of complexity.

Is the JAVA API called via a command line? If so there maybe a way.
Zane Leo

Posts: 55
Registered: 12/29/09
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 8:45 AM   in response to: Chad Hower in response to: Chad Hower
Chad Hower wrote:
On 3/31/2017 3:33 AM, Zane Leo wrote:
Has anyone used environment variables within an IW application?

Here is the issue. I must integrate with a government provided JAVA
based API to communicate with a government portal. One of the setting
is a "user based" environment variable used by the API to create per
user (read IW session) report files in a folder specified by that
"user based" environment variable.

Environment variables are global to a Windows user and no way that I can
think of to do what you want without involving a lot of windows user
accounts + a lot of complexity.

Is the JAVA API called via a command line? If so there maybe a way.

Thanks for confirming my experience.. I don't know if I am happy or sad.

I will investigate your command line suggestion but how will I do that from within IW?

Thinking aloud, I suppose could create a console app as a DLL - that may work.
Chad Hower

Posts: 613
Registered: 3/2/07
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 3:07 PM   in response to: Zane Leo in response to: Zane Leo
On 3/31/2017 11:45 AM, Zane Leo wrote:
I will investigate your command line suggestion but how will I do that from within IW?

CreateProcess. I think you can pass env variables that are process
specific. So by wrapping it or using it as a command line, each instance
could have unique en variables.
Chad Hower

Posts: 613
Registered: 3/2/07
Re: Session / Thread isolated environment variable  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 31, 2017 3:06 PM   in response to: Chad Hower in response to: Chad Hower
On 3/31/2017 11:26 AM, Chad Hower wrote:
Is the JAVA API called via a command line? If so there maybe a way.

I think I might know a way - but I need to know how you call the Java API.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02