device enumeration

Posted on 2002-04-08
Last Modified: 2010-04-15
How would you enumerate devices on a particular machine. For example on my machine(which is Windows NT 4.0), i want to know how many serial ports,parellel ports,modems etc available. How do i do that.
Could any one of you help me with code using c/c++ language.
thanking...and awiting..
Question by:rupesh_kumar
LVL 30

Accepted Solution

SteveGTR earned 20 total points
ID: 6925208
This is from MSDN help for WNetEnumResource:

BOOL WINAPI EnumerateFunc(HWND hwnd,
                          HDC hdc,
                          LPNETRESOURCE lpnr)
  DWORD dwResult, dwResultEnum;
  HANDLE hEnum;
  DWORD cbBuffer = 16384;      // 16K is a good size
  DWORD cEntries = -1;         // enumerate all possible entries
  LPNETRESOURCE lpnrLocal;     // pointer to enumerated structures
  DWORD i;
  // Call the WNetOpenEnum function to begin the enumeration.
  dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
                          RESOURCETYPE_ANY,   // all resources
                          0,        // enumerate all resources
                          lpnr,     // NULL first time the function is called
                          &hEnum);  // handle to the resource

  if (dwResult != NO_ERROR)
    // Process errors with an application-defined error handler.
    NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetOpenEnum");
    return FALSE;
  // Call the GlobalAlloc function to allocate resources.
  lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
    // Initialize the buffer.
    ZeroMemory(lpnrLocal, cbBuffer);
    // Call the WNetEnumResource function to continue
    //  the enumeration.
    dwResultEnum = WNetEnumResource(hEnum,      // resource handle
                                    &cEntries,  // defined locally as -1
                                    lpnrLocal,  // LPNETRESOURCE
                                    &cbBuffer); // buffer size
    // If the call succeeds, loop through the structures.
    if (dwResultEnum == NO_ERROR)
      for(i = 0; i < cEntries; i++)
        // Call an application-defined function to
        //  display the contents of the NETRESOURCE structures.
        DisplayStruct(hdc, &lpnrLocal[i]);

        // If the NETRESOURCE structure represents a container resource,
        //  call the EnumerateFunc function recursively.

        if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage
                                       & RESOURCEUSAGE_CONTAINER))
          if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i]))
            TextOut(hdc, 10, 10, "EnumerateFunc returned FALSE.", 29);
    // Process errors.
    else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
      NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource");
  // End do.
  while(dwResultEnum != ERROR_NO_MORE_ITEMS);
  // Call the GlobalFree function to free the memory.
  // Call WNetCloseEnum to end the enumeration.
  dwResult = WNetCloseEnum(hEnum);
  if(dwResult != NO_ERROR)
    // Process errors.
    NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum");
    return FALSE;

  return TRUE;

Good Luck,
LVL 45

Expert Comment

by:Kent Olsen
ID: 9478589
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept SteveGTR's comment as answer

Please leave any comments here within the next seven days.


Kent (Kdo)
EE Cleanup Volunteer

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to organize data in excel ? 2 115
How to get time_t value that is a week old 6 169
Global Keyboard Hooks Blocked 4 81
Memory going from 12gb to 64gb or 96gb. worth it? 15 203
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question