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.
EnumPorts (winspool)
.
C# S i g n a t u r e :
[DllImport("winspool.drv",CharSet = CharSet.Auto, SetLastError = true)]
static extern bool EnumPorts (string pName, int Level, IntPtr lpbPorts, int cbBuf,ref int pcbNeeded,ref int pcReturned);
VB Signature:
TODO
User-Defined Types:
None.
Alternative Managed API:
Do you know one? Please contribute it!
Notes:
None.
Tips & Tricks:
Please add some!
Sample Code:
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PORT_INFO_2
{
[MarshalAs(UnmanagedType.LPTStr)]
public string pPortName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pMonitorName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDescription;
public PortType fPortType;
internal uint Reserved;
}
[DllImport("winspool.drv",CharSet = CharSet.Auto, SetLastError = true)]
static extern bool EnumPorts (string pName, int Level, IntPtr lpbPorts, int cbBuf,ref int pcbNeeded,ref int pcReturned);
public PORT_INFO_2[] GetInstalledPorts()
{
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).