redeexpressos
redeexpressos
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
Thank you very much 🙂
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
also https://www.meziantou.net/stop-using-intptr-for-dealing-with-system-handles.htm:
SafeHandles are well-integrated with P/Invoke. You can use SafeHandle-derived class in the definition of the method instead of IntPtr, so they are strongly-typed.
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
and was intrigued how does this work under the hood..
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
I have another question, is SafeHandle a good replacement for IntPtr? ( T: SafeHandle)
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
so I can be sure that sizeof(T*) will always be sizeof(IntPtr)?
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
e.g:
[StructLayout(LayoutKind.Explicit)]
public unsafe struct Encryption
{
[FieldOffset(0)]
public EncryptionTag Tag; // discriminator

[FieldOffset(4)]
public IntPtr EncryptionKey; // pointer to c string, will cast later

[FieldOffset(4)]
public EncKeys* EncryptionKeys; // pointer to another struct, will cast later
}
[StructLayout(LayoutKind.Explicit)]
public unsafe struct Encryption
{
[FieldOffset(0)]
public EncryptionTag Tag; // discriminator

[FieldOffset(4)]
public IntPtr EncryptionKey; // pointer to c string, will cast later

[FieldOffset(4)]
public EncKeys* EncryptionKeys; // pointer to another struct, will cast later
}
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
what if they are not both nint?
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
I suppose my top-struct (Encryption) will need [StructLayout(LayoutKind.Sequential)]
20 replies
CC#
Created by redeexpressos on 1/21/2025 in #help
compile-time `IntrPtr` size for union-like structs with `FieldOffset`
interesting approach
20 replies