• C

Registry reading under Windows 95

I want't to read some registry keys in C/C++, i have used the following code myself:



HKEY openKey;
unsigned long size;
PCHAR keyValue;


if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ArchiServe\\ArchiShell\\File locations\\",0, KEY_ENUMERATE_SUB_KEYS | KEY_EXECUTE | KEY_QUERY_VALUE, &openKey)==ERROR_SUCCESS)
{
  if (RegQueryValueEx(openKey, "ArchiStream", NULL, NULL, NULL, &size)==ERROR_SUCCESS)
  {
    keyValue = (char*) malloc (size);
    if (RegQueryValueEx(openKey, "ArchiStream", NULL, NULL, (unsigned char*)keyValue, &size)==ERROR_SUCCESS)
    {
    }
    else
      ShowLastError();
  }
  else
    ShowLastError();
  RegCloseKey(openKey);
}
else
  ShowLastError();


This code seems to work, but not under windows 95, but it works on 98 and NT. The ShowLastError() function does a GetLastError() and FormatMessage() to produce some normal error string. But I get two different error while running the code, which one seems to be random, but both have got nothing to do with the registry. One says that the system has to run in win32 mode, and the other one i can't remember right now. (I haven't got a test platform right now.)
Anyone who can tell me what the problem with my code is, or can give me some other working C code, will get the points.
Thanks in advance, Elmar.
Zork2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zork2Author Commented:
Edited text of question.
0
AlexVirochovskyCommented:
Next is my code for make it
int Test(void)
{
  int bStop = 0;
      // skip registry key opening sequence for clarity
  const INT nSIZE = 4096; //maximum(real 8)
  const DWORD dwLen = 8;
  const char *lpszPathName = "SOFTWARE\\COMPANY\\MYCOMPANY";
  const char *pszValueName = "Param01";

  HKEY hKey,hBaseKey = HKEY_LOCAL_MACHINE;
  DWORD dwType;
  DWORD dwData  = nSIZE;
  char* bData   = (char *)::calloc(nSIZE, sizeof(char));
  struct date dDate;
  LONG retValue;

//open key
  retValue = ::RegOpenKeyEx(hBaseKey, lpszPathName,
       NULL, KEY_ALL_ACCESS,&hKey);
  if (retValue != ERROR_SUCCESS)//no such : 1-st time
    {                           //create key
       uint32 disposition;
       retValue = ::RegCreateKeyEx(hBaseKey, lpszPathName, 0, ""/*class*/,
    REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,
    0/*SecurityAttributes*/, &hKey,
    &disposition);
       if (retValue == ERROR_SUCCESS)//must be!
{
    //you code for form bData
//save data
   ::RegSetValueEx(hKey, pszValueName, 0, REG_BINARY,
  (LPBYTE)bData, dwLen);
   ::RegCloseKey(hKey); //close key
}
    }
  else //exist, read data
    {
      retValue = ::RegQueryValueEx(hKey, pszValueName,
  NULL, &dwType,(LPBYTE)bData, &dwData);

       if (retValue == ERROR_SUCCESS && REG_BINARY == dwType)
{
   if (Empty(bData)) //no data?
     {
       //you code for form bData
// Save
      ::RegSetValueEx(hKey, pszValueName, 0, REG_BINARY,
  (LPBYTE)bData, dwLen);
     }
   else  //test data
     {
     }

}
      ::RegCloseKey(hKey); //close key
    }
  if (bData)
    free (bData);
  return bStop;
}
Alex
 BTW: i think, that problem in
KEY_EXECUTE . I use  
KEY_ALL_ACCESS without problms.
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Zork2Author Commented:
Tnx, sorry for the late acceptance, but i was on vacation, so i had no time to test it. The problem was in the access rights for one part, another part of the problem was in not getting the return type of the key, and another part of the problem was getting the right keysize. But your code did work fine, although i had to make some small changes to get it to work in my proggy.
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

ColossalCaveCommented:
The answer is not correct. The code provided will not run on Windows 95.
And using KEY_ALL_ACCESS is bad practice.

0
ColossalCaveCommented:
Surprised that I can tell by just glancing over it that the proposed code will not work in Windows 95?
Are you interested in the real answer?
0
AlexVirochovskyCommented:
ColossalCave, i wrote my code exclusive for Windows 95 and he works in all
my applications!
0
ColossalCaveCommented:

Alex,

I will take your word on that.
But it should not work.

Please answer a question for me:
Are you using Windows 95 Second Edition or something? Have you tried on the original Windows 95?
0
AlexVirochovskyCommented:
>>Are you using Windows 95 Second Edition or something? Have you tried on the original Windows 95?

On original Windows 95.



0
ColossalCaveCommented:

Hm..
{takes good look at code again]

Yes, I can believe it may seem to work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.