5

这是使用的代码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 上。

4

1 回答 1

5

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

于 2012-05-01T03:29:30.290 回答