Skip to content

Commit 956f657

Browse files
Merge pull request #25 from vasilevp/fix/cyrillic
Fix non-Latin characters in device names
2 parents bb8c990 + 25387c9 commit 956f657

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Form1.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ public partial class Form1 : Form
2222
private bool InitDone = false;
2323
private string TOMLPath;
2424
private FlexGUIConfig flexGUIConfig;
25-
private System.Text.Encoding enc1252;
25+
private Encoding legacyEncoding;
2626
private string flexasioGuiVersion = "0.31";
2727
private string flexasioVersion = "1.8";
2828
private string tomlName = "FlexASIO.toml";
2929
private string docUrl = "https://github.com/dechamps/FlexASIO/blob/master/CONFIGURATION.md";
3030

3131
[DllImport(@"C:\Program Files\FlexASIO\x64\FlexASIO.dll")]
3232
public static extern int Initialize(string PathName, bool TestMode);
33+
[DllImport(@"kernel32.dll")]
34+
public static extern uint GetACP();
3335

3436
public Form1()
3537
{
@@ -40,7 +42,10 @@ public Form1()
4042
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
4143
customCulture.NumberFormat.NumberDecimalSeparator = ".";
4244
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
43-
enc1252 = Encoding.GetEncoding(1252);
45+
46+
// get the value of the "Language for non-Unicode programs" setting (1252 for English)
47+
// note: in Win11 this could be UTF-8 already, since it's natively supported
48+
legacyEncoding = Encoding.GetEncoding((int)GetACP());
4449

4550
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
4651
CultureInfo.DefaultThreadCurrentCulture = customCulture;
@@ -110,11 +115,12 @@ public Form1()
110115
GenerateOutput();
111116
}
112117

113-
private string W1252fromUTF(string s)
118+
private string DescrambleUTF8(string s)
114119
{
115-
byte[] bytes = Encoding.Default.GetBytes(s);
116-
byte[] output = Encoding.Convert(Encoding.UTF8, enc1252, bytes);
117-
return Encoding.UTF8.GetString(output);
120+
// portaudio incorrectly returns UTF-8 strings as if they were ANSI (CP1252 for most Latin systems, CP1251 for Cyrillic, etc...)
121+
// this line fixes the issue by reading the input as CP* and parsing it as UTF-8
122+
var bytes = legacyEncoding.GetBytes(s);
123+
return Encoding.UTF8.GetString(bytes);
118124
}
119125

120126
private TreeNode[] GetDevicesForBackend(string Backend, bool Input)
@@ -134,14 +140,14 @@ private TreeNode[] GetDevicesForBackend(string Backend, bool Input)
134140
{
135141
if (deviceInfo.maxInputChannels > 0)
136142
{
137-
treeNodes.Add(new TreeNode(W1252fromUTF(deviceInfo.name)));
143+
treeNodes.Add(new TreeNode(DescrambleUTF8(deviceInfo.name)));
138144
}
139145
}
140146
else
141147
{
142148
if (deviceInfo.maxOutputChannels > 0)
143149
{
144-
treeNodes.Add(new TreeNode(W1252fromUTF(deviceInfo.name)));
150+
treeNodes.Add(new TreeNode(DescrambleUTF8(deviceInfo.name)));
145151
}
146152
}
147153
}

0 commit comments

Comments
 (0)