Página 1 de 1

GETENV não está lendo variável

Enviado: 03 Jun 2016 08:02
por rubens
Bom dia...

Estou tentando ler uma variável com o GETENV() mas está retornando vazio. O que estou fazendo de errado?

Seto a variável
SETDIRORC.png
SETDIRORC.png (9.62 KiB) Exibido 4181 vezes
Quando executo o programa e executo GETENV('DIRORC') retorna vazio
GETENV.png
Eu executo o programa logo após definir o set dirorc=d:\orcame\
Já tentei também criar um bat e setar a variável e executar o programa
Quando busco qualquer variável do sistema tipo GETENV('PATH') funciona.

Obrigado
Rubens

GETENV não está lendo variável

Enviado: 03 Jun 2016 08:40
por Jairo Maia
Olá Rubens,

Isso é porque seu sistema está rodando em instância diferente da instância que foi definida a variável de ambiente DIRORC.

Por exemplo: Se você sair para o prompt do DOS e compilar o exemplo abaixo, em seguida setar a variável DIRORC, e rodar o exemplo ela retornará correta, pois o exemplo foi rodado na mesma instância que foi definida a variável.

O mesmo problema ocorre quando você usa o .BAT, pois o BAT abre uma nova instância para ser executado. E curiosidade, não resolve se usar: Hb_Run( "SET DIRORC=D:\ORCAME" ), já que o HB_Run() também abre outra instância para executar o comando.

Código: Selecionar todos

Function Main()

 Hb_Alert( GetEnv( "DIRORC" ) )
 
Return Nil

GETENV não está lendo variável

Enviado: 03 Jun 2016 09:48
por asimoes
Olá,

Setar variável de ambiente em tempo de execução

Código: Selecionar todos

SetEnvironmentVariable("TESTE", "BRAZILIAN PORTUGUESE_BRAZIL.WE8PC850")
cTeste:=hb_GetEnv("TESTE")
#pragma BEGINDUMP
#include <windows.h>
#include <shlobj.h>
#include <math.h>
#include "hbapi.h"
#include "hbvm.h"
#include "hbstack.h"
#include "hbapiitm.h"

HB_FUNC( SETENVIRONMENTVARIABLE )
{
   hb_retl( SetEnvironmentVariableA( (LPCSTR) hb_parcx( 1 ),
                                     (LPCSTR) hb_parcx( 2 )
                                     ) ) ;
}
#pragma ENDDUMP

GETENV não está lendo variável

Enviado: 03 Jun 2016 10:19
por asimoes
Pegar todas as variáveis de ambiente do sistema


Código: Selecionar todos

aEnv := hb_ATokens( EnvParam(), hb_Eol() )

#pragma BEGINDUMP

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <hbapi.h>

char * LToStr( long );

HB_FUNC( ENVPARAM )
{
    LPTSTR lpszVariable;
    LPTCH lpvEnv;
    char * pszBuffer = NULL;
    int iLastLen = 0;
   
    // Get a pointer to the environment block.
    lpvEnv = GetEnvironmentStrings();

    // If the returned pointer is NULL, exit.
    if (lpvEnv == NULL)
    {
        printf("GetEnvironmentStrings failed (%d)\n", GetLastError());
        return ;
    }

    // Variable strings are separated by NULL byte, and the block is
    // terminated by a NULL byte.
    lpszVariable = (LPTSTR) lpvEnv;

    while (*lpszVariable)
    {
          char * newBuffer;
          int iLen = lstrlen( lpszVariable );
        pszBuffer = hb_xrealloc( pszBuffer, iLastLen + iLen + 2 );        
        hb_xmemcpy( pszBuffer+iLastLen, lpszVariable, iLen );
        hb_xmemcpy( pszBuffer+iLastLen+iLen, "\r\n", 2 );
        iLastLen += iLen + 2;        
        lpszVariable += lstrlen(lpszVariable) + 1;
       
    }
    FreeEnvironmentStrings(lpvEnv);
   
    hb_retc( pszBuffer );
}

#pragma ENDDUMP

GETENV não está lendo variável

Enviado: 03 Jun 2016 13:16
por JoséQuintas
Já disseram, em todo caso:

Está criando a variável de ambiente na janela do prompt atual.
Seu programa está sendo executado na janela do prompt atual?
d:\temp>copy con test.prg
? getenv( "TEST" )
Substituir test.prg? (Sim/Não/Todos): s
^Z
1 arquivo(s) copiado(s).

d:\temp>hbmk2 test
hbmk2: Processing environment options: -comp=msvc
Harbour 3.4.0dev (34b404d) (2016-05-30 12:33)
Copyright (c) 1999-2016, https://github.com/vszakats/harbour-core/
Compiling 'test.prg'...
Lines 1, Functions/Procedures 1
Generating C source output to 'C:\Users\joseq\AppData\Local\Temp\hbmk_7lfk3t.dir\test.c'... Done.
test.c

d:\temp>test


d:\temp>SET TEST=XXXX

d:\temp>test

XXXX
d:\temp>
Nota: lembre-se que mesmo em outra janela de prompt ela não vai aparecer, muito menos em janela Windows.

GETENV não está lendo variável

Enviado: 14 Mar 2020 22:24
por syslink
asimoes escreveu:Pegar todas as variáveis de ambiente do sistema
uauuuu
asimoes escreveu:Setar variável de ambiente em tempo de execução
esse foi fera... agora q descobri por acaso ao pesquisar sobre getenv...
isso funciona so no autoexec.bat .nt ou da pra fazer tbm nas variaveis de ambiente do windows (vista/7/810)