I need clarification on how to use EnumPorts

  • Thread starter Thread starter Ken
  • Start date Start date
K

Ken

Hello Everyone,

I would like to use EnumPorts twice on my form. The first
time I want to use Port_Info_1 to just list the Port
Names. The second instance where I need EnumPorts , is
when I receive the Combo.SelectedIndexChanged event. I
want to use EnumPorts to get the Port_Info_2 based on the
Port Name. I have it working for Port_Info_1, however I am
having a problem understanding what I should send to
EnumPorts(,,,,). How do I find the info for just that
port? and how do I avoid getting errors when that port is
listed, but it has no info?

Thank You in Advance for Your Help,
Ken
 
Hi Ken,

You can call the EnumPorts API to get the PORT_INFO_1 in .Net like this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct PORT_INFO_1
{
[MarshalAs(UnmanagedType.LPTStr)]
public string pName;
}

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern bool EnumPorts(string pName, int level, IntPtr bufptr,int
cbBuf, out int pcbNeeded, out int pcReturned);

int pcReturned = 0;
int pcbNeeded = 0;
IntPtr outb = IntPtr.Zero;

EnumPorts(null, 1, outb, 0, out pcbNeeded, out pcReturned);
outb = Marshal.AllocHGlobal(pcbNeeded+1);
EnumPorts(null, 1, outb, pcbNeeded, out pcbNeeded, out pcReturned);
PORT_INFO_1[] manyArr = new PORT_INFO_1[pcReturned];

IntPtr current = outb;
for (int i=0; i<pcReturned; i++)
{
manyArr = (PORT_INFO_1) Marshal.PtrToStructure(current,
typeof(PORT_INFO_1));
current=(IntPtr)((int)current+Marshal.SizeOf(typeof(PORT_INFO_1)));
}
Marshal.FreeHGlobal(outb);

You can use the same logic to get the PORT_INFO_2, then you can get the
information of certian port like by comparing the port name.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Ken" <[email protected]>
| Sender: "Ken" <[email protected]>
| Subject: I need clarification on how to use EnumPorts
| Date: Thu, 9 Oct 2003 09:49:08 -0700
| Lines: 17
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOOhUG67BaNny/kTfiSMBBrSlaccQ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:190304
| NNTP-Posting-Host: TK2MSFTNGXA06 10.40.1.53
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hello Everyone,
|
| I would like to use EnumPorts twice on my form. The first
| time I want to use Port_Info_1 to just list the Port
| Names. The second instance where I need EnumPorts , is
| when I receive the Combo.SelectedIndexChanged event. I
| want to use EnumPorts to get the Port_Info_2 based on the
| Port Name. I have it working for Port_Info_1, however I am
| having a problem understanding what I should send to
| EnumPorts(,,,,). How do I find the info for just that
| port? and how do I avoid getting errors when that port is
| listed, but it has no info?
|
| Thank You in Advance for Your Help,
| Ken
|
|
|
 
Back
Top