加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 如果我调用GetOpenFileNameA,GetOpenFileNameW会导致

发布时间:2020-12-14 01:52:48 所属栏目:Windows 来源:网络整理
导读:这是使用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_FI
这是使用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_INIALALIZATION,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(出于错误检测的原因),所以你的数组基本上是1024个wchar.max实例.一个简单的buf [] = 0;应该解决这个问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读