Click to See Complete Forum and Search --> : error C2039: 'GetEnvironmentVariableA' : is not a member of 'System::Environment'


troelskn
September 21st, 2005, 07:41 AM
Hello

I'm trying to compile a program written in c++ .NET. I have previously managed to compile it as a console application, but I want it to run as a windowed app. I am now getting odd errors, which I don't understand.

I compile with :

cl /clr MyApp.cpp /link /subsystem:windows


And the error I recieve is :

C:\src\MyApp>cl /clr MyApp.cpp /link /subsystem:windows
Microsoft (R) C/C++ Optimizing Compiler Version 13.10.3077 for .NET Framework
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

MyApp.cpp
MyApp.cpp(163) : error C2039: 'GetEnvironmentVariableA' : is not a member of 'System::Environment'
MyApp.cpp(18) : see declaration of 'System::Environment'
MyApp.cpp(163) : error C2660: 'GetEnvironmentVariableA' : function does not take 1 arguments
MyApp.cpp(178) : error C2039: 'GetEnvironmentVariableA' : is not a member of 'System::Environment'
MyApp.cpp(18) : see declaration of 'System::Environment'
MyApp.cpp(178) : error C2660: 'GetEnvironmentVariableA' : function does not take 1 arguments


The offending lines are theese:
(#18)

#using <mscorlib.dll>

(#163)

String* sDirSpybot = String::Concat(Environment::GetEnvironmentVariable(S"ALLUSERSPROFILE"), "\\Application Data\\Spybot - Search & Destroy\\Logs");

(#178)

sComputerName = Environment::GetEnvironmentVariable(S"COMPUTERNAME");


It appears to me as if the problem stems from linking with mscorlib.dll, but I got no idea how to fix it ?

troelskn
September 21st, 2005, 08:29 AM
I found this post :
http://www.thescripts.com/forum/thread137134.html

And applied the same workaround to my code :

#using <mscorlib.dll>
#undef GetEnvironmentVariable


It works, but I got absolutely no idea why or what the problem was in the first place. It feels like I'm doing something I shouldn't.

Any explanations appreciated ?

Alex F
September 21st, 2005, 10:05 AM
Windows API which have string parameters are kept in two versions in libraries. For example, there is no function GetEnvironmentVariable, there are two functions: GetEnvironmentVariableA and GetEnvironmentVariableW. Depending on project configuration, GetEnvironmentVariable macro maps this name to GetEnvironmentVariableA or GetEnvironmentVariableW:


#ifdef _UNICODE
#define GetEnvironmentVariable GetEnvironmentVariableW
#else
#define GetEnvironmentVariable GetEnvironmentVariableA
#endif


If you add Win32 include files (for example, windows.h) to managed cpp file, preprocessor converts GetEnvironmentVariable to GetEnvironmentVariableA or GetEnvironmentVariableW. Compiler tries to compile:

String* sDirSpybot = String::Concat(Environment::GetEnvironmentVariableA(S"ALLUSERSPROFILE")...

and shows error message. #undef solves the problem. Do you have some specific reason to add Win32 #include files to managed project?

troelskn
September 21st, 2005, 10:44 AM
Do you have some specific reason to add Win32 #include files to managed project?
Thanks for the explanation. No I don't - except I don't know what I'm doing ;)
I basically have a working console application, written in managed c++, and I want to compile it as a non-console application (subsystem:windows). It produces no output - it's just meant to run in the background.
I replaced _tmain() with int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow), added #include <windows.h> and finally added the /subsystem:windows option to cl. Should I have done something else ?

ahoodin
September 21st, 2005, 10:52 AM
Prob. best to post this in Managed C++ forum.

ahoodin

Alex F
September 21st, 2005, 12:38 PM
>> added #include <windows.h>
Why do you need this? This #include is required to call unmanaged functions, remove it.
I think it is better to create new Windows application and move existing code to it.

troelskn
September 22nd, 2005, 03:34 AM
>> added #include <windows.h>
Why do you need this? This #include is required to call unmanaged functions, remove it.

Indeed yes. Removing that line made the difference. Thanks.