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

时间:2015-08-04 06:57:42

标签: winapi handle createfile file-copying

这是我的一段代码:

    #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;
    }

我试图为新复制的文件&#34; temp.out&#34;打开一个句柄,但是会抛出一个错误而且它不起作用。

我的调试打印(printf("CreateFile Errorcode %d\n", GetLastError());) 打印&#34; CreateFile错误代码:2&#34; (找不到文件)但是发现它,因为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   成功创建了Handle   错误:无法将文件映射到内存   createMapFile错误代码:6

1 个答案:

答案 0 :(得分:0)

没有<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\0";

    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;
}