Silly Question On Setting Registry

Started by
2 comments, last by fathom88 18 years, 7 months ago
I found a strange problem with setting and reading a string value inside the registry. I need to read the value twice to get the string value out. error = RegQueryValueEx(hk, "MySubKey", NULL, &dwType, NULL, &dwLength); if(error == ERROR_SUCCESS) { RegQueryValueEx(hk, "MySubKey", NULL, &dwType, (LPBYTE)Buffer, &dwLength); } Otherwise I get an error code 234 (ERROR_MORE_DATA). Is this normal? I call RegSetValueEx(hk, "MySubKey",0, REG_SZ,(LPBYTE)Buffer,(strlen(Buffer) + 1)); and check the registry. It looks fine. Thanks for any help. Sorry for the weird code spacing.
Advertisement
You need to set dwLength to be the size of your buffer before you make the first call. The second one should become unnecessary.

If you check the docs for RegQueryValueEx, you'll see lpcbData is marked with [in, out] - this means that the values it contains both before and after the function are meaningful.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

The dwLength parameter is used on input to tell RegQueryValueEx how big your output buffer is. If dwLength is too small to contain the value, RegQueryValueEx returns ERROR_MORE_DATA and sets dwLength to the size needed to hold the value. This allows you to resize your buffer, if needed, to retrieve the value.
Thanks for all the help. I didn't realize the parameters were in/out.

This topic is closed to new replies.

Advertisement