Search code examples
visual-c++c++-cliclr

C++ CLR Console Application [Cant take adress of Directory::GetCurrentDirectory C3374]


I have simple code:

msclr::interop::marshal_context ctx;
System::String ^converted = 
    msclr::interop::marshal_as<String^>(Directory::GetCurrentDirectory);
array<String^>^ files = 
    Directory::GetFiles(converted, "*.cpp", System::IO::SearchOption::AllDirectories);

I'm just trying to get current directory and then get a files in that folder, and all subfolders. But I'm getting the following error:

Error C3374 can't take address of 'System::IO::Directory::GetCurrentDirectory' unless creating delegate instance


Solution

  • I'm not sure why you need to try to marshal the method like that. Try calling it directly:

    System::String^ converted = Directory::GetCurrentDirectory();
    array<String^>^ files = 
        Directory::GetFiles(converted, "*.cpp", System::IO::SearchOption::AllDirectories);
    
    • Directory::GetCurrentDirectory already returns a System.String^ so you don't need to marshal or convert it.
    • The marshal_as function is used to convert managed types to unmanaged types, not cast values.
    • The error you are getting comes from not invoking Directory::GetCurrentDirectory with parentheses like this: Directory::GetCurrentDirectory().