VB.NET 中的 C 函数

标签 c vb.net interop marshalling

我在从 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/

相关文章:

c++ - 为什么相同的代码运行时间不同?

c - puts() 显示 strcpy 的全部内容,即使发生 dest 溢出

c - 如何在C程序中将数组中的元素设置为空

vb.net - Windows 窗体 - 如何通过列名称访问 ListView 的子项?

vb.net - VB.NET中 "FunctionName = value"和 "Return value"之间的区别?

c# - WPF:应用程序关闭时关闭辅助窗口而无需 "programmer"干预

ruby - 如何从 Ruby 调用 shell 命令

c - 从 Haskell 释放 C 运行时分配的内存

WPF Scrollviewer ItemsControl 将项目滚动到 View 中

java - Scala/Clojure 互操作可以在 PC 上运行,但在另一台 PC 上失败并出现 java.lang.ExceptionInInitializerError