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

问题描述

所有问候,

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

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编译时正确定义它? Google显然充满了危险的错误信息!

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结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:15