You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2019下Hello World驱动编译失败,遇C2220错误求助

Got it, let's work through your driver compilation issues step by step—they're all tied to warning handling, character encoding, and SDK compatibility in VS2019. Here's the breakdown and fixes:

Core Root Cause

The critical Error C2220 is triggered because your project is configured to treat all warnings as errors. The underlying warnings (like C4566, C4100) and SDK compatibility error (E1097) are what's causing this failure. Let's fix each issue:

1. Full-width Character Warning (C4566)

  • Why it happens: The exclamation marks in your DbgPrint calls are full-width Chinese characters (!), which can't be represented in the default code page (1252) used by VS2019 for driver projects.
  • Fix: Replace all full-width ! with standard half-width ! in your string literals.

2. Unreferenced Formal Parameters (C4100)

  • Why it happens: You defined the UNREFERENCED_PARAMETER macro but didn't actually use it to mark the unused driver and pUnicodeString parameters. The compiler still flags these as warnings.
  • Fix: Add the macro calls inside your functions to explicitly mark the parameters as unused:
    VOID DriverUnload(PDRIVER_OBJECT driver) {
        UNREFERENCED_PARAMETER(driver); // Add this line
        DbgPrint("first:HelloWorld End!");
    }
    
    Do the same for pUnicodeString in DriverEntry.

3. Unknown "no_init_all" Attribute (E1097)

  • Why it happens: Newer versions of the Windows SDK use the no_init_all attribute, which isn't supported by the VS2019 compiler toolset.
  • Fix:
    • Right-click your project → Properties → Configuration Properties → General → Set Windows SDK Version to a VS2019-compatible release (e.g., 10.0.18362.0 or earlier).
    • If adjusting the SDK version isn't an option, go to Configuration Properties → C/C++ → Advanced → Add E1097 to Disable Specific Warnings as a temporary workaround.

4. Missing Spectre Mitigation Libraries (MSB8038)

  • Why it happens: Your project has Spectre mitigation enabled, but the required libraries aren't installed with VS2019.
  • Fix:
    • Option 1 (Simpler): Disable Spectre mitigation. Go to Configuration Properties → C/C++ → Code Generation → Set Spectre Mitigation to Disabled.
    • Option 2 (For Security-Critical Projects): Open the Visual Studio Installer, modify your VS2019 installation, and check the C++ Spectre Mitigation Libraries component under the "Desktop development with C++" workload. Install it and recompile.

5. Optional: Disable "Warnings as Errors"

If you've fixed all underlying warnings but still want to avoid this issue in the future, you can turn off the "treat warnings as errors" setting:

  • Right-click project → Properties → Configuration Properties → C/C++ → General → Set Treat Warnings As Errors to No.

Corrected Code

Here's your code with all fixes applied:

#include <ntddk.h>
#include <wdf.h>
#define UNREFERENCED_PARAMETER(P) (P)

VOID DriverUnload(PDRIVER_OBJECT driver) {
    UNREFERENCED_PARAMETER(driver);
    DbgPrint("first:HelloWorld End!");
}

NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pUnicodeString) {
    UNREFERENCED_PARAMETER(pUnicodeString);
    DbgPrint("first:HelloWorld Begin!");
    pDriverObject->DriverUnload = DriverUnload;
    return STATUS_SUCCESS;
}

内容的提问来源于stack exchange,提问作者Isildor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:43:31