C# et dll C++ =(

jimbabwe Messages postés 2 Date d'inscription dimanche 3 avril 2005 Statut Membre Dernière intervention 3 avril 2005 - 3 avril 2005 à 12:48
jimbabwe Messages postés 2 Date d'inscription dimanche 3 avril 2005 Statut Membre Dernière intervention 3 avril 2005 - 3 avril 2005 à 12:51
bonjour,
voila je débute en C# et je désire utilisé une dll, dont je possede le code(ce n'est pas moi qui l'ait développé) et que je compile sous VC++ .Net 2003. Jusque la tout va bien, ensuite je désire l'exploiter en C# ...et la à chaque appel des methodes de la dll il me retourne une valeur null....je me suis laisser dire que c'est la facon dont j'avais créé mes structures qui n'allait pas bien.

voila comment la structure en C++ que j 'aimerai convertir en C# afin de passer les mêmes type d'objet à la dll...

/*********************************/
/*C++ Version */
/*********************************/
typedef enum {
GE_SUCCESS,
GE_BADARGS,
GE_INVALID,
GE_CORRUPTED,
GE_NSUP,
GE_NOTFOUND,
GE_INDEX,
GE_ERRNO,
GE_ZLIB,
GE_ZLIBFILE,
GE_NODATA,
GE_BADMODE
} GrfErrorType;


typedef struct {
GrfErrorType type;
uint32_t line;
const char *file;
const char *func;
uintptr_t *extra;
} GrfError;


typedef struct _GrfFile {
uint32_t compressed_len_aligned;
uint32_t compressed_len;
uint32_t real_len;
uint32_t pos;
uint8_t flags;
uint32_t hash;
char name[GRF_NAMELEN];
char *data;
struct _GrfFile *next;
struct _GrfFile *prev;
} GrfFile;


typedef struct {
char *filename;
uint32_t len;
uint32_t type;
uint32_t version;
uint32_t nfiles;
GrfFile *files;
GrfFile *first;
GrfFile *last;
uint8_t allowCrypt;
FILE *f;
uint8_t allowWrite;
void *zbuf;
} Grf;
typedef int (*GrfOpenCallback) (GrfFile *file, GrfError *error);
typedef int (*GrfFlushCallback) (GrfFile *file, GrfError *error);
/*********************************/
/*Fin C++ Version */
/*********************************/

/*****************************************/
/*C# Version(avec qlq appels aux methodes de la dll.*/
/*****************************************/
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct TGrfFile
{
public uint compressed_len_aligned;
public uint compressed_len;
public uint RealLen;
public uint pos;
public byte flags;
public uint hash;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]public char name;
public char[] data;
public IntPtr next;
public IntPtr prev;
}

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public class TGrf
{
public string filename;
public uint len;
public uint TheType;


public uint version;
public uint nfiles;
public TGrfFile[] files;


public TGrfFile first;
public TGrfFile last;

public byte allowCrypt;
public IntPtr f;
public byte allowWrite;
public IntPtr zbuf;
}


public enum TGrfErrorType
{
GE_SUCCESS,
GE_BADARGS,
GE_INVALID,
GE_CORRUPTED,
GE_NSUP,
GE_NOTFOUND,
GE_INDEX,
GE_ERRNO,
GE_ZLIB,
GE_ZLIBFILE,
GE_NODATA,
GE_BADMODE
}


[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public class TGrfError
{
public TGrfErrorType TheType;
public uint line;
public char[] FileName;
public char[] Func;
public IntPtr extra;
}
TGrf aGrf;
TGrfError aErr;
int TGrfOpenCallback(TGrfFile TheFile, TGrfError error){return 0;}
[DllImport("grf-priv-1.0.0.0.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
public static extern TGrf grf_callback_open(char[] fname, char[] mode, TGrfError error, 0);
[DllImport("grf-priv-1.0.0.0.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
public static extern string grf_strerror(TGrfError err);
/*********************************/
/*Fin C# Version */
/*********************************/

quelqu'un pourrait-il m'aider ...qu'est-ce qui ne va pas dans mon code C# ??

1 réponse

jimbabwe Messages postés 2 Date d'inscription dimanche 3 avril 2005 Statut Membre Dernière intervention 3 avril 2005
3 avril 2005 à 12:51
oui j'ai oublié de poster la déclaration en C++ de la méthode grf_callback_open

la voici :


GRFEXPORT Grf *grf_callback_open (
const
char *fname,
const
char *mode, GrfError *error, GrfOpenCallback callback);
0
Rejoignez-nous