Как я могу передать структуру со строками из C ++ в C# - PullRequest
0 голосов
/ 04 мая 2020

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

#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;
    }

Соответствующий код C#

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 ();

        }
    }
}

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...