DEV9: Use UTF8 strings on windows for AdapterEntry

This commit is contained in:
TheLastRar 2022-02-06 15:20:38 +00:00 committed by lightningterror
parent 8aaeb1c7ce
commit 30bc9b04c5
4 changed files with 21 additions and 22 deletions

View File

@ -27,6 +27,8 @@
#include <wx/spinctrl.h> #include <wx/spinctrl.h>
#include <wx/gbsizer.h> #include <wx/gbsizer.h>
#include "common/StringUtil.h"
#include "Config.h" #include "Config.h"
#include "DEV9.h" #include "DEV9.h"
#include "pcap_io.h" #include "pcap_io.h"
@ -319,8 +321,9 @@ public:
current = config.Eth; current = config.Eth;
for (size_t i = 0; i < list.size(); i++) for (size_t i = 0; i < list.size(); i++)
{ {
options.Add(list[i].name); wxString wxAdapterName = StringUtil::UTF8StringToWxString(list[i].name);
if (list[i].name == current) options.Add(wxAdapterName);
if (wxAdapterName == current)
selection = i + 1; selection = i + 1;
} }
} }

View File

@ -16,6 +16,7 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include "common/RedtapeWindows.h" #include "common/RedtapeWindows.h"
#include "common/StringUtil.h"
#include <stdio.h> #include <stdio.h>
#include <windows.h> #include <windows.h>
@ -186,9 +187,9 @@ std::vector<AdapterEntry> TAPAdapter::GetAdapters()
if (IsTAPDevice(enum_name)) if (IsTAPDevice(enum_name))
{ {
AdapterEntry t; AdapterEntry t;
t.type = NetApi::TAP; t.type = Pcsx2Config::DEV9Options::NetApi::TAP;
t.name = std::wstring(name_data); t.name = StringUtil::WideStringToUTF8String(std::wstring(name_data));
t.guid = std::wstring(enum_name); t.guid = StringUtil::WideStringToUTF8String(std::wstring(enum_name));
tap_nic.push_back(t); tap_nic.push_back(t);
} }
} }

View File

@ -69,13 +69,9 @@ enum struct NetApi : int
struct AdapterEntry struct AdapterEntry
{ {
NetApi type; NetApi type;
#ifdef _WIN32 //UTF8
std::wstring name;
std::wstring guid;
#else
std::string name; std::string name;
std::string guid; std::string guid;
#endif
}; };
class NetAdapter class NetAdapter

View File

@ -22,6 +22,7 @@
#include <iphlpapi.h> #include <iphlpapi.h>
//#include <ws2tcpip.h> //#include <ws2tcpip.h>
//#include <comdef.h> //#include <comdef.h>
#include "common/StringUtil.h"
#elif defined(__linux__) #elif defined(__linux__)
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if.h> #include <net/if.h>
@ -502,29 +503,27 @@ std::vector<AdapterEntry> PCAPAdapter::GetAdapters()
entry.type = NetApi::PCAP_Switched; entry.type = NetApi::PCAP_Switched;
#ifdef _WIN32 #ifdef _WIN32
//guid //guid
wchar_t wEth[sizeof(config.Eth)] = {0}; entry.guid = std::string(d->name);
mbstowcs(wEth, d->name, sizeof(config.Eth) - 1);
entry.guid = std::wstring(wEth);
IP_ADAPTER_ADDRESSES adapterInfo; IP_ADAPTER_ADDRESSES adapterInfo;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer; std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
if (PCAPGetWin32Adapter(d->name, &adapterInfo, &buffer)) if (PCAPGetWin32Adapter(entry.guid, &adapterInfo, &buffer))
entry.name = std::wstring(adapterInfo.FriendlyName); entry.name = StringUtil::WideStringToUTF8String(std::wstring(adapterInfo.FriendlyName));
else else
{ {
//have to use description //have to use description
//NPCAP 1.10 is using an version of pcap that dosn't //NPCAP 1.10 is using an version of pcap that dosn't
//allow us to set it to use UTF8 //allow us to set it to use UTF8
//see https://github.com/nmap/npcap/issues/276 //see https://github.com/nmap/npcap/issues/276
//But use MultiByteToWideChar so we can later switch to UTF8 //We have to convert from ANSI to wstring, to then convert to UTF8
int len_desc = strlen(d->description) + 1; const int len_desc = strlen(d->description) + 1;
int len_buf = MultiByteToWideChar(CP_ACP, 0, d->description, len_desc, nullptr, 0); const int len_buf = MultiByteToWideChar(CP_ACP, 0, d->description, len_desc, nullptr, 0);
wchar_t* buf = new wchar_t[len_buf]; std::unique_ptr<wchar_t[]> buf = std::make_unique<wchar_t[]>(len_buf);
MultiByteToWideChar(CP_ACP, 0, d->description, len_desc, buf, len_buf); MultiByteToWideChar(CP_ACP, 0, d->description, len_desc, buf.get(), len_buf);
entry.name = std::wstring(buf);
delete[] buf; entry.name = StringUtil::WideStringToUTF8String(std::wstring(buf.get()));
} }
#else #else
entry.name = std::string(d->name); entry.name = std::string(d->name);