Hello,
I’m planning to change IP address on ethernet port using Cpp program ( Qt/qml + Cpp).
I have been refering this link for solution.
It works correctly if I hardcode ip address like L"192.168.225.045".
But I’m taking input from user via QML interface in QString variable.
I’m unable to convert QString to BYTE *
Working code:
HKEY hKey;
int res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Comm\\ENET1\\Parms\\TcpIp", 0, KEY_WRITE, &hKey);
const BYTE* data = (BYTE*)(L"192.168.225.045");
long ret = RegSetValueEx(hKey,L"IpAddress",0,REG_MULTI_SZ,data,16);
Not working code:
func(QString wifi_ip)
{
std::string wifi_ip_str = wifi_ip.toUtf8().constData();
const char *text = (wifi_ip_str.c_str());
HKEY hKey;
int res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Comm\\ENET1\\Parms\\TcpIp", 0, KEY_WRITE, &hKey);
const BYTE* data = (BYTE*)text;
long ret = RegSetValueEx(hKey,L"IpAddress",0,REG_MULTI_SZ,data,16);
}
I believe the last arg in RegSetValueEx() is the total size of the data field including the terminating character(s). I have code that looks like the following where the ipAddress is stored in a long int:
The WIN32 docs say it must include the terminating character(s):
[in] lpData
The data to be stored.
For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters.
[in] cbData
The size of the information pointed to by the lpData parameter, in bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters.
The issue with your code is that you are using the constData() function of QString to get a pointer to the UTF-8 encoded data. However, the constData() function returns a const char *, while data needs to be a const BYTE * (which is an alias for const unsigned char *).
To fix this, you can use the toLatin1() function of QString to get a QByteArray containing the Latin-1 (ISO-8859-1) encoded data, and then use the data() function of QByteArray to get a pointer to the raw data as const char *. You can then cast this pointer to const BYTE *:
The issue with your code is that you are using the constData() function of QString to get a pointer to the UTF-8 encoded data. However, the constData() function returns a const char *, while data needs to be a const BYTE * (which is an alias for const unsigned char *).
To fix this, you can use the toLatin1() function of QString to get a QByteArray containing the Latin-1 (ISO-8859-1) encoded data, and then use the data() function of QByteArray to get a pointer to the raw data as const char *. You can then cast this pointer to const BYTE *:
void func(QString wifi_ip)
{
QByteArray wifi_ip_bytes = wifi_ip.toLatin1();
const BYTE* data = reinterpret_cast<const BYTE*>(wifi_ip_bytes.data());
HKEY hKey;
int res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Comm\ENET1\Parms\TcpIp", 0, KEY_WRITE, &hKey);
long ret = RegSetValueEx(hKey, L"IpAddress", 0, REG_MULTI_SZ, data, wifi_ip_bytes.size() + 1);
// …
}