Closed Ephron-WL closed 1 week ago
Rookie mistake. When the method returns references to unmanaged memory are lost. To return a string, even if the string is copied, requires that the unmanaged memory remain, I presume due to compilation optimizations leading to memory assignments that avoid allocating to the heap. By assigning variables that hold unmanaged memroy to a context that survives method calls I can ensure the memory remains accessible.
Actual behavior
SP_DEVICE_INTERFACE_DETAIL_DATA_W
is generated with a propertyDevicePath
of typeVariableLengthInlineArray<char>
An instance of
SP_DEVICE_INTERFACE_DETAIL_DATA_W
is used as an argument toSetupDiGetDeviceInterfaceDetail
.To obtain the
DevicePath
value I usenew string(&detail.DeviocePath.eb0)
. This works great. If I execute this code within any method other than the Main method, I get an access violation at the point in which the method returns. If I create theSP_DEVICE_INTERFACE_DETAIL_DATA_W
inside of the Main method and pass a pointer into the method and instantiate the string in the Main method, it works.Great library, otherwise.
Expected behavior
I would expect to be able to execute my code within a method other than Main and return the results in the form of a string from a method other than Main.
Repro steps
NativeMethods.txt
content:Any of your own code that should be shared?
Context
0.4.422-beta
] 0.3.106netstandard2.0
] core 8LangVersion
(if explicitly set by project): [e.g.9
]