如何在 C# 中定义 TBBUTTON 结构?

编程入门 行业动态 更新时间:2024-10-23 04:58:56
本文介绍了如何在 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 结构?

本文发布于:2023-11-06 06:52:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   结构   如何在   TBBUTTON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!