Je rencontre des problèmes pour appeler une fonction C depuis une DLL et j'espérais que quelqu'un puisse m'aider. La fonction retourne -101 qui se traduit par un "mauvais paramètre". Les valeurs que je transmets ont été renvoyées à partir d'un autre appel de fonction réussi, donc mon hypothèse actuelle est que j'ai mal construit la structure. Toute aide est appréciée.Fonction C dans VB.NET
La définition de la fonction est:
int sm_switch_channel_input(struct sm_switch_channel_parms *switchp)
Parameters
*switchp (a structure of the following type):
typedef struct sm_switch_channel_parms {
tSMChannelId channel; /* in */
tSM_INT st; /* in */
tSM_INT ts; /* in */
enum kSMTimeslotType type; /* in */
} SM_SWITCH_CHANNEL_PARMS;
typedef struct tSMChannelId_struct *tSMChannelId;
typedef int tSM_INT;
enum kSMTimeslotType {
kSMTimeslotTypeALaw,
kSMTimeslotTypeMuLaw,
kSMTimeslotTypeData,
};
Et voici comment je l'ai appelé & défini ...
Enum kSMTimeslotType
kSMTimeslotTypeALaw = 0
kSMTimeslotTypeMuLaw = 1
kSMTimeslotTypeData = 2
End Enum
Public Structure sm_switch_channel_input_params
<MarshalAsAttribute(UnmanagedType.SysInt)> _
Public channel As IntPtr
<MarshalAsAttribute(UnmanagedType.I4)> _
Public stream As Integer
<MarshalAsAttribute(UnmanagedType.I4)> _
Public timeslot As Integer
<MarshalAsAttribute(UnmanagedType.U4)> _
Public tsType As kSMTimeslotType
End Structure
<DllImport("TiNG.dll")> _
Private Shared Function sm_switch_channel_input_iPsCtiie1_3__(ByRef x As sm_switch_channel_input_params) As Integer
End Function
Dim sscip As sm_switch_channel_input_params
Dim err as Integer
sscip.channel = chanA
sscip.stream = streamA
sscip.timeslot = timeSlotA
sscip.tsType = kSMTimeslotType.kSMTimeslotTypeMuLaw
err = sm_switch_channel_input_iPsCtiie1_3__(sscip)
'SysInt' est redondant ici, mais cela ne devrait pas le rendre pire:" SysInt Entier dépendant de la plate-forme, signé, 4 octets sur Windows 32 bits, 8 octets sur Windows 64 bits ". De plus, 'StructLayout' est redondant (à la fois en VB et en C#), car les structures sont mises en page séquentiellement par défaut même sans elles. –
@Pavel, SysInt est l'un des rares attributs que j'ai vu poser des problèmes, même dans le cas où il est redondant. J'ai rencontré plusieurs problèmes, généralement dans la zone avec des délégués, où le code fonctionne bien sans SysInt mais échoue avec. Pas encore fini avec mon café, donc j'ai du mal à me souvenir des cas. Même ainsi, la suppression de la redondance simplifie un peu le problème. – JaredPar