无法创建句柄,或句柄无效 - WinAPI (CreateFile)

Cant create a handle, or handle is invalid - WinAPI (CreateFile)

这是我的一段代码:

    #include <stdio.h>
    #include <windows.h>
    #include <iostream.h>
    #include <signal.h>
    #include <tchar.h>
    #include <stdarg.h>

    int main(int argc, char * agrv[]) {

        //Convert argument 2 to wide char pointer
        wchar_t w[MAX_PATH];
        size_t size_of_w = sizeof(w);
        mbstowcs_s(&size_of_w, w, argv[1], MAX_PATH);
        LPWSTR pFile = w;

        //Copy original file to temp file
        if (!CopyFile(w, L"temp.out", False))
        {
            printf("Error: could not copy file.");
            printf("CopyFile Errorcode %d\n", GetLastError());
            return 1;
        }
        pFile = L"temp.out";

        HANDLE hFile = CreateFile(pFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

        if (hFile == INVALID_HANDLE_VALUE)
        {
            printf("Error: could not create handle to file");
            printf("CreateFile Errorcode %d\n", GetLastError());
            return 1;
        }
        printf("Successfully created Handle");
        return 0;
    }

我正在尝试为新复制的文件 "temp.out" 打开一个句柄,但抛出了一个错误并且不起作用。

我的调试打印 (printf("CreateFile Errorcode %d\n", GetLastError());) 打印 "CreateFile Errorcode: 2"(未找到文件)但已找到,因为 CopyFile 工作正常。

编辑: 我已经使用了绝对路径,现在,当我尝试使用句柄将文件映射到内存时,它会抛出“6”,这意味着句柄无效:

    HANDLE pMap = CreateFileMapping(hFile, NULL, PAGE_EXECUTE_READWRITE,0 ,0 ,NULL);
        LPVOID lpBase MapViewOfFile(pMap, FILE_MAP_ACCESS| FILE_MAP_EXECUTE, 0, 0, 0);
        printf("CreateFileMapping Errorcode %d\n", GetLastError());
        if (!lpBase)
        {
            printf("Error: could not map file to memory");
            printf("CreateFileMapping Errorcode %d\n", GetLastError());
            return 1;
        }
        printf("Successfully mapped file");

编辑 2:

我已将错误处理添加到 CopyFile,但它工作正常并且确实复制了文件。 现在的输出是:

program.exe shit.txt Successfully created Handle Error: could not map file to memory createMapFile errorcode: 6

没有<iostream.h>,考虑使用新的编译器!

您可以使用 wmain 来避免字符串转换。尽管您并不真正需要它,也不需要创建文件。使用 INVALID_HANDLE_VALUE 而不是 hFile

#include <iostream>
#include "windows.h"

int wmain(int argc, wchar_t* argv[])
{
    const int buflen = 1000;
    wchar_t* mapName = L"MyMapName";
    wchar_t* message = L"123[=10=]";

    HANDLE hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, buflen, mapName);
    if (!hFileMapping)
    {
        printf("CreateFileMapping error %d\n", GetLastError());
        return 1;
    }

    LPVOID buf = MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, buflen);
    if (!buf)
    {
        printf("MapViewOfFile error %d\n", GetLastError());
        CloseHandle(hFileMapping);
        return 1;
    }
    CopyMemory(buf, message, (wcslen(message) * sizeof(wchar_t)));
    system("pause");//wait until another program reads this data
    UnmapViewOfFile(buf);
    CloseHandle(hFileMapping);
    return 0;
}

如果您使用真实的文件句柄而不是 INVALID_HANDLE_VALUE,那么请改用此方法(注意,buflen 为零): CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, mapName);

在另一个程序中:

int wmain()
{
    const int buflen = 1000;
    wchar_t* mapName = L"MyMapName";
    HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapName);
    if (!hMapFile)
    {
        DWORD e = GetLastError();
        printf("OpenFileMapping %d\n", e);//EDITED: this line was missing
        return 1;
    }

    wchar_t* buf = (wchar_t*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, buflen);
    if (!pBuf)
    {
        DWORD e = GetLastError();    
        printf("MapViewOfFileerror %d\n", e);//EDITED: this line was missing
        CloseHandle(hMapFile);
        return 1;
    }
    wcout << buf << endl;
    UnmapViewOfFile(buf);
    CloseHandle(hMapFile);
    return 0;
}