vb.net vb .net,将枚举值作为函数参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14060637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:41:38  来源:igfitidea点击:

vb .net, passing enum value as function parameter

vb.net

提问by user1797147

This compiles without error but when I run this code section some strange error appears:

这编译没有错误但是当我运行此代码部分时出现一些奇怪的错误:

FatalExecutionEngineError was detected Message: The runtime has encountered a fatal error. The address of the error was at 0x792062fb, on thread 0xd7c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

检测到 FatalExecutionEngineError 消息:运行时遇到致命错误。错误的地址是 0x792062fb,在线程 0xd7c 上。错误代码是 0xc0000005。此错误可能是 CLR 或用户代码的不安全或不可验证部分中的错误。此错误的常见来源包括 COM 互操作或 PInvoke 的用户封送错误,这可能会损坏堆栈。

I try something like follows:

我尝试如下:

    Public Enum PipePolicyType
        SHORT_PACKET_TERMINATE = 1
        AUTO_CLEAR_STALL = 2
        PIPE_TRANSFER_TIMEOUT = 3
        IGNORE_SHORT_PACKETS = 4
        ALLOW_PARTIAL_READS = 5
        AUTO_FLUSH = 6
        RAW_IO = 7
        RESET_PIPE_ON_RESUME = 9
    End Enum

...
...

        Dim a As UInt32 = 3000

        SetPolicy(1, PIPE_TRANSFER_TIMEOUT, a)

...
...
Public Sub SetPolicy(ByVal ep As Integer, ByVal PolicyType As PipePolicyType, ByVal PolicyValue As UInt32)


end sub

what is wrong ?

怎么了 ?

采纳答案by Hanlet Esca?o

You need to call it using PipePolicyType.PIPE_TRANSFER_TIMEOUT

您需要使用 PipePolicyType.PIPE_TRANSFER_TIMEOUT 调用它

Public Class Form1
    Public Sub SetPolicy(ByVal ep As Integer, ByVal PolicyType As PipePolicyType, ByVal PolicyValue As UInt32)

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim a As UInt32 = 3000

        SetPolicy(1, PipePolicyType.PIPE_TRANSFER_TIMEOUT, a)
    End Sub
End Class

Public Enum PipePolicyType
    SHORT_PACKET_TERMINATE = 1
    AUTO_CLEAR_STALL = 2
    PIPE_TRANSFER_TIMEOUT = 3
    IGNORE_SHORT_PACKETS = 4
    ALLOW_PARTIAL_READS = 5
    AUTO_FLUSH = 6
    RAW_IO = 7
    RESET_PIPE_ON_RESUME = 9
End Enum

回答by user1797147

Ok, I found the problem totally somewhere else and I don't understand. There was no problem with my enums at all, some curious error in some wrapper to unmanaged:

好的,我完全在其他地方发现了问题,我不明白。我的枚举完全没有问题,在一些未托管的包装器中出现了一些奇怪的错误:

    Dim PUnmanagedDetailedInterfaceDataStructure As IntPtr = IntPtr.Zero
    PUnmanagedDetailedInterfaceDataStructure = Marshal.AllocHGlobal(StructureSize)

This was shortened by me few days ago like this

这是我几天前像这样缩短的

    Dim PUnmanagedDetailedInterfaceDataStructure As IntPtr = Marshal.AllocHGlobal(StructureSize)

What is the problem with second declaration ?!

第二个声明有什么问题?!