# FileChooser - filter implemented incorrectly on windows when using native dialog

**URL:** <https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005>\
**Category:** General JUCE discussion\
**Created:** [June 18, 2015, 3:49pm UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005 "2015-06-18T15:49:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kroko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kroko/32/1151_2.png) [@Kroko](https://forum.juce.com/u/Kroko)\
**Post date:** [June 18, 2015, 3:49pm UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/1 "2015-06-18T15:49:46Z")

</div>

I'm using Windows 7, latest Juce version.  
Problem is in file juce\_win32\_FileChooser.cpp around line 224 where "filter" is copied to "filters".&nbsp;  
Windows OPENFILENAMEW structure uses \0 characters to separate filters. This is ignored in juce implementation.  
  
This is copy/paste from MSDN  
lpstrFilter  
Type: LPCTSTR  
A buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters.  
The first string in each pair is a display string that describes the filter (for example, "Text Files"), and the second string specifies the filter pattern (for example, "\*.TXT"). To specify multiple filter patterns for a single display string, use a semicolon to separate the patterns (for example, "\*.TXT;\*.DOC;\*.BAK"). A pattern string can be a combination of valid file name characters and the asterisk (\*) wildcard character. Do not include spaces in the pattern string.  
The system does not change the order of the filters. It displays them in the File Types combo box in the order specified in lpstrFilter.  
If lpstrFilter is NULL, the dialog box does not display any filters.  
In the case of a shortcut, if no filter is set, GetOpenFileName and GetSaveFileName retrieve the name of the .lnk file, not its target. This behavior is the same as setting the OFN\_NODEREFERENCELINKS flag in the Flags member. To retrieve a shortcut's target without filtering, use the string "All Files\0\*.\*\0\0".  
&nbsp;

---

<div class="post-metadata">

**Author:** ![Kroko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kroko/32/1151_2.png) [@Kroko](https://forum.juce.com/u/Kroko)\
**Post date:** [June 18, 2015, 4:12pm UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/2 "2015-06-18T16:12:44Z")

</div>

I managed to fix it by adding couple of lines to juce\_win32\_FileChooser.cpp and&nbsp;replacing '|' characters with \0.

&nbsp;

```
filter.copyToUTF16(filters + (bytesWritten / sizeof(WCHAR)),
            ((filterSpaceNumChars - 1) * sizeof(WCHAR) - bytesWritten)); **for (int i = 0; i \< filterSpaceNumChars; i++) if (filters[i] == '|') filters[i] = '\0';**OPENFILENAMEW of = { 0 };
```

I call FileChooser like this now:

```
FileChooser fc("Open", File::getCurrentWorkingDirectory(), "All Files|*.*|G-Code Files|*.nc;*.tap;*.cnc;*.iso;*.gcode;*.ncf;*.txt|DXF Files|*.dxf||", useNativeVersion);
```

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Kroko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kroko/32/1151_2.png) [@Kroko](https://forum.juce.com/u/Kroko)\
**Post date:** [June 19, 2015, 11:35am UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/3 "2015-06-19T11:35:13Z")

</div>

It alo does not work correctly in Linux with Zenity

I fixed it by replacing this

```
args.add ("--file-filter");
args.add (filters.replaceCharacter (';', ' '));

args.add ("--file-filter");
args.add ("All files | *");
```

to this

```
StringArray tokens;
tokens.addTokens(filters, ";", "\"");
for (int i = 0; i < tokens.size(); i++)
{
    args.add("--file-filter=" + tokens[i]);
}
```

in file&nbsp;juce\_linux\_FileChooser.cpp

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 24, 2015, 3:49pm UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/4 "2015-06-24T15:49:02Z")

</div>

Ah, good catch - thanks! Will sort that out..

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [June 24, 2015, 4:38pm UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/5 "2015-06-24T16:38:52Z")

</div>

G-Code files? You're parsing CAM files using JUCE? I guess you must be controlling some interesting&nbsp;machines in the same application?  
  
Care to share (show-case?) your software? :)

---

<div class="post-metadata">

**Author:** ![falkTX](https://avatars.discourse-cdn.com/v4/letter/f/9f8e36/32.png) [@falkTX](https://forum.juce.com/u/falkTX)\
**Post date:** [June 24, 2015, 7:46pm UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/6 "2015-06-24T19:46:22Z")

</div>

So you're adding multiple "--file-filter=\<file\>" arguments?  
Are you sure that works?  
&nbsp;

---

<div class="post-metadata">

**Author:** ![Kroko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kroko/32/1151_2.png) [@Kroko](https://forum.juce.com/u/Kroko)\
**Post date:** [July 9, 2015, 10:17am UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/7 "2015-07-09T10:17:22Z")

</div>

Yes, multiple&nbsp;--file-filter arguments must&nbsp;be used.

---

<div class="post-metadata">

**Author:** ![Kroko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kroko/32/1151_2.png) [@Kroko](https://forum.juce.com/u/Kroko)\
**Post date:** [July 9, 2015, 10:19am UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/8 "2015-07-09T10:19:56Z")

</div>

You can get more information about my software on my web site&nbsp;www.planet-cnc.com

---

<div class="post-metadata">

**Author:** ![Kroko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kroko/32/1151_2.png) [@Kroko](https://forum.juce.com/u/Kroko)\
**Post date:** [July 30, 2015, 10:30pm UTC](https://forum.juce.com/t/filechooser-filter-implemented-incorrectly-on-windows-when-using-native-dialog/15005/9 "2015-07-30T22:30:42Z")

</div>

It is still not OK. You also need to remove those two&nbsp;lines:

```
filter.copyToUTF16 (filters + (bytesWritten / sizeof (WCHAR)),
                            ((filterSpaceNumChars - 1) * sizeof (WCHAR) - bytesWritten));
```

file: juce\_win32\_FileChooser.cpp line: 225

&nbsp;
