如何在 C# 中定义 TBBUTTON 结构? [英] How do I define the TBBUTTON struct in C#?

查看:27
本文介绍了如何在 C# 中定义 TBBUTTON 结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

TBBUTTON 结构体在 MSDN 上定义如下:

The TBBUTTON struct is defined on MSDN as follows:

typedef struct {
  int       iBitmap;
  int       idCommand;
  BYTE      fsState;
  BYTE      fsStyle;
#ifdef _WIN64
  BYTE      bReserved[6];
#else 
#if defined(_WIN32)
  BYTE      bReserved[2];
#endif 
#endif 
  DWORD_PTR dwData;
  INT_PTR   iString;
} TBBUTTON, *PTBBUTTON, *LPTBBUTTON;

我需要使用这个结构在 C# 中做一些互操作.我如何复制这个怪物,以便在为 AnyCPU 编译时正确定义它?谷歌显然充满了危险的错误信息!

I need to do some interop in C# using this struct. How do I replicate this monster so that it's defined correctly when compiled for AnyCPU? Google is apparently full of dangerous misinformation!

推荐答案

啊,我知道必须有办法.这是:

Ahah, I knew there had to be a way. And here it is:

[StructLayout(LayoutKind.Sequential)]
public struct TBBUTTON {
    public int iBitmap;
    public int idCommand;
    [StructLayout(LayoutKind.Explicit)]
    private struct TBBUTTON_U {
        [FieldOffset(0)] public byte fsState;
        [FieldOffset(1)] public byte fsStyle;
        [FieldOffset(0)] private IntPtr bReserved;
    }
    private TBBUTTON_U union;
    public byte fsState { get { return union.fsState; } set { union.fsState = value; } }
    public byte fsStyle { get { return union.fsStyle; } set { union.fsStyle = value; } }
    public UIntPtr dwData;
    public IntPtr iString;
}

Marshal.SizeOf 在 x64 进程上返回 32,在 x86 进程上返回 20,当我将它传递给 SendMessage 时,一切都会结束.我知道你可以做到,C#!

Marshal.SizeOf returns 32 on x64 processes and 20 on x86 processes, and everything ends up where it should when I pass this to SendMessage. I knew you could do it, C#!

这篇关于如何在 C# 中定义 TBBUTTON 结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆