2010-12-08 6 views
2

Je suis coincé dans le côté de l'implémentation de C#, car je suis assez nouveau dans ce domaine. La chose est, je veux passer un 'pointeur' (ayant de la mémoire) à partir du code C# afin que mon application C++ puisse copier le tampon pchListSoftwares dans pchInstalledSoftwares. Je ne suis pas capable de comprendre comment passer le pointeur du côté C#.Pointeur char passant de la fonction C# à la fonction C++

C++ code natif (MyNativeC++ DLL.dll)

void GetInstalledSoftwares(char* pchInstalledSoftwares){ 
    char* pchListSoftwares = NULL; 
    ..... 
    ..... 
    pchListSoftwares = (char*) malloc(255); 

    /* code to fill pchListSoftwares buffer*/ 

    memcpy(pchInstalledSoftwares, pchListSoftwares, 255); 

    free(pchListSoftwares); 

} 

Passant 'string' simple, ne fonctionne pas ...

C# mise en œuvre

[DllImport("MyNativeC++DLL.dll")] 
private static extern int GetInstalledSoftwares(string pchInstalledSoftwares); 


static void Main(string[] args) 
{ 
......... 
......... 
     string b = ""; 
     GetInstalledSoftwares(0, b); 
     MessageBox.Show(b.ToString()); 
} 

Toute aide est grandement apprécié ...

Répondre

2

Essayez d'utiliser un StringBuilder

[DllImport("MyNativeC++DLL.dll")] 
private static extern int GetInstalledSoftwares(StringBuilder pchInstalledSoftwares); 


static void Main(string[] args) 
{ 
......... 
......... 
     StringBuilder b = new StringBuilder(255); 
     GetInstalledSoftwares(0, b); 
     MessageBox.Show(b.ToString()); 
} 
1

Mon erreur ... supprimer 0 dans l'appel à GetInstalledSoftwares(0, b); .

+0

Bon travail (le découvrir) –

0

Essayez de changer la ligne prototype à:

private static extern int GetInstalledSoftwares(ref string pchInstalledSoftwares); 

(Envoyer la chaîne par référence).

Questions connexes