windows - 如果我调用 GetOpenFileNameA,GetOpenFileNameW 会导致 FNERR_INVALIDFILENAME 或 CDERR_INITIALIZATION

标签 windows d

这是使用 GetOpenFileNameW 的代码:

import core.sys.windows.windows;
import std.stdio, std.string, std.utf;

pragma(lib, "comdlg32");

// Fill in some missing holes in core.sys.windows.windows.
extern (Windows) DWORD CommDlgExtendedError();
enum OFN_FILEMUSTEXIST = 0x001000;

void main()
{
    auto buf = new wchar[1024];

    OPENFILENAMEW ofn;
    ofn.lStructSize = ofn.sizeof;
    ofn.lpstrFile = buf.ptr;
    ofn.nMaxFile = buf.length;
    ofn.lpstrInitialDir = null;
    ofn.Flags = OFN_FILEMUSTEXIST;

    BOOL retval = GetOpenFileNameW(&ofn);
    if (retval == 0) {
        // Get 0x3002 for W and 0x0002 for A. ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms646916(v=vs.85).aspx )
        throw new Exception(format("GetOpenFileName failure: 0x%04X.", CommDlgExtendedError()));
    }

    writeln(buf);
}

这导致 FNERR_INVALIDFILENAME,但我没有看到任何我未填写的非可选字符串。这是 GetOpenFileNameA:

auto buf = new char[1024];

OPENFILENAMEA ofn;
// ...
BOOL retval = GetOpenFileNameA(&ofn);

这导致 CDERR_INITIALIZATION,而 MSDN 给我的唯一说明是

The common dialog box function failed during initialization. 
This error often occurs when sufficient memory is not available.

这是在 Windows 7 64 位,DMD v2.059 上运行。

最佳答案

buf 必须完全归零。这里的问题是 wchar.init == wchar.max(出于错误检测原因),因此您的数组实际上是 wchar.max 的 1024 个实例。一个简单的 buf[] = 0; 应该可以解决这个问题。

关于windows - 如果我调用 GetOpenFileNameA,GetOpenFileNameW 会导致 FNERR_INVALIDFILENAME 或 CDERR_INITIALIZATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10393102/

相关文章:

windows - BASH:从字符串中去除换行符(读取行)

windows - 有没有办法在 Windows 10 中手动添加 Web 凭据?

windows - 使用用户模式转储确定 WinDbg 中的线程等待时间

c++ - 使用 Windows 行尾有效地将文件读取到 std::string

d - 从 D 中的网页读取数据?

xml - phobos 的 std.xml 的状态是什么

d - 如何使用 UDA 选择具有给定属性名称的所有成员?

windows - 在 Windows 上更改 .docker 目录

D 类、OOP 和 GTKd 笔记本

语: how to enforce an interface on a template function