Greg Dolley’s Weblog

A Blog about Graphics Programming, Game Programming, Tips and Tricks

Archive for January 10th, 2008

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()); // OK

    Console::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.

Posted in General Programming, Tips and Tricks | 5 Comments »