January 11, 2020
On Saturday, 11 January 2020 at 12:22:25 UTC, Marcone wrote:
>     wchar[1024] szFileName = 0;
>     ofn.lpstrFile = cast(LPWSTR) szFileName;

You shouldn't cast there, just use `szFileName.ptr` instead.

>     ofn.nMaxFile = MAX_PATH;

and this should be the length of the array. It may require a case

nMaxFile = cast(DWORD) szFileName.length;
January 12, 2020
On Saturday, 11 January 2020 at 14:21:25 UTC, Adam D. Ruppe wrote:
> On Saturday, 11 January 2020 at 12:22:25 UTC, Marcone wrote:
>>     wchar[1024] szFileName = 0;
>>     ofn.lpstrFile = cast(LPWSTR) szFileName;
>
> You shouldn't cast there, just use `szFileName.ptr` instead.
>
>>     ofn.nMaxFile = MAX_PATH;
>
> and this should be the length of the array. It may require a case
>
> nMaxFile = cast(DWORD) szFileName.length;

Changed! New code with changes working very well:

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

// Function askopenfilename()
string askopenfilename(const(wchar)* filter = "All Files (*.*)\0*.*\0"){
    OPENFILENAME ofn;
    wchar[1024] szFileName = 0;
    ofn.lpstrFile = szFileName.ptr;
    ofn.lpstrFilter = filter;
    ofn.nMaxFile = cast(DWORD) szFileName.length;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    if (GetOpenFileNameW(&ofn)){
        return to!string(szFileName[0..szFileName.indexOf('\0')]);
    } else {
        return "";
    }
}


void main(){
    writeln(askopenfilename()); // Call without filter.
    writeln(askopenfilename("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0")); // Cal using filter.
}
1 2
Next ›   Last »