0

What I would like to do is call an unmanaged C++ function from managed C++:

int runConfig(int argc, char* argv[])

public: static bool CreateConfigurationFile(int parameters, array<String^>^ arguments)

My question is how do I convert a managed array of managed strings into an unmanaged array of char*?

4

1 回答 1

1

你可以使用这样的东西:

using namespace System;
using namespace System::Runtime::InteropServices;

bool CreateConfigurationFile(int parameters, array<String^>^ arguments)
{
    int length = arguments->Length;
    char** argv = new char*[length];

    for(int i = 0; i < length; ++i)
    {
        argv[i] = (char*)Marshal::StringToHGlobalAnsi(arguments[i]).ToPointer();
    }

    runConfig(length, argv);

    for(int i = 0; i < length; ++i)
    {
        Marshal::FreeHGlobal(IntPtr(argv[i]));
    }

    delete[] argv;

    return true;
}

请注意int parameters未使用,因为 .NET 数组具有Length属性。

如果要runConfig使用 PInvoke 调用,请使用以下 PInvoke 声明:

int runConfig(int argc, IntPtr argv);
于 2013-06-04T13:16:08.917 回答