Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than winspool, prefix the name with the module name and a period.
public PORT_INFO_2[] GetInstalledPorts()
{
uint pcbNeeded = 0;
uint pcReturned = 0;
int pcbNeeded = 0;
int pcReturned = 0;
if (EnumPorts(null, 2, IntPtr.Zero, 0, ref pcbNeeded, ref pcReturned))
{
//succeeds, but must not, because buffer is zero (too small)!
throw new Exception("EnumPorts should fail!");
}
int lastWin32Error = Marshal.GetLastWin32Error();
//ERROR_INSUFFICIENT_BUFFER = 122 expected, if not -> Exception
if (lastWin32Error != 122)
{
throw new Win32Exception(lastWin32Error);
}
for (int i = 0; i < pcReturned; i++)
{
pinfo[i] = (PORT_INFO_2)Marshal.PtrToStructure(currentPort, typeof(PORT_INFO_2));
currentPort = (IntPtr) (currentPort.ToInt32() + Marshal.SizeOf(typeof (PORT_INFO_2)));
}
Marshal.FreeHGlobal(pPorts);
return pinfo;
}
throw new Win32Exception(Marshal.GetLastWin32Error());
}
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).