Git Product home page Git Product logo

Comments (7)

AArnott avatar AArnott commented on July 18, 2024 1

Thanks for reporting. The char type referenced in the DevicePath field is causing .NET Framework to consider SP_DEVICE_INTERFACE_DETAIL_DATA_W to be a managed type. Changing it to ushort gets it to work. I'll investigate what the best fix is for CsWin32 here.

from cswin32.

AaronRobinsonMSFT avatar AaronRobinsonMSFT commented on July 18, 2024 1

The char type referenced in the DevicePath field is causing .NET Framework to consider SP_DEVICE_INTERFACE_DETAIL_DATA_W to be a managed type

A nit on this statement. The concept of "managed" or "unmanaged" is historically a C# ism and not defined by the runtime. The runtime deals with the terms blittable and non-blittable. In .NET the bool and char primitives are non-blittable but are considered "unmanaged" by C#. This is friction we are slowly trying to fix in .NET 7+, it will be a long process.

@tannergooding @AaronRobinsonMSFT do you have any ideas here?

Using short, as you discovered, is what I would recommend.

from cswin32.

AArnott avatar AArnott commented on July 18, 2024

I tested this and it appeared to work. I think it would be safe.

-internal global::Windows.Win32.VariableLengthInlineArray<char> DevicePath;
+internal global::Windows.Win32.VariableLengthInlineArray<char, ushort> DevicePath;

and

-    internal struct VariableLengthInlineArray<T>
+    internal struct VariableLengthInlineArray<T, T2>
 where T : unmanaged
+where T2 : unmanaged
     {
-        internal T e0;
+        internal T2 e0;
 
 
         internal unsafe ref T this[int index]
         {
             [UnscopedRef]
             [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            get => ref Unsafe.Add(ref this.e0, index);
+            get => ref Unsafe.Add(ref Unsafe.AsRef<T>(Unsafe.AsPointer(ref this.e0)), index);
         }

This works by changing the field from char to ushort, while still presenting almost the same char based API. In fact we could probably change the field e0 itself to private and add a ref T property getter to even present char there.

@tannergooding @AaronRobinsonMSFT do you have any ideas here?

from cswin32.

AArnott avatar AArnott commented on July 18, 2024

In .NET the bool and char primitives are non-blittable

@AaronRobinsonMSFT Do you mean in .NET Framework? .NET 8 didn't throw an exception in the OP's repro -- only .NET Framework did. That seems to suggest that .NET 8 considers char to be blittable.

from cswin32.

AaronRobinsonMSFT avatar AaronRobinsonMSFT commented on July 18, 2024

.NET 8 didn't throw an exception in the OP's repro -- only .NET Framework did. That seems to suggest that .NET 8 considers char to be blittable.

Hmmm. That is surprising. Are you using DisableRuntimeMarshalling?

from cswin32.

AArnott avatar AArnott commented on July 18, 2024

No.

from cswin32.

Lightczx avatar Lightczx commented on July 18, 2024

Hmmm. That is surprising. Are you using DisableRuntimeMarshalling?

I can confirm that it will not throw when the [assembly: DisableRuntimeMarshalling] present in .NET 8

from cswin32.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.