gpt4 book ai didi

windows-8 - CreateFile2 在 Windows 8 中返回访问被拒绝错误

转载 作者:行者123 更新时间:2023-12-02 22:21:19 24 4
gpt4 key购买 nike

我编写了以下代码行来打开 InstalledFolder 目录下的文件:

Platform::String^ locationPath = Platform::String::Concat(Package::Current->InstalledLocation->Path, "\\Assets\\Logo.png");

CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {0};
extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
extendedParams.lpSecurityAttributes = nullptr;
extendedParams.hTemplateFile = nullptr;

Wrappers::FileHandle file(
CreateFile2(
locationPath->Data(),
GENERIC_READ,
0,
OPEN_EXISTING,
&extendedParams
)
);

DWORD e = GetLastError();
if (file.Get() == INVALID_HANDLE_VALUE)
{
throw ref new Platform::FailureException();
}

CreateFile2 返回访问被拒绝错误。谁能帮帮我吗?

根据 JP Alioto 的建议,我尝试使用 WinRT 文件 I/O 如下

    create_task(StorageFile::GetFileFromApplicationUriAsync(ref new Windows::Foundation::Uri("ms-appx:///Assets/Logo.png")))
.then([=](StorageFile^ f)
{
auto p = create_task(f->OpenAsync(FileAccessMode::Read));
p.wait();
});

我在 p.wait() 处仍然收到以下错误:

An invalid parameter was passed to a function that considers invalid parameters fatal

谢谢

最佳答案

您正在为 dwShareMode 传递 0CreateFile2 的文档说这个值...

Prevents other processes from opening a file or device if they request delete, read, or write access. Exclusive access to a file or directory is only granted if the application has write access to the file.

您没有对包内文件的写访问权限,这就是您收到访问被拒绝错误的原因。您需要将共享模式设置为FILE_SHARE_READ

关于windows-8 - CreateFile2 在 Windows 8 中返回访问被拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152096/

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