How to Fix – "C2039: ‘GetCurrentDirectoryA()’ : is Not a Member of ‘System::IO::Directory’"
Posted by gregd1024 on January 10, 2008
Have you ever received either one of these annoying error messages from the MSVC++ 9.0 compiler?
“C2039: ‘GetCurrentDirectoryW’ : is not a member of ‘System::IO::Directory'”
“C2039: ‘GetCurrentDirectoryA’ : is not a member of ‘System::IO::Directory'”
You’ll get this error when converting a native C++ project to a MC++ (managed) project and somewhere in your code there is a call to Directory::GetCurrentDirectory(). Below is a very simple example:
#include “stdafx.h”
#include <windows.h>
using namespace System;
using namespace System::IO;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L”Dir: “+Directory::GetCurrentDirectory()); // Error!
Console::ReadKey();
return 0;
}
The reason for the error is that “GetCurrentDirectory” is also defined as a macro in windows.h. The macro expands to “GetCurrentDirectoryA” or “GetCurrentDirectoryW.” So in the example above, the compiler actually sees “Directory::GetCurrentDirectoryW” which obviously isn’t defined in the “Directory” class.
The fix is simple – just “#undef” the macro:
#include “stdafx.h”
#include <windows.h>
using namespace System;
using namespace System::IO;
#undef GetCurrentDirectory
int main(array<System::String ^> ^args)
{
Console::WriteLine(L”Dir: “+Directory::GetCurrentDirectory()); // OKConsole::ReadKey();
return 0;
}
Now the compiler sees the real “GetCurrentDirectory” function and there are no compile errors. 🙂
-Greg Dolley
Subscribe via RSS here. Want email updates instead? Click here.
prateek kumar said
Thanks dude… that stuff was really cool. keep sharing similar interesting tid-bits 😀
Abhay said
Thanks a lot. Really saved an hours work for me!!! 😀
Alex said
Thanks man,
Short description, well explained fix -> perfect!
Martin said
Thanks, great tip, well explained.
Martin.
Shail said
Thanks,Good explaination and quite useful.