Thread: How to update the value of LPCTSTR lpSubKey in RegOpenKeyEx()?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70

    How to update the value of LPCTSTR lpSubKey in RegOpenKeyEx()?

    I just want to open a key in a registry(Windows XP) using RegOpenKeyEx() function which takes the second argument as path of the registry key. The second argument type is LPCTSTR (means pointer to a Const Char).

    Example of the Path:
    "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\.. .\\...\\..."

    here I know about the path till the \\CurrentControlSet\\... at starting. There after i will came to know the exact path at runtime.
    How can i modified that?

    Main problem is we have to pass the second argument in RegOpenKeyEx() function as LPCTSTR.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I quite dont understand. So you dont know the part of the register, but you partially know it.

    Well cant you just construct the path at the run time, once you get to know the registry? Its would rather be pretty easy to construct a string. And then type cast it to LPCTSTR?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by ssharish2005 View Post
    I quite dont understand. So you dont know the part of the register, but you partially know it.

    Well cant you just construct the path at the run time, once you get to know the registry? Its would rather be pretty easy to construct a string. And then type cast it to LPCTSTR?

    ssharish
    I already try that typecasting, but it doesn't work.
    because while in the actual scenario, calling of this the function could seems like,
    Code:
    RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    		TEXT("SYSTEM\\CurrentControlSet\\Enum\\USB\\Vid_0458&Pid_9856\\7654321"),
    		0,
    		KEY_READ,
    		&hKey);
    The above function calls executes fine and returns 0.

    In my actual scenario, We have to do the type casting on the second parameter TEXT("....").
    so what i did,
    get the path into "ptr" pointer in the run time, and did the typecasting as
    Code:
    RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    		(LPCWSTR)ptr,
    		0,
    		KEY_READ,
    		&hKey);
    but this function gets failed and returns 2.

    Any suggestions are welcome!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Then you need to make sure ptr is pointing to a wide string.
    If it's just a narrow string, then simply casting your way out of trouble just to make the compiler STFU just doesn't work.

    Casting a pointer doesn't magically make what it points to valid.

    Think first, and then cast only if it's the right thing to do. Applying it all over the place as some band-aid sticking plaster isn't the way to go.
    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.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why don't you turn off unicode? (In project properties, set character set to "not set".)

    Or explicitly use the non-unicode version of the function RegOpenKeyExA? (Note the A at the end.)

    Or convert your string to unicode like this?
    Code:
    #define WSTR_SIZE 256
    LPWSTR wstr[WSTR_SIZE];
    
    // with a C library function
    mbstowcs(wstr, str, WSTR_SIZE);
    
    // with a Windows function
    MultiByteToWideChar(CP_ACP, 0, str, strlen(str), wstr, WSTR_SIZE);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int to LPCTSTR
    By CPlus in forum Windows Programming
    Replies: 2
    Last Post: 07-26-2010, 02:41 AM
  2. Converting INT to LPCTSTR
    By CPlus in forum C++ Programming
    Replies: 1
    Last Post: 07-25-2010, 10:21 PM
  3. LPCTSTR to LPWSTR
    By maxcat in forum Windows Programming
    Replies: 3
    Last Post: 03-07-2008, 10:42 AM
  4. lpctstr problem
    By mr_empty in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2007, 02:31 PM
  5. RegOpenKeyEx question
    By c669 in forum C++ Programming
    Replies: 4
    Last Post: 07-16-2004, 09:55 AM