Du C/C++ au C#

Résolu
louvinon Messages postés 6 Date d'inscription jeudi 10 septembre 2009 Statut Membre Dernière intervention 10 septembre 2009 - 15 janv. 2008 à 15:25
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 - 15 janv. 2008 à 21:47
Bonjour,

on me fournit une DLL faites en C++/C.

En C++, j'ai ceci :



typedef struct
{
 UCHAR cnv_name [8];
 ULONG lgd_utcc;
 ULONG lgd_phys;
 ULONG lgd_maxi;
 UCHAR cnv_retc[2];
 UCHAR cnv_info[2];
 ULONG cnv_errp;
} CnvParm1;


//---------------------------------------------------------------------
// Prototype Appel Fonction UTCNVFCT                                  
//---------------------------------------------------------------------
ULONG Utcnvfct (CnvParm1* prm, char* buf_utcc, char* buf_phys);






Maintenant, je souhaite l'utiliser dans C#.
Pour l'utiliser à partir de C# :




[ DllImport( s_UtiPInvoke )]
private static extern uint Utcnvfct (CNVPARM1 prm, StringBuilder buf_utcc, StringBuilder buf_extd);
// StringBuilder car ce sont des strings en entrée/sortie


[ StructLayout( LayoutKind.Sequential)]
public struct CNVPARM1

  [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 )]
 public string cnv_name;
  [ MarshalAs( UnmanagedType.U8 )]
 public uint  lgd_utcc;
 [ MarshalAs( UnmanagedType.U8 )]
 public uint lgd_phys;
 [ MarshalAs( UnmanagedType.U8 )]
 public uint  lgd_maxi;
 [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]
 public string cnv_retc;
 [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]
 public string cnv_info;
 [ MarshalAs( UnmanagedType.U8 )]
 public uint cnv_errp;
}

 J'ai mis uint mais ceci déclenche une erreur (System.TypeLoadException: Cannot marshal field 'lgd_utcc' of type 'eid.o500.uti.CNVPARM1': Invalid managed/unmanaged type combination (Int32/UInt32 must be paired with I4, U4, or Error) donc j'ai à la place mis ulong et ca n'émet plus d'exception.

 Tout ceci vous paraît-il convenable ?  

3 réponses

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
15 janv. 2008 à 21:47
Ca me semble correct, précise CharSet.Ansi dans DllImport et StructLayout.
3
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
15 janv. 2008 à 17:00
Salut, ULONG dans ton code C++ ne fait que 32 bits.. respect aussi la taille de tes chaines C++ cnv_name devrait avoir un SizeConst de 8.
0
louvinon Messages postés 6 Date d'inscription jeudi 10 septembre 2009 Statut Membre Dernière intervention 10 septembre 2009
15 janv. 2008 à 17:21
Lutino, suites à tes recommandations, voici je que je fais :

[ StructLayout( LayoutKind.Sequential)]
public struct CNVPARM1

  [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=8 )]
 public string cnv_name;
  [ MarshalAs( UnmanagedType.U4 )]
 public uint  lgd_utcc;
 [ MarshalAs( UnmanagedType.U4 )]
 public uint lgd_phys;
 [ MarshalAs( UnmanagedType.U4 )]
 public uint  lgd_maxi;
 [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=2)]
 public string cnv_retc;
 [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=2 )]
 public string cnv_info;
 [ MarshalAs( UnmanagedType.U4 )]
 public uint cnv_errp;
}

Ceci est correct ?
0
Rejoignez-nous