Thread: More Registry Trouble...

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007

    More Registry Trouble...


    I have one final question about doing stuff with the registry. I was trying to use the "RegQueryValueEx" function to get a value from the registry, but I can't seem to get it to work. Everything compiles just fine, however when I run I get a dialog box "Unhandled exception at 0x77dd7904 in Program_Name.exe: 0xC0000005: Access violation reading location 0x000000c8." I'm pretty sure the error is in how I'm trying to save the data, but nothing I try works. Here's what I did:
    BYTE lb;
    RegQueryValueExA(phKey, "password", NULL, NULL, &lb, (LPDWORD)200/*wasn't sure what to put here...*/);
    I'm pretty sure the "&lb" part is what is causing the problem, however since I've never used the "LPBYTE" type before maybe it's not (why couldn't they just stick to using char arrays?). All of the keys and values exist, and even if they didn't it still shouldn't give me an error like this. Can anyone help? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > (LPDWORD)200/*wasn't sure what to put here...*/
    Interestingly, 200 is, wait for it, in hex, 0xc8.

    > Access violation reading location 0x000000c8

    My guess is have another read of MSDN to find out what parameters you need to pass. It would seem that you need to pass the address of some buffer or address of some variable.

    Simply inventing values, and casting away all the warnings doesn't work.

    Perhaps something like
    BYTE lb[200];
    DWORD length = 200;
    RegQueryValueExA(phKey, "password", NULL, NULL, &lb, &length);
    Apparently, length is updated with the actual length of the data read from the registry.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Ah, thank you Salem. I had done all of that but had forgotten to change that to a variable. Many thanks. Hopefully this is my last question!
    Last edited by mikeman118; 12-23-2007 at 04:08 PM.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    When there is a "LP" prefix before something, that means it's expecting a pointer to a variable of type after the LP prefix. So don't cast to LPX - never ever do that. Very bad.
    Also check msdn's docs - since they actually say which parameters go in and out. And if you check closely you can also read what you're supposed to pass. If it says "pointer to a variable" then you must pass a pointer (sometimes NULL is also acceptable).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Ok I'll make sure to do that from now on. Thanks for the advice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Registry, Regedit
    By franse in forum C++ Programming
    Replies: 21
    Last Post: 01-29-2009, 09:57 AM
  2. Registry HowTo
    By xxxrugby in forum C Programming
    Replies: 2
    Last Post: 04-10-2005, 10:44 AM
  3. Problem with the registry
    By neandrake in forum Windows Programming
    Replies: 1
    Last Post: 03-03-2004, 06:41 PM
  4. Registry
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 07-30-2003, 04:02 PM
  5. Registry Access
    By ExDigit in forum Windows Programming
    Replies: 3
    Last Post: 01-04-2002, 04:02 AM