RegQueryValueEx Correct use method

Source: Internet
Author: User

The project needs to read the value of the next subkey of the HKEY_CLASSES_ROOT primary key in the registry, looking at the instructions for MSDN, with RegOpenKeyEx and RegQueryValueEx Two functions can be used. Without carefully reading the function description, I wrote a simple code to test:

HKEY HKEY; WCHAR Szlocation[max_path] = {' REG_SZ '};D Word dwsize = sizeof;D word dwtype LPCTSTR Studiopath = TEXT ("Insta360\\studio"); LONG Ret;ret = RegOpenKeyEx (HKEY_CLASSES_ROOT, Studiopath, 0, Key_read, &hkey); wprintf (L "RegOpenKeyEx returns%d\n" , ret), if (error_success = = ret) {ret = RegQueryValueEx (HKey, TEXT ("Install_location"), 0, &dwtype, (LPBYTE) &szlo cation, &dwsize); wprintf (L "RegQueryValueEx returns%d, dwsize=%d\n", ret, dwsize); if (error_success = = ret) {wprintf (L "Location:%s", szlocation);} RegCloseKey (HKey);}

A run to discover what also did not read out, and RegQueryValueEx () unexpectedly return is 234 (error_more_data), suddenly a face Meng force.

The key is that the last parameter of the function. The type is labeled [In][out]. In other words, this parameter is both the input of the function and the output of the function . The function will be in this parameter when it reads the registry with the amount of space that is actually needed. The user allocates the required memory space based on this size. So the program can call this function to test the size before it is formally read:

Note that the second-to-last argument to this function should pass NULL when tempted. But in fact it can not be tempted. Instead, set the input value of the last parameter a bit larger so that the 234 error code is not returned.

Reference links

Https://www.experts-exchange.com/questions/23897919/RegQueryValueEx-causes-Error-234.html

RegQueryValueEx Correct use method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.