On Saturday, 11 January 2020 at 10:34:34 UTC, Marcone wrote:
This code works, but I can't get file Path. Someone can help me?

import std;
import core.sys.windows.windows;
pragma(lib, "comdlg32");

void main(){
    OPENFILENAME ofn;
    wchar* szFileName;

You need to supply a buffer, not a pointer:

  wchar[MAX_PATH] szFileName;

    (cast(byte*)& ofn)[0 .. ofn.sizeof] = 0;
    ofn.lStructSize = ofn.sizeof;
    ofn.hwndOwner = null;

The above lines are unnecessary as D structs are automatically initialized and lStructSize is already filled in.

ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;

It wants a pointer to your buffer:

  ofn.lpstrFile = szFileName.ptr;

    ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "txt";
    if(GetOpenFileNameW(&ofn)){
      writeln(szFileName); // Print null
      writeln(ofn.lpstrFile); // Print null

You'll need to slice the buffer to the right length:

  import core.stdc.wchar_ : wcslen;
  writeln(ofn.lpstrFile[0 .. wcslen(ofn.lpstrFile.ptr)]);

    }
}


Reply via email to