Bonjour à tous,

Je suis entrain de m’arracher les cheveux pour utiliser des fonctions définies dans une api C, et je me trouve coincé par le problème suivant :

Dans mon Api j’ai une fonction qui a le prototype suivant :
Code C : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**************** Mon fichier .H********************/
Int maFonction  (StructRequest *pRequest, StructReponse *pResponse);
typedef struct
{
	SWCHAR	*pData;
	SWCHAR	Addresse [256+1];
	SWCHAR	City [100+1];
	SWCHAR	Country [100+1];
	SWCHAR	Format [50+1];
	SWCHAR	*pContext;
	int		iAlert;
	int		iRank;
} StructRequest, *pStructRequest;
 
typedef struct
{
	int				iLastErrorCode;
	SWCHAR			LastError [1024+1];
	int				iNbReport;
	int				iDetectionId;
	StructReport		*pReport;
} StructReponse, *pStructReponse;
 
 
typedef struct
{
	int					iRank;
	SWCHAR				*pData;
	SWCHAR				ListName [256+1];
	int					iEntityId;
	SWCHAR				Remark [1024+1];
	SWCHAR				Program [128+1];
	int					iLine;
	SWCHAR				Title [128+1];
	int					iNbEntity;
	StructReportEntity		*pEntity;
} StructReport, *pStructReport;
 
typedef struct 
{
	SWCHAR	NameType [32+1];
	SWCHAR	Name [256+1];
} StructReportEntity, *pStructReportEntity;
 
/**************** fin du .H**************************/

Dans mon programme C# je procède comme suit


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
internal class CS_StructReportEntity
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32 + 1)]
    public string NameType;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256 + 1)]
    public string Name;
}
 
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
internal class CS_StructReport
{
    public int iRank;
    public string pData;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256 + 1)]
    public string ListName;
    public int iEntityId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024 + 1)]
    public string Remark;
    [MarshalAs(UnmanagedType.LPWStr, SizeConst = 128 + 1)]
    public string Program;
    public int iLine;
 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128 + 1)]
    public string Title;
    public int iNbEntity;
    [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Struct)] // c'est ici où ca coince ??
    public CS_StructReportEntity[] pEntity;
}
 
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
internal class CS_StructReponse
{
    public int iLastErrorCode;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024 + 1)]
    public string LastError;
    public int iNbReport;
    public int iDetectionId;
    [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Struct)]
    public CS_StructReport[] pReport;
}
 
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
internal class CS_StructRequest
{
 
    public string pData;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256 + 1)]
    public string Addresse;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100 + 1)]
    public string City;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100 + 1)]
    public string Country;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50 + 1)]
    public string Format;
    public string pContext;
    public int iAlert;
    public int iRank;
}
 
 
 
    [DllImport(_CheminAPI)]
    private static extern void maFonction(IntPtr pRequest, IntPtr pResponse);
    public static int CS_maFonction(ref CS_StructReponse Request,
                                                   ref CS_StructRequest Response)
    {
        int retour;
        IntPtr pRequest = Marshal.AllocHGlobal(Marshal.SizeOf(Request));
        IntPtr pResponse = Marshal.AllocHGlobal(Marshal.SizeOf(Response));
        try
        {
            Marshal.StructureToPtr(Request, pRequest, false);
            Marshal.StructureToPtr(Response, pResponse, false);
            retour = maFonction(pRequest, pResponse);
            if (pRequest != IntPtr.Zero)
            {
                Marshal.PtrToStructure(pRequest, Request);
            }
            else
            {
                //code
            }
            if (pResponse != IntPtr.Zero)
            {
                Marshal.PtrToStructure(pResponse, Response);
            }
            else
            {
                //code
            }
        }
        catch (System.Exception ex)
        {
            //gestion de l’exception            
        }
        finally
        {
            Marshal.FreeHGlobal(pRequest);
            Marshal.FreeHGlobal(pResponse);
 
        }
        return retour;
    }
Malheuresement ceci ne fonctionne pas avez-vous une idée ? Merci