More Registry Trouble...

This is a discussion on More Registry Trouble... within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I have one final question about doing stuff with the registry. I was trying to use the "RegQueryValueEx" function ...

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

    More Registry Trouble...

    Hi,

    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:
    Code:
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > (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.
    http://msdn2.microsoft.com/en-us/library/ms724911.aspx

    Perhaps something like
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    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 03:08 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Posts
    183
    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, 08: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, 05: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, 03:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21