我在从 DLL 调用 C 函数时遇到一些问题,希望有人能帮助我解决。该函数返回 -101,翻译为“错误参数”。我传递的值是从另一个成功的函数调用返回的,所以我当前的假设是我错误地构建了结构。如有任何帮助,我们将不胜感激。
函数定义为:
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,
};
这是我如何定义和调用它的......
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 属性是导致您出现问题的原因。在这种情况下没有必要,因为 IntPtr
将始终根据当前平台正确编码。您还缺少 StructLayout
属性
还有一些其他属性也不是必需的。我会尝试使用以下定义
Enum kSMTimeslotType
kSMTimeslotTypeALaw = 0
kSMTimeslotTypeMuLaw = 1
kSMTimeslotTypeData = 2
End Enum
<StructLayout(LayoutKind.Sequential)> _
Public Structure sm_switch_channel_input_params
Public channel As IntPtr
Public stream As Integer
Public timeslot As Integer
Public tsType As kSMTimeslotType
End Structure
关于VB.NET 中的 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1861842/