VS2022 C++ GetLastError()的编译问题

wuyifan 45 信誉分
2024-04-02T09:38:15.3433333+00:00

一段祖传代码要移植到VS2022,结果编译出现error,请大神帮忙看一下 代码如下:

#ifdef errno #undef errno #endif #define errno GetLastError()

这段代码本身是没有问题的,即将errno强行指向GetLastError()。

但是编译时出现下面的异常:

error:

error: E0461-非常量引用的初始值必须为左值 C2440 无法从“DWORD”转换为“int &”

求大佬们帮忙看一下解决方法。PS:系统配置 OS:windows10 VS:Visual Studio 2022C++:17.0

Windows 开发 | Windows API - Win32
Windows 商业版 | 面向 IT 专业人士的 Windows 客户端 | 用户体验 | 其他
开发人员技术 | C++
开发人员技术 | Visual Studio | 其他
0 个注释 无注释
{count} 票

接受的答案
  1. Xiaopo Yang - MSFT 12,731 信誉分 Microsoft 外部员工
    2024-04-03T05:52:56.3533333+00:00

    根据once pragma:

    The use of #pragma once can reduce build times, as the compiler won't open and read the file again after the first #include of the file in the translation unit.

    所以如果你的宏定义在有errno定义的头文件和xlocnum之间的话,会覆盖之后的errno宏定义即使xlocnum也已经include那些头文件.

    #include <Windows.h>
    #include <errno.h>
    #include <stddef.h>
    #include <stdlib.h>
    #ifdef errno
    #undef errno
    #endif
    #define errno GetLastError()
    #include <xlocnum>
    
    1 个人认为此答案很有帮助。

1 个其他答案

排序依据: 非常有帮助
  1. Jeanine Zhang-MSFT 11,356 信誉分 Microsoft 外部员工
    2024-04-03T01:03:48.8866667+00:00

    error: E0461-非常量引用的初始值必须为左值 C2440 无法从“DWORD”转换为“int &”

    我建议你使用DWORD&而不是int&.

    2 个人认为此答案很有帮助。

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。