Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:33:14+00:00 2026-06-08T04:33:14+00:00

I am trying to list all devices attached to my system and after searching

  • 0

I am trying to list all devices attached to my system and after searching found this code which throws up error local function definations are illegal can someone explain what its means please.

Or is my issue because I am trying to use code that was from in C++. Thanks

WORKING CODE

#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#pragma comment(lib,"SetupAPI") 


void print_property
(
    __in HDEVINFO hDevInfo,
    __in SP_DEVINFO_DATA DeviceInfoData,
    __in PCWSTR Label,
    __in DWORD Property
)
{
    DWORD DataT;
    LPTSTR buffer = NULL;
    DWORD buffersize = 0;

    // 
    while (!SetupDiGetDeviceRegistryProperty(
                hDevInfo,
                &DeviceInfoData,
                Property,
                &DataT,
                (PBYTE)buffer,
                buffersize,
                &buffersize))
    {
        if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
        {
            // Change the buffer size.
            if (buffer)
            {
                LocalFree(buffer);
            }
            // Double the size to avoid problems on 
            // W2k MBCS systems per KB 888609. 
            buffer = (LPTSTR)LocalAlloc(LPTR, buffersize * 2);
        }
        else
        {
            break;
        }
    }

    wprintf(L"%s %s\n",Label, buffer);

    if (buffer)
    {
        LocalFree(buffer);
    }
}



int main() 
{


    //int setupdi_version()
    //{
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD i;

    // Create a HDEVINFO with all present devices.
    hDevInfo = SetupDiGetClassDevs(
        NULL,
        0, // Enumerator
        0,
        DIGCF_PRESENT | DIGCF_ALLCLASSES);

    if (INVALID_HANDLE_VALUE == hDevInfo)
    {
        // Insert error handling here.
        return 1;
    }

    // Enumerate through all devices in Set.

    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
    {
        LPTSTR buffer = NULL;
        DWORD buffersize = 0;

        print_property(hDevInfo, DeviceInfoData, L"Friendly name :", SPDRP_FRIENDLYNAME);

        while (!SetupDiGetDeviceInstanceId(
            hDevInfo, 
            &DeviceInfoData, 
            buffer, 
            buffersize, 
            &buffersize))
        {
            if (buffer)
            {
                LocalFree(buffer);
            }

            if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
            {
                // Change the buffer size.
                // Double the size to avoid problems on
                // W2k MBCS systems per KB 888609.
                buffer = (LPTSTR)LocalAlloc(LPTR, buffersize * 2);
            }
            else
            {
                wprintf(L"error: could not get device instance id (0x%x)\n", GetLastError());
                break;
            }
        }

        if (buffer)
        {
            wprintf(L"\tDeviceInstanceId : %s\n", buffer);
        }

        print_property(hDevInfo, DeviceInfoData, L"\tClass :", SPDRP_CLASS);
        print_property(hDevInfo, DeviceInfoData, L"\tClass GUID :", SPDRP_CLASSGUID);
    }


    if (NO_ERROR != GetLastError() && ERROR_NO_MORE_ITEMS != GetLastError())
    {
        // Insert error handling here.
        return 1;
    }

    // Cleanup
    SetupDiDestroyDeviceInfoList(hDevInfo);

    system ("pause");

    return 0;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:33:16+00:00Added an answer on June 8, 2026 at 4:33 am

    The code will compile and run if you comment out the following two lines as shown:

    // int setupdi_version()
    // {
    

    I think the original code is from a function named setupdi_version() and it got mangled a bit when you tried to change it to main(). Note: it looks like the original source code is from here.

    To answer your follow-on problem. Those are linker errors. You need to tell Visual Studio which .lib file(s) to link against. You can do that in the Visual Studio project dependencies or just add the following to the top of the source code.

    #pragma comment(lib,"SetupAPI")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the code to list all the directories in a
i am trying to build a statics system i want list all pages in
What I am trying to achieve is writing a task which will list all
I'm trying to create a page by using view 2. This page list all
I am trying to develop an enterprise application which needs to list all the
I'm trying to list all WIA devices with C++/CLI. I'm fairly new to C++/CLI
I have an activity which implements a fragment where all of this code lives.
Im trying to list all categories in a VPP-folder using a module from Meridium
I am trying to list all the controllers and their actions with specific attributes
I am trying to list all the book_sales information for a particular book author.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.