Win32 Api: How create Open/"Save as" Dialog?

John Chapman johnch_atms at hotmail.com
Sat Jan 11 11:42:52 UTC 2020


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)]);

>     }
> }




More information about the Digitalmars-d-learn mailing list