Domanda

La mia prima domanda qui :)

Sto lavorando con un'applicazione scritta in C ++ (un editor di mappe per un gioco) che ha l'interfaccia utente front -end scritta in C#. Dal momento che sono nuovo in C# sto cercando di fare il più possibile dal lato C ++.

Da C# voglio chiamare una funzione C ++ che restituirà un elenco di strutture con semplici tipi variabili (int e stringa) in modo da poter popolare una casella di elenco che ho nell'interfaccia utente con loro. È possibile? Come dovrei scrivere la funzione di importazione DLL in C#?

Ho provato a cercare qui la risposta, ma ho trovato solo post su come passare gli elenchi da C# a C ++.

Il codice C ++:

struct PropData
{
PropData( const std::string aName, const int aId )
{
    myName = aName;
    myID = aId;
}

std::string myName;
int myID;
};

extern "C" _declspec(dllexport) std::vector<PropData> _stdcall GetPropData()
{
std::vector<PropData> myProps;

myProps.push_back( PropData("Bush", 0) );
myProps.push_back( PropData("Tree", 1) );
myProps.push_back( PropData("Rock", 2) );
myProps.push_back( PropData("Shroom", 3) );

return myProps;
}

La funzione di importazione C#:

    [DllImport("MapEditor.dll")]
    static extern ??? GetPropData();

MODIFICARE:

Dopo il post da ED S. ho modificato il codice C ++ in Struttura propData {propData (const std :: string aname, const int aid) {myname = aname; myid = aiuti; }

    std::string myName;
    int myID;
};

extern "C" _declspec(dllexport) PropData* _stdcall GetPropData()
{
    std::vector<PropData> myProps;

    myProps.push_back( PropData("Bush", 0) );
    myProps.push_back( PropData("Tree", 1) );
    myProps.push_back( PropData("Rock", 2) );
    myProps.push_back( PropData("Shroom", 3) );

    return &myProps[0];
}

e il c# a [dllimport ("mapeditor.dll")] propdata esterno statico getPropData ();

    struct PropData
    {
        string myName;
        int myID;
    }

    private void GetPropDataFromEditor()
    {
        List<PropData> myProps = GetPropData();
    }

Ma ovviamente questo non si compila come getPropData () non restituisce nulla che si traduca in un elenco.

Grazie mille Ed S. per avermi portato così lontano!

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top