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()。

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

User's image

User's image

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

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

Windows 10
Windows 10
在个人计算机和平板电脑上运行的 Microsoft 操作系统。
62 个问题
Visual Studio
Visual Studio
一系列 Microsoft 集成开发工具套件,用于生成适用于 Windows、Web 和移动设备的应用程序。
56 个问题
Windows API - Win32
Windows API - Win32
一组适用于桌面和服务器应用程序的核心 Windows 应用程序编程接口 (API)。 以前称为 Win32 API。
49 个问题
C++
C++
一种通用的高级编程语言,作为 C 编程语言的扩展而创建,除了用于低级别内存操作的功能外,还具有面向对象、泛型和功能性等特点。
81 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Xiaopo Yang - MSFT 11,506 信誉分 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 9,181 信誉分 Microsoft 供应商
    2024-04-03T01:03:48.8866667+00:00

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

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

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