C ++ ULONG定义到VB.NET或C#等效?

时间:2009-03-16 12:40:02

标签: c# c++ vb.net types

我正在尝试使用套接字使用呼叫记录API。我们有API文档,但样本都是C ++。

如何在VB.NET或C#中声明以下内容?

#define SIF_GENERAL 0x08000000
#define SIF_CONFIGURATION 0x08010000
#define SIF_ARCHIVE 0x08020000
#define SIF_SEARCH 0x08030000
#define SIF_REPLAY 0x08040000
#define SIF_STATISTICS 0x08050000
#define SIF_ENGINEER 0x08060000

文档中注意:消息标识符是无符号的32位值(ULONG)。

4 个答案:

答案 0 :(得分:4)

VB.NET

Module Constants
  Public Const SIF_GENERAL as Integer =&H08000000
  Public Const SIF_CONFIGURATION As Integer = &H08010000
  Public Const SIF_ARCHIVE As Integer = &H08020000
  Public Const SIF_SEARCH As Integer = &H08030000
  Public Const SIF_REPLAY As Integer = &H08040000
  Public Const SIF_STATISTICS As Integer = &h08050000
  Public Const SIF_ENGINEER As Integer = &h08060000

End Module

C#

public static class Constants {
  public const int SIF_GENERAL =0x08000000;
  public const int SIF_CONFIGURATION = 0x08010000;
  public const int SIF_ARCHIVE = 0x08020000;
  public const int SIF_SEARCH = 0x08030000;
  public const int SIF_REPLAY = 0x08040000;
  public const int SIF_STATISTICS = 0x08050000;
  public const int SIF_ENGINEER = 0x08060000;
}

答案 1 :(得分:2)

C ++ ULONG是一种32位无符号整数类型。在这种情况下,你的常量不需要是无符号的,所以只需在C#中使用int或在VB.NET中使用Integer。

VB.NET

Public Const SIF_GENERAL as Integer = &H08000000

C#

public const int SIF_GENERAL = 0x08000000;

请勿使用64位长数据类型,因为API调用的数据大小不正确。

答案 2 :(得分:2)

您可以使用枚举来使值看起来更像.NETty:

//C#
public enum SIF : uint
{
    SIF_GENERAL = 0x08000000,
    SIF_CONFIGURATION = 0x08010000,
    SIF_ARCHIVE = 0x08020000,
    SIF_SEARCH = 0x08030000,
    SIF_REPLAY = 0x08040000,
    SIF_STATISTICS = 0x08050000,
    SIF_ENGINEER = 0x08060000,
}

'VB.NET
Public Enum SIF As UInt32
    SIF_GENERAL = &H08000000
    SIF_CONFIGURATION = &H08010000
    SIF_ARCHIVE = &H08020000
    SIF_SEARCH = &H08030000
    SIF_REPLAY = &H08040000
    SIF_STATISTICS = &H08050000
    SIF_ENGINEER = &H08060000
End Enum

通过这种方式,您可以获得枚举的可发现性和类型安全性(除非您通过界面传递它们,您需要在其中进行投射)。

您甚至可以通过重命名来增加.NET外观 - 例如SIF.SIF_GENERAL可能会成为SIF.General,尽管它的优势非常小。

答案 3 :(得分:1)

如果你有常数> = 0x8000000,那么你可以这样定义它们:

class Test
{
   public const int AllBits = unchecked((int)0xFFFFFFFF);
   public const int HighBit = unchecked((int)0x80000000);
}

See this MSDN article on unchecked