Search code examples
c++.netmanaged

Building C++ CLR Managed DLL


Can you help me resolve this error:

enter image description here

The values I am passing is a type String^ and I am actually passing the values in a template class but shows an error


Solution

  • To start with it looks like you're trying to pass a managed memory 'username' to an unmanaged function.

    String^ s = gcnew String("sample string");
    IntPtr ip = Marshal::StringToHGlobalAnsi(s);
    const char* str = static_cast<const char*>(ip.ToPointer());
    
    Console::WriteLine("(managed) passing string...");
    NativeTakesAString( str );
    
    Marshal::FreeHGlobal( ip );
    

    reference: http://msdn.microsoft.com/en-us/library/22e4dash.aspx