Handle leak in NvCpl.dll


I’m on Windows XP 32-bit, trying to use the dtcfg functions in NvCpl from C++. Consider the following code:

HMODULE m_hNvCplLib;
typedef DWORD (APIENTRY *_dtcfgex)(char*);
_dtcfgex m_fpDtcfgex;

m_hNvCplLib = LoadLibrary(TEXT("NvCpl.dll"));
if (m_hNvCplLib)
	m_fpDtcfgex = (_dtcfgex) GetProcAddress(m_hNvCplLib, "dtcfgex");

// inside some method:

m_fpDtcfgex("set_normalize_brightness 0 all 0.5");

Running the last line, the method, multiple times results in the handle count for my process increasing.

When examining this using Process Explorer, it is revealed that, at each call to dtcfgex, two new key handles are opened, pointing to the registry key “HKLM\Software\NVidia Corporation\Global\NVTweak\State\DTCFG\LastCommandLine”, where it apparently store what the latest call was, and its result. These handles are never removed, resulting in a handle leak that causes the application to crash after a sufficient amount of API calls.

This has been verified to occur for the following set-ups:
Card: 7600GS, driver: 307.83
Card: 9400GT, driver: 182.08
Card: 9400GT, driver: 332.21
Card: 8400GS, driver: 314.22

Am I somehow using the API incorrectly, or is this a bug in the driver? If so, is there a workaround?

Any hints would be much appreciated.

This is a defect inside the control panel. It’s fixed now and will be available in an upcoming driver release.

In general it’s recommended to switch to the NVAPI instead of using the control panel functions when possible.