【问题标题】:Local function definitions are rejected by Visual Studio本地函数定义被 Visual Studio 拒绝
【发布时间】:2012-07-22 19:36:37
【问题描述】:

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

或者是我的问题,因为我正在尝试使用来自 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;

}

【问题讨论】:

  • 如果你修正了你的代码格式,你会更容易看到你有不平衡的花括号和某个地方的错字。看起来它在int setupdi_version() 行周围开始出错了。
  • 我已经修复了缩进,但正如@Blastfurnace 所说,仍然存在问题。 setupdi_version() 的定义似乎开始于 main() 的定义内部——但没有对 setupdi_version() 的调用,并且缺少右括号。你能告诉我们你在哪里找到这个代码吗? #include &lt;string&gt; 暗示这是 C++ 代码。
  • 缩进真的很重要。我编辑了您的帖子以更正缩进,然后您的编辑再次左对齐。 ...我刚刚再次修复了缩进。
  • 删除代码有什么意义?!

标签: c tags device


【解决方案1】:

您在main 的主体中定义了另一个函数;这是无效的 C. 将其移到 main 之外。

【讨论】:

  • 一些编译器可能允许它作为扩展(例如,我相信 gcc 可以),但它在 C 和 C++ 中都是非标准的。
  • 感谢您的回复,我试图将 int setupdi_version() 部分移到 main 之外会引发大约 29 个错误。有没有一种很好的方法来格式化这个,或者我应该把它装箱并在别处寻找?
  • "如果您要在函数调用中间换行,请将第二行缩进 函数调用的左括号"好的开始。在我的时代,我见过很多古怪的缩进,但仅在外部 whileif 声明中缩进参数......!
  • 谢谢,我在博客上发现它来自 c++,但我试图将它转换为 c,因为我创建了一个通过 USB 读取手机的应用程序,但设备路径略有变化,所以我试图获取拉路径 vid+pid+guid。我正在寻找 C 中 SetupDiGetDeviceInstanceId 和 SetupDiGetClassDevs 的工作示例。我在 MSDN 上阅读过,但有点混乱,只有 c++ 示例。
  • @CorrieDuck:编译器并不关心代码的格式(尽管任何阅读它的人都会关心)。
【解决方案2】:

注释掉如下两行代码就可以编译运行了:

// int setupdi_version()
// {

我认为原始代码来自一个名为 setupdi_version() 的函数,当您尝试将其更改为 main() 时,它有点混乱。注意:看起来原始源代码来自here

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

#pragma comment(lib,"SetupAPI")

【讨论】:

  • 感谢该网站是出色的侦探工作 :-) 但是,当我将那部分鬼怪出来时,我现在得到了未解决的外部问题。例如 Error 2 error LNK2019: unresolved external symbol __imp__SetupDiDestroyDeviceInfoList@4 referenced in function _main c:\Users\K\documents\visual studio 2010\Projects\SetupDI\SetupDI\main.obj SetupDI
  • @CorrieDuck:我为链接器问题添加了修复程序。
  • 非常感谢,它现在可以正常工作了,非常感谢您的帮助,这几天来一直让我发疯,试图获取此信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多