Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Pavel's class <code>SetupDiWrap</code> works great, it just needs a few small tweaks for Windows 7. </p> <p>Hopefully, this update will help other people struggling (like me) to get COM port numbers from VCP names in Windows 7.</p> <p>1) The <code>SP_DEVINFO_DATA</code> has changed in Windows 7, the total length is no longer 28 bytes, it is 32 bytes long. This is what works for me:</p> <pre><code> private struct SP_DEVINFO_DATA { /// &lt;summary&gt;Size of the structure, in bytes.&lt;/summary&gt; public int cbSize; /// &lt;summary&gt;GUID of the device interface class.&lt;/summary&gt; public Guid ClassGuid; /// &lt;summary&gt;Handle to this device instance.&lt;/summary&gt; public int DevInst; /// &lt;summary&gt;Reserved; do not use.&lt;/summary&gt; public ulong Reserved; } </code></pre> <p>Note ulong for Reserved instead of an int. Changing uint <code>cbSize</code> to int <code>cbSize</code> saved me a cast later on, otherwise you can leave it as uint.</p> <p>2) I also wrote the line:</p> <pre><code> da.cbSize = (uint)Marshal.SizeOf(da); </code></pre> <p>a bit different, for clarity, to get the cbSize to 32 bits:</p> <pre><code>da.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)); </code></pre> <p>3) I changed</p> <pre><code> [DllImport("setupapi.dll", CharSet = CharSet.Auto)] static extern IntPtr SetupDiGetClassDevs( ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, int Flags ); </code></pre> <p>to</p> <pre><code> [DllImport("setupapi.dll", CharSet = CharSet.Auto)] private static extern IntPtr SetupDiGetClassDevs( ref Guid ClassGuid, UInt32 Enumerator, IntPtr hwndParent, UInt32 Flags ); </code></pre> <p>The Enumerator is no longer an <code>IntPtr</code>, so you need to call <code>SetupDiGetClassDevs</code> like this:</p> <pre><code>IntPtr h = SetupDiGetClassDevs(ref guidClone, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_PROFILE); </code></pre> <p>Note "0" instead of <code>IntPtr.Zero</code> when passing the Enumerator.</p> <p>The code now runs like a charm in Windows 7!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload