c++ - WinAPI 创建文件函数

标签 c++ winapi

<分区>

我必须用 C++ 编写 CreateFile 函数 我写了这个,但是没有用

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
    HANDLE hfile = CreateFile(
           ".\temp.txt",                // "\\.\C:" 
           GENERIC_READ, 
           0,
           NULL,
           CREATE_NEW,
           FILE_ATTRIBUTE_NORMAL,
           NULL);

    //if (hFile == INVALID_HANDLE_VALUE) cout << "Unable to create file \n";
    return 0;
}

error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [10]' to 'LPCWSTR'

我该如何修复这个错误?

最佳答案

错误消息告诉您它正在尝试调用 CreateFileW,这是 CreateFile 的宽字符版本。您需要向它传递一个宽字 rune 字而不是常规字 rune 字;

hfile = CreateFile(
       L".\\temp.txt",               // Notice the L for a wide char literal 
       GENERIC_READ, 
       0,
       NULL,
       CREATE_NEW,
       FILE_ATTRIBUTE_NORMAL,
       NULL);

更多信息 can be found here .

关于c++ - WinAPI 创建文件函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12546384/

相关文章:

c# - 停止 SetVolumeMountPoint 打开文件资源管理器

c++ - 设置无效值后,注册表项是否会采用默认值

c++ - 在可变参数模板中实现 STL 函数

c++ - 拦截窗口鼠标点击

c++ - 如何检测win32应用程序中显示器的热插拔?

c++ - 如何在 Windows XP 中获取快速启动栏的大小?

C++ 不断收到错误 LNK2019 : unresolved external symbol

c++ - Doxygen show 功能简述

c++ - 功能模板返回类型

c++ - 使 wpcap.dll "optional"