代码之家  ›  专栏  ›  技术社区  ›  Clive Atkins

Visual Studio拒绝本地函数定义

  •  2
  • Clive Atkins  · 技术社区  · 13 年前

    我正在尝试列出连接到我的系统的所有设备,在搜索后发现这个代码会引发错误,本地函数定义是非法的,有人能解释一下它的意思吗。

    或者我的问题是因为我试图使用C++中的代码。谢谢

    工作代码

    #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;
    
    }
    
    2 回复  |  直到 13 年前
        1
  •  1
  •   R.. GitHub STOP HELPING ICE    13 年前

    在的正文中定义了另一个函数 main ;这是无效的C.把它移出 主要的

        2
  •  0
  •   Blastfurnace    13 年前

    如果注释掉以下两行,代码将编译并运行,如图所示:

    // int setupdi_version()
    // {
    

    我认为原始代码来自一个名为 setupdi_version() 当你试图把它改成 main() 。注意:看起来原始源代码来自 here

    回答你的后续问题。这些都是链接器错误。您需要告诉Visual Studio .lib 要链接的文件。您可以在VisualStudio项目依赖项中执行此操作,也可以将以下内容添加到源代码的顶部。

    #pragma comment(lib,"SetupAPI")