[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

How to get the defined value for user environment variables?

Hi All,
      I would be facing the value in getting the defined value for user environment variables..
  I used ExpandEnvironmentStringsForUser(),which is a shell API...
Platform is .NET 2003(VC++.NET),Windows Xp...
  getting error like " error C2039: 'ExpandEnvironmentStringsForUser' : is not a member of 'operator``global namespace'''

I have included UserEnv.h file and also added UserEnv.lib file...

Need suggestion very badly
Thanks in Advance,Deepa.
0
bhavana75
Asked:
bhavana75
  • 4
  • 3
  • 2
1 Solution
 
jkrCommented:
Are you using 'C++.NET' (aka 'managed C++')? If so, see http://msdn.microsoft.com/library/en-us/dndotnet/html/win32map.asp ("Microsoft Win32 to Microsoft .NET Framework API Map")

'ExpandEnvironmentStringsForUser()' maps to:

Microsoft.Win32.Registry class
Microsoft.Win32.RegistryKey.OpenSubKey(name).GetValueNames, and then call GetValue for each name
0
 
bhavana75Author Commented:
Thanks for your response....I would need the win32 APIs only...Eventhough my platform is VC++.NET..Just explain me how to use  ExpandEnvironmentStringsForUser() function...
Thanks,Deepa.
0
 
AlexFMCommented:
Since ExpandEnvironmentStringsForUser is included in Windows XP and Windows 2000 Professional, you need to add precompiler constants before including of Userenv.h, as described here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/using_the_windows_headers.asp

Try this:

#define _WIN32_WINNT 0x0500
#include <Userenv.h>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bhavana75Author Commented:
tried with what you said ...But there is no luck Alex.....
still getting the error as
error C3861: 'ExpandEnvironmentStringsForUser': identifier not found, even with argument-dependent lookup...

Thanks in Advance,deepa.
0
 
jkrCommented:
Actually,

#define _WIN32_WINNT 0x0500
#include <Userenv.h>

is incorrect - that should be

#define WINVER 0x0500 // <---------------!
#include <Userenv.h>

The declaration in userenv.h is

#if(WINVER >= 0x0500)

USERENVAPI
BOOL
WINAPI
ExpandEnvironmentStringsForUserA(
    IN HANDLE hToken,
    IN LPCSTR lpSrc,
    OUT LPSTR lpDest,
    IN DWORD dwSize);
USERENVAPI
BOOL
WINAPI
ExpandEnvironmentStringsForUserW(
    IN HANDLE hToken,
    IN LPCWSTR lpSrc,
    OUT LPWSTR lpDest,
    IN DWORD dwSize);
#ifdef UNICODE
#define ExpandEnvironmentStringsForUser  ExpandEnvironmentStringsForUserW
#else
#define ExpandEnvironmentStringsForUser  ExpandEnvironmentStringsForUserA
#endif // !UNICODE

#endif /* WINVER >= 0x0500 */
0
 
AlexFMCommented:
WINVER problem is usually result of working without SDK.
0
 
bhavana75Author Commented:
Hi Jkr,
    I have implemented the way u suggested..It started working...
Thanks ...
Does this function works only for windows versions which are higher than NT & later or for other windows versions also like win98,me,etc..
Thanks,Deepa.
0
 
bhavana75Author Commented:
Hi Alex,
>>WINVER problem is usually result of working without SDK...
  I didnt understand exactly what working without SDK...
Thanks,Deepa.
0
 
AlexFMCommented:
WINVER is defined in SDK headers to the latest Windows version. If there are some problems with WINVER, this usually means that SDK is not installed or it's headers are not used by VC++ compiler.
SDK can be downloaded from Microsoft WEB site:
http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5&displaylang=en

In the list of VC++ include directories (Tools - Options - Directories - Include files) SDK include directory should be listed on the first place, before VC++ include directories. The same for library directories.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now