Как я могу передать структуру с струнами от C ++ до C#C++

Программы на C++. Форум разработчиков
Anonymous
Как я могу передать структуру с струнами от C ++ до C#

Сообщение Anonymous »

У меня есть следующий код C ++ < /p>

#ifdef EXPORT_DLL
#define CfgAPI __declspec(dllexport)
#else
#define CfgAPI
#endif

struct WrkPaths
{
public:
char *WrkDir;
char *URL1;
char *URL2;
char *URL3;
};

CfgAPI int getURLFromDir(WrkPaths * pathCfg){

pathCfg->WrkDir = (char*)malloc( 5 * sizeof(char) );
strcpy( pathCfg->WrkDir , "Test");
return 0;
}
< /code>

Соответствующий код C# < /p>

using System;
using System.Runtime.InteropServices;
using System.Text;

// namespace declaration
namespace HelloWorldApp {

// Class declaration
class Geeks {

[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct WrkPaths {

[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string WrkDir;
[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string URL1;
[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string URL2;
[MarshalAs (UnmanagedType.LPStr, SizeConst = 255)]
public string URL3;
}

[DllImport ("CfgAPI.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int getRepoURLFromDir (
ref WrkPaths wPath
);

// Main Method
static void Main (string[] args) {

// statement
// printing Hello World!
Console.WriteLine ("Hello World!");

WrkPaths wPath = new WrkPaths();
int a = getRepoURLFromDir(ref wPath);

Console.WriteLine (wPath.WrkDir.ToString ());

// To prevents the screen from
// running and closing quickly

Console.ReadKey ();

}
}
}
< /code>

На экране появляется только Hello World < /code>. Как я могу перенести данные из C/C ++ DLL на C#?

Подробнее здесь: https://stackoverflow.com/questions/615 ... to-c-sharp

Вернуться в «C++»