LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

sdk GetEnvironmentVariable() question

I'm currently trying to read the value of environment variables via sdk function GetEnvironmentVariable() - here is a code snippet:

    LPTSTR Path = ""; LPCTSTR Var = "PATH";
    int  P = 0, L;

    L = sizeof(Path);
    P = GetEnvironmentVariable(Var,  Path, L);

- problem is, that 'Path' is always empty, whereas P, the return value, is the actual length of my %PATH% env variable.
anyone can give me a clue what's wrong here ?


--
Once the game is over, the king and the pawn go back into the same box.
0 Kudos
Message 1 of 3
(3,476 Views)

It seems that the problem in your code is in the size of the buffer passed to the function: nSize> parameter must be passed with the lenght of the (already) allocated buffer to hold the string. According to SDK documentation, "If the buffer pointed to by lpBuffer is not large enough, the return value is the buffer size, in characters, required to hold the value string and its terminating null character. " which seems to be your case. Try this in the interactive execution window:

#include "windows.h"
#include <utility.h>

static int  p;
static char path[1024];

// DWORD GetEnvironmentVariable (
// LPCTSTR lpName,  // environment variable name
// LPTSTR lpBuffer, // buffer for variable value
// DWORD nSize      // size of buffer
//);
p = GetEnvironmentVariable ("Path", path, 1024);
DebugPrintf ("Returned bytes: %d\n", p);
DebugPrintf ("Returned string: %s\n", path);



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(3,472 Views)
I think you're right, Roberto,
just tried your approach and it works 🙂
- I had thought that it would be just ok to declare the return buffer as LPTSTR as mentioned in the SDK help, and initialize it as an empty string.
obviously, that was wrong.
Thanks.
Werner

--
Once the game is over, the king and the pawn go back into the same box.
0 Kudos
Message 3 of 3
(3,468 Views)