Appel DLL C++ avec tableau +in/out

Résolu
babe59 Messages postés 189 Date d'inscription vendredi 28 mai 2004 Statut Membre Dernière intervention 27 novembre 2015 - 20 oct. 2008 à 12:36
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 - 20 oct. 2008 à 14:24
Bonjour,

Je dois appeler un méthode d'une DLL C++  (xxx_G) dans mon programme C#. Cette méthode possède un structure d'entrée/sortie en argument. J'ai trouvé le principe mais il me manque quelques infos... Pour m'aider, j'ai un exemple de programme en C++ qui appelle cette DLL. Pourriez vous m'aider à faire l'appel de cette DLL Merci

Voici le source C++

Header_G.h
#ifndef

HEADER_G_H_

#define HEADER_G_H_

typedef
struct
{

float H;

float B;

float T[3];

short TYPE

char  TYPE2

short RET[12][3];

} inoutG, *lpinoutG;

extern
"C"
void XXX_G(lpinoutG t);

extern
"C"
void trace();

#endif
/*HEADER_G_H_*/
le programme de test C++
...
int main()
{
   lpinoutGOP2 param;
  
param->H = 30.0;
   param->B = 2500.0;

   param->T[0] = 30.0;

   param->T[1] = 31.0;

   param->T[2] = 32.0;

   ...
   xxx_G(param);

...

Voici mon programme C# de test

[

StructLayout (
LayoutKind.Sequential)]

public
struct
TPARAM
{
[
MarshalAs(
UnmanagedType.R4)]
public
float H;

[
MarshalAs(
UnmanagedType.R4)]
public
float B;

[
MarshalAs(
UnmanagedType.LPArray)]
public
float[] T;

[
MarshalAs(
UnmanagedType.I2)]
public
short TYPE;

[
MarshalAs(
UnmanagedType.AnsiBStr)]
public
char TYPE2;

[
MarshalAs(
UnmanagedType.LPArray)]
public
short[,] RETOUR;

}

[
DllImport(
"xxx_G.dll")]

public
static
extern
void PTD_GOP2(
[
MarshalAs(
Unmana
gedType.LPStruct)]
ref
TPARAM param);
Public Form1 ()
{
xxx_G (ref param)
}

DT

1 réponse

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
20 oct. 2008 à 14:24
Salut, il ne me semble pas possible de passer le dernier paramètre sous forme de tableau à 2 dimensions, tu pourras par exemple utiliser "la notation tableau" avec un pointeur fixé sur le tableau "RET".

[ StructLayout( LayoutKind.Sequential ) ]
private struct InOutG
{
    public float H;
    public float B;
    [ MarshalAs( UnmanagedType.ByValArray, SizeConst = 3 ) ]
    public float[ ] T;
    public short TYPE;
    public byte TYPE2;
    [ MarshalAs( UnmanagedType.ByValArray, SizeConst = 12*3 ) ]
    public short[ ] RET;
}


[ DllImport( "xxx_G.dll" ) ]
private static extern void XXX_G( ref InOutG t );
3
Rejoignez-nous