无法创建句柄,或句柄无效-WinAPI(CreateFile)
这是我的一段代码:无法创建句柄,或句柄无效-WinAPI(CreateFile),winapi,handle,createfile,file-copying,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
#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;
}
编辑2:
我在CopyFile中添加了错误处理,但它可以正常工作并复制文件。
现在输出为:
program.exe.txt
已成功创建句柄
错误:无法将文件映射到内存
createMapFile错误代码:6
没有<代码> <代码>,考虑使用一个新的编译器! 您可以使用
wmain
来避免字符串转换。虽然您并不真正需要它,也不需要创建文件。使用无效的\u句柄\u值
而不是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;
}
目录必须退出。您在错误的时间调用了
GetLastError
。如果函数失败,则特别注意调用GetLastError
。你在无条件地调用它。我想知道为什么这么多人会犯这个基本错误。@johnnybravo,除非CreateFile
返回INVALID\u HANDLE\u VALUE
你没有错误要处理。在下一个函数中,你也在做同样的事情(实际上,代码甚至不会如图所示编译)。除非出现故障,否则不要调用GETLASTERROR()。
#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;
}
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;
}