-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to get dbcc_name from DEV_BROADCAST_DEVICEINTERFACE_W #1005
Comments
It won't be a |
Hi @AArnott , I'm not sure if this is fully fixed. I'm working with some USB event notification code and I use the CsWin32 library as follows,
I have some more code where I extract the SP_DEVICE_INTERFACE_DETAIL_DATA_W's __char_1 DevicePath and return the null terminated string as " return new string((char*)&pD->DevicePath); " without a problem. I am lost as to why i should offset the this dbcc_name by 1184 to obtain the device_name. Any help/tips? is it something to do with the generated struct? |
|
The OP code was faulty. Using I tested this modified code and found it works: protected override void WndProc(ref Message m)
{
if (m.Msg == PInvoke.WM_DEVICECHANGE && m.WParam.ToInt64() == PInvoke.DBT_DEVICEARRIVAL)
{
ref DEV_BROADCAST_DEVICEINTERFACE_W deviceInterface = ref Unsafe.AsRef<DEV_BROADCAST_DEVICEINTERFACE_W>((void*)m.LParam);
// The length of the entire structure (including trailing buffer) is given by dbcc_size.
// We subtract the known size of the structure to get the length of the trailing buffer.
// Then because the length is given in bytes, we divide by the size of a char to get the number of characters.
int length = ((int)deviceInterface.dbcc_size - sizeof(DEV_BROADCAST_DEVICEINTERFACE_W)) / sizeof(char);
string? name = deviceInterface.dbcc_name.AsSpan(length).ToString();
}
base.WndProc(ref m);
} @BartoszCichecki, Your workaround seems extremely dangerous and you evidently are just scanning random memory. You shouldn't make any assumptions about the length of the string or where in memory it might have been copied to. The above code I provide seems to work precisely. |
Thanks for the correct solution! You are right this was a very dangerous hack. By the way, would it make sense to generate convenience methods for stuff like this? |
I'm glad the solution works for you. The |
Actual behavior
The generated struct includes the dbcc_name field as
winmdroot.__char_1
. It should be a null terminated string, but there's only ever one character there.Expected behavior
I expect a char* with my full string available.
Repro steps
NativeMethods.txt
content:Here's a quick WinForms example that should repro the issue. Plug in a flash drive with this running.
If I declare my own struct as follows, the above code then works as I expect.
Context
The text was updated successfully, but these errors were encountered: