I am trying to change the NIC speed through the registry.. But my problem is that I dont fully understand the command RegSetValueEx. I have checked the Internet up and down for some good examples but have fallen short.. I am tring to set the default value for the NIOC speed to be 0 (Auto). Here is what I have for that part..

LPTSTR lptstrRegSubKey;
LPCTSTR Caption2 = "Speed Change Successfull!";
HKEY hKey;

if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\0001\\Ndi\\Params\\*SpeedDuplex\\enum", 0, KEY_SET_VALUE, &hKey) == ERROR_SUCCESS)
{
    lptstrRegSubKey = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\0001\\Ndi\\Params\\*SpeedDuplex\\default";
    RegCloseKey (hKey);
}

if (RegSetValueEx(HKEY_LOCAL_MACHINE, lptstrRegSubKey, 0, REG_DWORD,   (PBYTE) 0, sizeof(0))==ERROR_SUCCESS)
{
     MessageBox(NULL, "NIC Speed was changed to Auto..", Caption2,MB_OK);
}

Dani AI

Generated

already solved it, but a concise explanation and a safe pattern are useful for others who find this thread later.

The original problem is a common API misuse: RegSetValueEx expects a handle to an already-open registry key (opened with KEY_SET_VALUE), the value name (or NULL for the unnamed/"(Default)" value), a pointer to the data buffer, and the size in bytes. Passing HKEY_LOCAL_MACHINE plus a path as the lpValueName, or casting the literal 0 to a pointer, will fail. Validate every API return value to detect access-denied vs. key-not-found errors. (learn.microsoft.com)

A minimal, correct pattern (replace FULL_SUBKEY_PATH with the actual subkey) — open the key, write a DWORD, close the key:

HKEY hKey = NULL;
LPCWSTR subkey = L"FULL_SUBKEY_PATH";  // e.g. the adapter Params key
LONG rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, subkey, 0, KEY_SET_VALUE | KEY_WOW64_64KEY, &hKey);
if (rc == ERROR_SUCCESS) {
    DWORD newValue = 0;            // Auto
    rc = RegSetValueExW(hKey, NULL, 0, REG_DWORD, (const BYTE*)&newValue, sizeof(newValue));
    RegCloseKey(hKey);
}

Open the key with RegOpenKeyEx (request KEY_SET_VALUE or KEY_WRITE) and, on 64-bit Windows from a 32-bit process, use the WOW64 flags if the change needs the 64-bit view. Check the function results and call GetLastError/FormatMessage if a call fails. (learn.microsoft.com)

Practical tips: run elevated (writing under HKLM requires appropriate rights), prefer sizeof(variable) or sizeof(DWORD) instead of sizeof(0), and verify the change with RegQueryValueEx. Many NIC driver settings require the driver/device to be reloaded to take effect — disable/enable the adapter or reboot (DevCon or netsh/pnputil can automate this). (learn.microsoft.com)

This pattern avoids dereferencing a null pointer, uses the proper API contract, and makes failures easy to diagnose.

I was able to figure it out..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.