I return a string array from C++ function and want to use this string array in C#. When i try to get the array from C# it gives me this error:
System.Runtime.InteropServices.MarshalDirectiveException: "Cannot marshal 'return value': Invalid managed/unmanaged type
Here is my C++ code:
Header
#pragma once
#ifdef CRITELAB_EXPORTS
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
#include <string>
extern "C" API std::string* gatherInfos();
CPP
std::string* gatherInfos()
{
std::string username;
std::string password;
puts("Please enter a username:");
fflush(stdout);
std::cin >> username;
std::cout << std::endl;
puts("Please enter a password:");
fflush(stdout);
std::cin >> password;
std::string* const gatheredInfos = new std::string[2];
gatheredInfos[0] = username;
gatheredInfos[1] = password;
return gatheredInfos;
}
And here is my C# code
using System;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
string[] gatheredInfos = gatherInfos();
Console.ReadLine();
}
[DllImport("critelab.dll")]
public static extern int connectToDB();
[DllImport("critelab.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern string[] gatherInfos();
}
}