Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C++

Registry Symbolic Links

Rate me:
Please Sign up or sign in to vote.
4.75/5 (14 votes)
21 Oct 2005CPOL9 min read 69.2K   2K   30  
An article on creating and using registry symbolic links.
static char *rcsid = "$Id: main.c,v 1.6 2005/10/16 12:14:54 cvs Exp $";
/*
*
* $RCSfile: main.c,v $
* $Source: /cvs/rgsymlnk/x64SoftwareKeyLink/main.c,v $
* $Author: cvs $
* $Revision: 1.6 $
* $Date: 2005/10/16 12:14:54 $
* $State: Exp $
* Copyright (c) Stefan Kuhr
*
*
*
* $Log: main.c,v $
* Revision 1.6  2005/10/16 12:14:54  cvs
* Added SetSymLink flags
*
* Revision 1.5  2005/10/07 16:58:37  cvs
* Added dwFlags to functions, made a couple functions easier, added VERIFY, removed _alloca
*
* Revision 1.4  2005/10/06 20:03:40  cvs
* Added x86 build
*
* Revision 1.3  2005/10/06 19:07:28  cvs
* Made adaptions for x86 Builds
*
* Revision 1.2  2005/10/06 18:39:00  cvs
* Adapted to new signature of CreateSymLinkKey
* 
*/
#pragma warning(disable: 4305)
#include <windows.h>
#pragma warning(default: 4305)
#include <tchar.h>
#include <stdio.h>
#include <crtdbg.h>
#include "../rgsymlnk.h"
#include "../wow64hlp.h"




#define SOFTWARE_SUBKEY _T("Software\\")
#define WOW_6432_NODE_SUBKEY _T("Software\\Wow6432Node\\")

static int PrintUsage(void);

int _tmain(int iArgs, LPCTSTR argv[])
{
    HKEY hx64Key = NULL;
    LPTSTR szSubkey = NULL;
    LONG lStatus = ERROR_SUCCESS;
    if (iArgs<2)
        return PrintUsage();

#ifndef WIN64
    if(!IsRunningUnderWow64())
        return PrintUsage();
#endif



    szSubkey = _alloca(  (_tcslen(WOW_6432_NODE_SUBKEY)+_tcslen(argv[1])+1)*sizeof(TCHAR) );
    _tcscpy(szSubkey, SOFTWARE_SUBKEY);
    _tcscat(szSubkey, argv[1]);
    
    if (iArgs==3)
    {
        if (argv[2][0] != _T('-') && argv[2][0] != _T('/') && 
            argv[2][1] != _T('d') && argv[2][1] != _T('D'))
        return PrintUsage();

        /// we delete an existing registry link:
        
        
        /// 
        /// CSL_WOW64_64KEY is ignored when compiled with WIN64 defined so we can 
        /// safely pass it for the x86 build *and* the x64 build:
        /// 
        lStatus = DeleteSymLink(HKEY_LOCAL_MACHINE, szSubkey, CSL_WOW64_64KEY);
        _tprintf(_T("Attempt to delete an existing symlink key %s\n"), ERROR_SUCCESS==lStatus?_T("succeeded"):_T("failed"));
        return lStatus;
    }
    else if (iArgs==2)
    {
        
        /// 
        /// CSL_WOW64_64KEY is ignored when compiled with WIN64 defined so we can 
        /// safely pass it for the x86 build *and* the x64 build:
        /// 
        lStatus = CreateSymLinkKey(HKEY_LOCAL_MACHINE, szSubkey, 
            &hx64Key, CSL_WOW64_64KEY);

        if (ERROR_SUCCESS!=lStatus)
        {
            _tprintf(_T("Failed to create symlink key %ld, attempting to open an existing symlink key\n"), lStatus);
            
            /// 
            /// CSL_WOW64_64KEY is ignored when compiled with WIN64 defined so we can 
            /// safely pass it for the x86 build *and* the x64 build:
            /// 
            lStatus = OpenSymLink(HKEY_LOCAL_MACHINE, szSubkey, &hx64Key, CSL_WOW64_64KEY);
            _tprintf(_T("Attempt to open an existing symlink key %s\n"), ERROR_SUCCESS==lStatus?_T("succeeded"):_T("failed"));
        }

        if (ERROR_SUCCESS == lStatus)
        {
#pragma warning (disable:4127)
            _ASSERT(hx64Key);
#pragma warning (default:4127)
            
            _tcscpy(szSubkey, WOW_6432_NODE_SUBKEY);
            _tcscat(szSubkey, argv[1]);

            
            /// 
            /// SSL_IGNORE_WOW6432NODE_HANDLING is ignored when compiled with WIN64 defined so we can 
            /// safely pass it for the x86 build *and* the x64 build:
            /// 
            lStatus = SetSymLink(hx64Key, HKEY_LOCAL_MACHINE, szSubkey, SSL_IGNORE_WOW6432NODE_HANDLING);
            _tprintf(_T("Setting symlink key %s\n"), ERROR_SUCCESS==lStatus?_T("succeeded"):_T("failed"));
            RegCloseKey(hx64Key);
        }
        return lStatus;
    }

    return PrintUsage();
}




static int PrintUsage(void)
{
    _tprintf(_T("Usage:\nx64SoftwareKeyLink.exe key [-d]\n\nwhere key is a registry key under the 32-bit HKLM\\Software key\n-d: deletes the link\nThis program is only meant to be run under x64 Windows\n"));
    return ERROR_INVALID_PARAMETER;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
Stefan has been writing programs in C/C++ since 1991, and for Windows since 1993. He holds a German engineer's degree Dipl.-Ing. (FH) in "Microelectronics/Technical Computer Science" from the Aalen (Germany) University of Applied Sciences and an MSc in "Software Technology" from the Stuttgart (Germany) University of Applied Sciences. Currently, he is employed by a software company in the south-west of Germany that specializes in PC life-cycle products and software deployment technology. In his spare time, Stefan likes to go swimming and enjoys listening to jazz music from the fifties. And yes, he has a Weblog at http://mcblogs.craalse.de/sku (German only).

Comments and Discussions