【问题标题】:How to marshall as the I8 type with PInvoke?如何使用 PInvoke 编组为 I8 类型?
【发布时间】:2012-03-11 02:07:20
【问题描述】:

我有一个 UInt32 值,我想使用 InterOpServices 传递给外部 dll。

非托管代码的原型是:

[DllImport("svr.dll")]
public static extern UInt32  CreateTag (
    [MarshalAs(UnmanagedType.LPStr)] String Name,
    Object Value,
    UInt16 InitialQuality,
    bool IsWritable);

调用代码是:

int myValue = Convert.ToInt32(item); //How to marshal as I8 type
tagNumber = (UInt32)svr_DLL.CreateTag(
    DeviceName + "." + el.tagName,
    myValue, // <-- this argument
    192,
    Convert.ToBoolean(el.tagEditable));

我想将对象值“myValue”作为 I8 类型传递给对象。

如何做到这一点?

【问题讨论】:

    标签: c# pinvoke marshalling interopservices


    【解决方案1】:

    您需要在参数声明中指定:[MarshalAs(UnmanagedType.I8)]

    【讨论】:

      【解决方案2】:

      UnmanagedType 是一个枚举,所以你可以试试 Enum.Parse 方法:

      string value = "9";
      UnmanagedType i8 = (UnmanagedType)Enum.Parse(typeof(UnmanagedType), value);
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-15
        相关资源
        最近更新 更多