Regqueryvalueex returned error 2

System: Windows 7 32bit Language: C++ I have tried to access register HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\Scsi\Scsi Port 0, key Driver (type REG_SZ) -- no problem. The same for reading from

System: Windows 7 32bit
Language: C++

I have tried to access register HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\Scsi\Scsi Port 0, key Driver (type REG_SZ) — no problem.

The same for reading from HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM, all keys (types REG_SZ) got slashes, for example DeviceSerial0.

While reading such keys it always returns 2 (no such file) with following example code:

HKEY hKey = 0;
DWORD dwType = REG_SZ;
char buf[255] = {0};
DWORD dwBufSize = sizeof(buf);

if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("HARDWARE\DEVICEMAP\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS ) 
{
  auto ret = RegQueryValueEx( hKey, TEXT("DeviceSerial0"), 0, &dwType, (LPBYTE)buf, &dwBufSize );
  // ret always == 2 for key with slashes
--- CUT ---

What is the proper way to read key values with slashes in name?


Above has been properly answered by Cody Gray.


Below another issue.


Im getting the same problem when Im using variable instead of a text string.
Iv considered both approaches with single and double slashes:

HKEY hKey = 0;
DWORD keyType = REG_SZ;
TCHAR buf[255] = {0};
DWORD bufSize = sizeof(buf);

QSettings winReg("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", QSettings::NativeFormat);
auto comsKey = winReg.allKeys();

FOREACH( auto com, comsKey )
{
  // FOREACH - boost macro
  // comsKey = QList<QString> (list of key names) [from Qt framework]
  // com = QString (single key name) [from Qt framework]
  if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\DEVICEMAP\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS )
  {
    wchar_t* keyw = new wchar_t();
    //com.replace("/", "\\"); <- checked both variants commented and not commented; com == /Device/Serial0 so im converting to \Device\Serial0
    int size = com.size();
    mbstowcs( keyw, com.toStdString().data(), size );
    //auto ret = RegQueryValueEx( hKey, TEXT("\Device\Serial0"), 0, &keyType, (LPBYTE)buf, &bufSize ); // <- this works!
    auto ret = RegQueryValueExW( hKey, (LPCWSTR)&keyw, 0, &keyType, (LPBYTE)buf, &bufSize ); // <- this one not works!

I have tried all variants with «Device..», «/Device», «Device», etc.

I am trying to read three REG_SZ values (strings) from the registry using C, but I am having no success. The function RegOpenKeyEx runs correctly, but after I then run RegQueryValueExW, I sometimes get error 2 (ERROR_FILE_NOT_FOUND), or sometimes error 127 (ERROR_PROC_NOT_FOUND). Any help would be greatly appreciated, as I’ve been working on getting this to work for a few days now and it has been very frustrating! Thank you so much for taking the time to assist me!

The following code gives me error 127:

TCHAR date[11];
TCHAR regSerialKey[12];
TCHAR strAN[15];
HKEY hkey;
unsigned long datalen = sizeof(char) * 11; 
if (RegOpenKeyEx(HKEY_CURRENT_USER, L"\Software\VB and VBA Program Settings\App Version\Align", 0, KEY_ALL_ACCESS, &hkey) != ERROR_SUCCESS)                                       { printf("RegOpenKeyEx failed with error: %dn", GetLastError());  }
else
{
    printf("RegOpenKeyEx was successfuln."); 
    if (RegQueryValueExW(hkey, TEXT("Date"), NULL, NULL, (LPBYTE)date, &datalen) != ERROR_SUCCESS)         {  printf("RegQueryValueExW run 1 failed with error: %dn", GetLastError());  }
    datalen = sizeof(char) * 12;     if (RegQueryValueExW(hkey, TEXT("strSerNum"), NULL, NULL, (LPBYTE)regSerialKey, &datalen) != ERROR_SUCCESS)
    {  printf("RegQueryValueExW run 2 failed with error: %dn", GetLastError());  }
    datalen = sizeof(char) * 12;
    if (RegQueryValueExW(hkey, TEXT("strAN"), NULL, NULL, (LPBYTE)strAN, &datalen) != ERROR_SUCCESS)
    {  printf("RegQueryValueExW run 3 failed with error: %dn", GetLastError());  }
    RegCloseKey(hkey);
}

Edit: I’m really new to the whole idea of the Windows Registry (I’m predominantly a Mac user), and I’ve never used these registry read functions before. Could someone please post code samples that should replace mine? Thanks!

Edit 2: Yes, my app is Unicode.
It does compile when I use L»address goes here», but when I use a const char * variable and use it as the parameter for the RegOpenKey function, that function returns error 127 (it used to be error 2, so there must be a few things wrong with my code). I’ve noticed that when I use L»address goes here», the compiler gives me a warning, «Suspicious pointer conversion,» and also gives me the same warning for the RegQueryValue functions if I use L»Date» or L»strSerNum». If I use TEXT(), I don’t get these warnings.

Edit 3: «In this case it can easily happen that GetLastError() returns something that is unrelated to your RegQueryValueEx call, for example the last_error of a previously failed function call.»

You’re right. I changed my code so that the a variable stores the return value of the function calls, and they keep giving me error 2. Error 127 must be from something unrelated, as you predicted.

Система: Windows 7 32bit
Язык: С++

Я попытался получить доступ к регистру HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\Scsi\Scsi Port 0, key Driver (type REG_SZ) — не проблема.

То же самое для чтения из HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM, все клавиши (типы REG_SZ) получили косые черты, например DeviceSerial0.

При чтении таких ключей он всегда возвращает 2 (нет такого файла) со следующим примером кода:

HKEY hKey = 0;
DWORD dwType = REG_SZ;
char buf[255] = {0};
DWORD dwBufSize = sizeof(buf);

if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("HARDWARE\DEVICEMAP\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS ) 
{
  auto ret = RegQueryValueEx( hKey, TEXT("DeviceSerial0"), 0, &dwType, (LPBYTE)buf, &dwBufSize );
  // ret always == 2 for key with slashes
--- CUT ---

Каков правильный способ считывания ключевых значений с помощью слэшей в имени?


Выше был правильно ответил Коди Грей.


Ниже другой проблемы.


Я получаю ту же проблему, когда я использую переменную вместо текстовой строки.
Iv рассматривал оба подхода с одиночной и двойной косой чертой:

HKEY hKey = 0;
DWORD keyType = REG_SZ;
TCHAR buf[255] = {0};
DWORD bufSize = sizeof(buf);

QSettings winReg("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", QSettings::NativeFormat);
auto comsKey = winReg.allKeys();

FOREACH( auto com, comsKey )
{
  // FOREACH - boost macro
  // comsKey = QList<QString> (list of key names) [from Qt framework]
  // com = QString (single key name) [from Qt framework]
  if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\DEVICEMAP\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS )
  {
    wchar_t* keyw = new wchar_t();
    //com.replace("/", "\\"); <- checked both variants commented and not commented; com == /Device/Serial0 so im converting to \Device\Serial0
    int size = com.size();
    mbstowcs( keyw, com.toStdString().data(), size );
    //auto ret = RegQueryValueEx( hKey, TEXT("\Device\Serial0"), 0, &keyType, (LPBYTE)buf, &bufSize ); // <- this works!
    auto ret = RegQueryValueExW( hKey, (LPCWSTR)&keyw, 0, &keyType, (LPBYTE)buf, &bufSize ); // <- this one not works!

Я пробовал все варианты с помощью » Device..», «/Device», » Device» и т.д.

Понравилась статья? Поделить с друзьями:
  • Regqueryvalueex error 2 не удается найти указанный файл
  • Regmsc cmd 1с ошибка
  • Registry import error accessing the registry
  • Registry filter driver exception как исправить
  • Registry error синий экран как исправить