Comments (4)
Thanks. But that's what they realy are ... ideas ... not proposals. Like I said, I could use the generated types as they are by just rewriting a little bit of code. But if you are interested in the "flexible array without a 1-byte empty struct" idea. There are some corner cases to consider. Such as alignment, for example a C struct:
UCHAR a;
UCHAR b;
UCHAR c;
DWORD array[0];
In C this will have alignment 4, size 4, and a byte of padding between c and array. But if you simply make a C# struct with the first 3 members and a kludge method like I suggested, then of course you need something like pack(4). In that case a simple dummy padding member is not enough, as that would make the size 4, but still have alignment 1.
from cswin32.
That's an interesting problem. Am I correct in supposing it's important for you to get the size of the struct that reflects a 0 length trailing buffer specifically, such that passing a size that implies a length of 1 causes a malfunction? Or is this just to 'feel good' about the size matching what C would say?
It's potentially problematic to change the behavior to match what you say, because to C#, the VariableLengthInlineArray
struct is one element in length. Suppose then that you use SizeOf(0)
to determine the size of a buffer you needed to allocate, and then you copied a USB_DESCRIPTOR_REQUEST
into it from C#. That would be a buffer overrun because the runtime would copy USB_DESCRIPTOR_REQUEST
plus one element, since that's how the type is defined in C#. The SizeOf
method was written to reflect the size of the memory that must be allocated from C#. It wasn't meant for initializing a field on the native struct that would represent the whole size of the struct plus buffer (given a 0 length buffer, anyway).
If this really is important, we may need to introduce another function alongside SizeOf
with a very clear name and xml docs so folks know which one to call when.
from cswin32.
My use case could definitely be rewritten to use the current way CsWin32 generates the struct. But before CsWin32 supported the USB structures I already had my own definition as here:
I use it "C-style" to custom marshal the data that comes after it, which is not really byte data, but instead another structure. And with "C-style" I mean: use offset into the byte representation to get to the start of that following data struct. And that offset in C is just sizeof(USB_DESCRIPTOR_REQUEST). But not in C#, where there currently is no way to get the that original size (except subtracting the 1 byte manually). Here is how I use it (note: this code predates the CsWin32 support for these structures.
I am slowly trying to get rid of all my manual WinSDK "defines" and move to CsWin32 for everything. This thing just struck me. And like I said, I can easily rewrite it to work with CsWin32. I was just wondering why it is not possible to get the original C size for "sizeof(USB_DESCRIPTOR_REQUEST)".
On a side note: what if, instead of adding the empty struct with [] operator (which VariableLengthInlineArray
really is) you would implement the 0-size array last element as member function. Then you could do something like:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ref TData FlexibleArrayMember(int index)
{
if (index < 0) {
throw new ArgumentOutOfRangeException(nameof(index));
}
unsafe
{
fixed (STRUCT_TYPE* ptr = &this)
{
return ref *(((TData *)(ptr + 1)) + index);
}
}
}
Instead of addressing the flexible array like Data[index]
it would be Data(index)
, but at no cost of the one extra byte for the empty struct. And such a member function should probably be marked as unsafe
(even though its prototype itself does not require that).
And on that last note: why is the SizeOf()
helper marked as unsafe
. I think it shouldn't be...
from cswin32.
I think you have some good ideas here. I haven't the time this week probably, but I'll review more closely and see what we can adopt.
from cswin32.
Related Issues (20)
- APPBARDATA should compile on AnyCPU HOT 2
- CsWin32 should avoid Unsafe.SkipInit when it doesn't exist in the compilation HOT 1
- WlanOpenHandle doesn't support SafeHandle HOT 1
- The API "Gdi32" is ambiguous HOT 4
- Add `GET_WHEEL_DELTA_WPARAM` macro
- `TASKDIALOGCONFIG.pfCallback` should use associated enum for `msg` parameter
- Broken `VariableLengthInlineArray<T>` after upgrade from 0.3.49-beta to 0.3.106 HOT 8
- NetFramework - Unsafe.SkipInit(out T value) unavailable HOT 3
- Inconsistent const in parameters of RmRegisterResources HOT 9
- LocalExternFunctions should use `DefaultDllImportSearchPaths`
- Code Quality: Use `WinMdRoot` instead of `winmdroot` to supress CS8981 (type name is ASCII lowercase only) HOT 5
- `PCWSTR` is not marked with `[GeneratedCode]`
- Many Win32 structs (HDC, HMONITOR, etc.) lack the IntPtr conversion operator.
- "Pointers cannot reference marshaled structures. Use ByRef instead" caused by VariableLengthInlineArray
- Latest WDK metadata fails to create DEVPKEY field
- Daily nuget feed not updating HOT 1
- MFTEnumEx signature incorrect
- `VirtualFreeEx` missing HOT 1
- Missing NtCreateThread and NtCreateThreadEx functions HOT 1
- Add an analyzer to catch inadvertent ephemeral delegates HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cswin32.