gpt4 book ai didi

C++ CreateFile 无法读取文件 ERROR_ACCESS_DENIED

转载 作者:行者123 更新时间:2023-11-28 05:22:00 27 4
gpt4 key购买 nike

<分区>

我正在尝试读取文件内容,但尽管它能够写入同一文件。我无法从中读取!该程序以 Adminstator 身份运行。

我已尝试授予“FILE_SHARE_WRITE | FILE_SHARE_READ”权限,但仍然无效。

DWORD   dwBytesWritten = 0;
unsigned long BytesRead = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
wchar_t text_file[MAX_PATH] = { 0 };


TCHAR *save_text(void) {
OPENFILENAME ofn = { 0 };
TCHAR filename[512] = _T("C://Windows/EXAMPLE.txt");

ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = L"Txt files (*.txt)\0*.txt\0All Files\0*.*\0";
ofn.lpstrFile = filename;
ofn.nMaxFile = sizeof(filename);
ofn.Flags = OFN_NONETWORKBUTTON | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_LONGNAMES | OFN_EXPLORER | OFN_HIDEREADONLY;
ofn.nFilterIndex = 1;


return(filename);
}




void WriteToFile(TCHAR *wText)
{

wchar_t loginchar[1000];

hFile = CreateFile(text_file, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);

WriteFile(hFile, wText, wcslen(wText) * sizeof(wchar_t), &dwBytesWritten, NULL); // its writing without problem
ReadFile(hFile, loginchar, wcslen(loginchar) * sizeof(wchar_t), &BytesRead, NULL); // accses denied

ResultInFile(GetLastError()); // ResultInFile funcitons writes paramater to the file
//ResultInFile(BytesRead); // to see how many bytes read, but of course doesnt work..
CloseHandle(hFile);
}

// this is the how file created at main function :
hFile = CreateFile(txt_file, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com