Question

Ma première question ici :)

Je travaille avec une application écrite en C ++ (un éditeur de carte pour un jeu) qui a l'interface utilisateur frontale écrite en C #. Puisque je suis nouveau dans C #, j'essaie de faire autant que possible du côté C ++.

De C #, je veux appeler une fonction C ++ qui renverra une liste de structures avec des types de variables simples (int et string) afin que je puisse remplir une boîte de liste que j'ai dans l'interface utilisateur avec eux. Est-ce possible? Comment dois-je écrire la fonction d'importation DLL en C #?

J'ai essayé de rechercher ici la réponse, mais je n'ai trouvé que des articles sur la façon de passer des listes de C # à C ++.

Le code 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 fonction d'importation C #:

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

ÉDITER:

Après le post d'Ed S., j'ai changé le code C ++ en struct propdata {propData (const std :: string aname, const int aide) {myName = aname; MyID = AID; }

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

et le C # à [dLLIMPORT ("MapEditor.dll")] Propdata extern statique getPropData ();

    struct PropData
    {
        string myName;
        int myID;
    }

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

Mais bien sûr, cela ne se compile pas car getPropData () ne renvoie rien qui se traduit par une liste.

Merci beaucoup Ed S. de m'avoir fait aussi loin!

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top