Registry Problem

This is a discussion on Registry Problem within the Windows Programming forums, part of the Platform Specific Boards category; Here's a code snippet form CodeProject.org about adding a program to the system registry for it to start up. Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Registry Problem

    Here's a code snippet form CodeProject.org about adding a program to the system registry for it to start up.

    Code:
    bool CWinStartup::AddApp(LPCTSTR lpszName, LPCTSTR lpszPath, StartupUser user)
    {
    	HKEY hRootKey;
    
    	if (user == CurrentUser)
    	{
    		hRootKey = HKEY_CURRENT_USER;
    	}
    	else
    	{
    		hRootKey = HKEY_LOCAL_MACHINE;
    	}
    
    	HKEY hKey = 0;
    	HRESULT hr = RegOpenKey(hRootKey, KEY_STARTUP, &hKey);
    
    	if (hr == ERROR_SUCCESS)
    	{
    		hr = RegSetValueEx(hKey, lpszName, 0, REG_SZ, (const BYTE*)lpszPath, _tcslen(lpszPath));
    
    
    		RegCloseKey(hKey);
    	}
    
    	return (hr == ERROR_SUCCESS);
    }
    the bolded text is text i had to replace to get it to compile (from strlen() to _tcslen)) I'm in UNICODE mode... if that makes any difference

    When I call that from a MFC program, it only puts a part of the lpszPath in the registry (first 11 chars of any string I give it)

    Heres me calling it:
    Code:
                    LPCTSTR PATH = _T("D:\\DEARD\\EATEAR\\KDEKAR");
    		TCHAR NAME[] = _TEXT("AntiLeet");
    
    		CWinStartup startup;
    
    		startup.AddApp(NAME, PATH, CurrentUser);
    In the registry, it only shows up as D:\DEARD\EA for the path, the name is correct. I've tried using wchar_t, char, and LPCTSTR for the path, they all offer the same results. i've also put the string in _T("") in the actual function, nothing different...

    Why?


    Thanks,

    Matt
    ~guitarist809~

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    There are typically two types of strings available. Old ANSI and Wide char (unicode).
    TCHAR is a macro that resolves either to ANSI or Unicode depending on your project configuration. Similarly, _tcslen and _T are also such functions that translate into either ANSI or Unicode depending on project config.

    This is very handy in that the code will work without modifications whether you compile it with ANSI or Unicode.
    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.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    RegSetValueEx wants size in bytes
    _tcslen returns size in characters - so you need to multiply it on sizeof (TCHAR)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by vart View Post
    RegSetValueEx wants size in bytes
    _tcslen returns size in characters - so you need to multiply it on sizeof (TCHAR)
    Amazing! It worked

    Thank you so much.


    I changed it to
    Code:
    ( _tcslen(lpszPath) * sizeof(TCHAR) )
    I'm assuming that's what you wanted (i hope) ^^
    ~guitarist809~

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Yes, that's right. RegSetValueEx makes no distinction between ANSI and Unicode, and as Unicode is 2 bytes on Windows, you need to specify the correct size manually. This can easily be done by multiplying by sizeof(TCHAR), as you've done.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Possible Windows MDAC problem
    By BobS0327 in forum Tech Board
    Replies: 3
    Last Post: 06-28-2006, 05:57 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

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