从 C# 传递位值

发布于 2024-11-16 09:44:01 字数 672 浏览 3 评论 0原文

我有一个 C++ 接口,它有一个公共属性“P”,它接受 5、6,7 等位值。 它的文档说:“设置组类型的位掩码。第 5 位用于“a”,第 6 位用于“b”等。”

我在我的 C# 类中使用这个接口,元数据中显示的属性“P”的类型在 VS.Net 中是“char”。

如何将 6 和 7 的位值从我的 C# 代码传递到此属性? 请注意,如上所述,应将“char”类型的值从 C# 传递到此 C++ 接口,因为这是 VS.net 元数据中显示的类型,

请建议。 代码:

从 VS.Net IDE 中看到的 C++ 接口定义 -

[SuppressUnmanagedCodeSecurity]
    [Guid("f274179c-6d8a-11d2-90fc-00806fa6792c")]
    [InterfaceType(1)]
    public interface IAccount
    {
        char GroupType { get; set; }
    }

C#:

IAccount objAccount= new AccountClass();
((IAccount)objAccount).GroupType = ??//I need to pass char value here

谢谢。

I've a C++ interface having a public property "P" which accepts bit value like 5, 6,7 etc.
Its documentation says:"Set bit mask of group type. Bit 5 is for 'a', Bit 6 is for 'b',etc."

Am using this interface in my C# class and the type of this property "P" shown in the metadata is "char" in VS.Net.

How do I pass the bit value of 6 and 7, to this property from my C# code?
Please note that, as mentioned above, a value of type "char" should be passed from C# to this C++ interface, as this is the type which is shown in the metadata in VS.net

Please suggest.
Code:

C++ interface definition as seen from VS.Net IDE--

[SuppressUnmanagedCodeSecurity]
    [Guid("f274179c-6d8a-11d2-90fc-00806fa6792c")]
    [InterfaceType(1)]
    public interface IAccount
    {
        char GroupType { get; set; }
    }

C#:

IAccount objAccount= new AccountClass();
((IAccount)objAccount).GroupType = ??//I need to pass char value here

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情痴 2024-11-23 09:44:02

C++ 中的 char 类型始终为 8 位,这可能意味着您将使用 byte 在 C# 中表示相同的事物。 (这假设您的 C++ 平台使用标准 8 位字节,因为 C++ char 被定义为 1 字节,但 C++“字节”不一定保证为 8 位!

byte b = 0;
b |= 1 << 5;    // set bit 5 (assuming that the bit indices are 0-based)
b |= 1 << 6;    // set bit 6 (assuming that the bit indices are 0-based)

)不确定如何将该值封送回 C++ 例程(如果这是您需要做的)。

The char type in C++ is always 8 bits, which presumably means that you'd use a byteto represent the same thing in C#. (This assumes that your C++ platform uses standard 8-bit bytes, since a C++ char is defined as being 1 byte, but a C++ "byte" isn't necessarily guaranteed to be 8 bits!)

byte b = 0;
b |= 1 << 5;    // set bit 5 (assuming that the bit indices are 0-based)
b |= 1 << 6;    // set bit 6 (assuming that the bit indices are 0-based)

I'm not sure how you'd marshal that value back to your C++ routine, if that's what you need to do.

拥醉 2024-11-23 09:44:01

您可以使用继承自“byte”的“Enum”类型:

[Flags]
enum BitFlags : byte
{
    One = ( 1 << 0 ),
    Two = ( 1 << 1 ),
    Three = ( 1 << 2 ),
    Four = ( 1 << 3 ),
    Five = ( 1 << 4 ),
    Six = ( 1 << 5 ),
    Seven = ( 1 << 6 ),
    Eight = ( 1 << 7 )
}

void Main()
{
    BitFlags myValue= BitFlags.Six | BitFlags.Seven;

    Console.WriteLine( Convert.ToString( (byte) myValue, 2 ) );
}

输出:1100000

您需要发布有关本机方法以及如何调用它的更多信息,以便提供进一步帮助。

[SuppressUnmanagedCodeSecurity]
[Guid("f274179c-6d8a-11d2-90fc-00806fa6792c")]
[InterfaceType(1)]
public interface IAccount
{
    byte GroupType { get; set; } // char in native C++ is generally going to be 8 bits, this = C# byte
}

IAccount objAccount= new AccountClass();  

( ( IAccount ) objAccount ).GroupType = ( byte )( BitFlags.Six | BitFlags.Seven );

You can use a "Enum" type which inheres from "byte":

[Flags]
enum BitFlags : byte
{
    One = ( 1 << 0 ),
    Two = ( 1 << 1 ),
    Three = ( 1 << 2 ),
    Four = ( 1 << 3 ),
    Five = ( 1 << 4 ),
    Six = ( 1 << 5 ),
    Seven = ( 1 << 6 ),
    Eight = ( 1 << 7 )
}

void Main()
{
    BitFlags myValue= BitFlags.Six | BitFlags.Seven;

    Console.WriteLine( Convert.ToString( (byte) myValue, 2 ) );
}

Output: 1100000

You need to post more information on the native method and how you're invoking it in order to help further.

[SuppressUnmanagedCodeSecurity]
[Guid("f274179c-6d8a-11d2-90fc-00806fa6792c")]
[InterfaceType(1)]
public interface IAccount
{
    byte GroupType { get; set; } // char in native C++ is generally going to be 8 bits, this = C# byte
}

IAccount objAccount= new AccountClass();  

( ( IAccount ) objAccount ).GroupType = ( byte )( BitFlags.Six | BitFlags.Seven );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文