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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now