using the Registry tutorial needed...

hiya does anyone know where i could download 'using the Registry and Registry functions' tutorial...

thanks,
grahamoj.

Comments

  • : hiya does anyone know where i could download 'using the Registry and Registry functions' tutorial...
    :
    : thanks,
    : grahamoj.
    :

    Its actually quite easy. Look up OpenRegKey(), RegOpenKeyEx(), RegDeleteValue(), RegOpenKeyEx(), RegCloseKey(), and RegSetValueEx(), and you'll be set. I can give some examples if It wasn't so late :)
  • : : hiya does anyone know where i could download 'using the Registry and Registry functions' tutorial...
    : :
    : : thanks,
    : : grahamoj.
    : :
    :
    : Its actually quite easy. Look up OpenRegKey(), RegOpenKeyEx(), RegDeleteValue(), RegOpenKeyEx(), RegCloseKey(), and RegSetValueEx(), and you'll be set. I can give some examples if It wasn't so late :)
    :

    i have tried this to open the application when the computer starts:

    HKEY hkey = HKEY_CURRENT_USER;
    char* lpSubKey = "\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
    HKEY phkey;

    RegOpenKeyEx( hkey,lpSubKey,0,KEY_ALL_ACCESS,&phkey );

    RegCloseKey( hkey );

    but i opened the 'path' in RegEdit and it doesn't add the key to the folder 'Run'..

    am i doing something wrong??

    thanks,
    grahamoj.
  • : : : hiya does anyone know where i could download 'using the Registry and Registry functions' tutorial...
    : : :
    : : : thanks,
    : : : grahamoj.
    : : :
    : :
    : : Its actually quite easy. Look up OpenRegKey(), RegOpenKeyEx(), RegDeleteValue(), RegOpenKeyEx(), RegCloseKey(), and RegSetValueEx(), and you'll be set. I can give some examples if It wasn't so late :)
    : :
    :
    : i have tried this to open the application when the computer starts:
    :
    : HKEY hkey = HKEY_CURRENT_USER;
    : char* lpSubKey = "\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
    : HKEY phkey;
    :
    : RegOpenKeyEx( hkey,lpSubKey,0,KEY_ALL_ACCESS,&phkey );
    :
    : RegCloseKey( hkey );
    :
    : but i opened the 'path' in RegEdit and it doesn't add the key to the folder 'Run'..
    :
    : am i doing something wrong??
    :
    : thanks,
    : grahamoj.
    :

    First off, you need to actually write a value to the key, rather than just open it. Also, to have a program run at startup, you could also just add a link to the startup folder. Anyway, to write a value to the registry, you need to use RegSetValueEx() once the key is opened/created.

    This is just some snippets from one of my progs:
    [code]
    #define REGKEY_SOFTWARE "Software\MyAppName"
    #define REGKEY_DIRECTORY_PATH "Path"

    //this is used to create reg keys
    BOOL CPlugODlg::CreateRegistryKeys()
    {
    HKEY hKey;
    DWORD dwDisp;
    if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
    REGKEY_SOFTWARE,
    0,
    NULL,
    REG_OPTION_NON_VOLATILE,
    KEY_QUERY_VALUE | KEY_SET_VALUE,
    NULL,
    &hKey,
    &dwDisp ))
    {
    return FALSE;
    }

    if (dwDisp == REG_OPENED_EXISTING_KEY)
    {
    RegCloseKey(hKey);
    return TRUE;
    }
    RegCloseKey(hKey);

    return TRUE;
    }

    //this is used to open keys
    HKEY CPlugODlg::OpenRegKey(CString REG_KEY)
    {
    HKEY hKey;

    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    REG_KEY,
    0,
    KEY_QUERY_VALUE | KEY_READ | KEY_WRITE,
    &hKey ) != ERROR_SUCCESS)
    {
    return NULL;
    }
    return hKey;
    }

    // this is used to set registry values
    BOOL CPlugODlg::WriteRegistrySettings()
    {
    DWORD rc;
    HKEY hKey;
    char x_plug_dir[MAX_PATH];

    strcpy (x_plug_dir, REGVAL_XPLUGIN_DIRECTORY);

    rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    REGKEY_SOFTWARE,
    0,
    KEY_QUERY_VALUE | KEY_SET_VALUE,
    &hKey );

    if (rc != ERROR_SUCCESS)
    {
    return FALSE;
    }

    rc = RegSetValueEx( hKey, _T(REGKEY_DIRECTORY_PATH), 0, REG_SZ, (const unsigned char *)x_plug_dir, strlen(x_plug_dir) );
    assert( rc == ERROR_SUCCESS );

    RegCloseKey( hKeyPlugO );

    return TRUE;
    }

    //this is used to read registry values
    BOOL CPlugODlg::ReadRegistrySettings()
    {
    DWORD ret;
    DWORD dwDisp;
    HKEY hKeyReg;
    TCHAR data[MAX_PATH]="N/A";
    DWORD data_length = sizeof(data);
    if ( (hKeyReg = OpenRegKey(REGKEY_SOFTWARE)) != NULL )
    {
    data_length = sizeof(data);
    ret = RegQueryValueEx(hKeyReg, _T(REGKEY_DIRECTORY_PATH), NULL, &dwDisp, (unsigned char *)&data, &data_length);

    if (ret != ERROR_SUCCESS)
    {
    //return FALSE;
    }

    if (dwDisp == REG_SZ)
    {
    XtraPluginDirectory.Empty();
    XtraPluginDirectory.Insert(0, (LPCTSTR)data);
    }
    RegCloseKey( hKeyReg );
    }
    return TRUE;
    }

    //this is used to clear registry values
    BOOL CPlugODlg::ClearRegValues()
    {
    HKEY hKey;
    LONG val;
    BOOL ret=FALSE;

    if ( (hKey = OpenRegKey(REGKEY_SOFTWARE)) == NULL ) return FALSE;

    val = RegDeleteValue(hKey, REGKEY_DIRECTORY_PATH);
    if (val == ERROR_SUCCESS)
    ret = TRUE;
    RegCloseKey(hKey);
    return ret;
    }
    [/code]
  • : : : : hiya does anyone know where i could download 'using the Registry and Registry functions' tutorial...
    : : : :
    : : : : thanks,
    : : : : grahamoj.
    : : : :
    : : :
    : : : Its actually quite easy. Look up OpenRegKey(), RegOpenKeyEx(), RegDeleteValue(), RegOpenKeyEx(), RegCloseKey(), and RegSetValueEx(), and you'll be set. I can give some examples if It wasn't so late :)
    : : :
    : :
    : : i have tried this to open the application when the computer starts:
    : :
    : : HKEY hkey = HKEY_CURRENT_USER;
    : : char* lpSubKey = "\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
    : : HKEY phkey;
    : :
    : : RegOpenKeyEx( hkey,lpSubKey,0,KEY_ALL_ACCESS,&phkey );
    : :
    : : RegCloseKey( hkey );
    : :
    : : but i opened the 'path' in RegEdit and it doesn't add the key to the folder 'Run'..
    : :
    : : am i doing something wrong??
    : :
    : : thanks,
    : : grahamoj.
    : :
    :
    : First off, you need to actually write a value to the key, rather than just open it. Also, to have a program run at startup, you could also just add a link to the startup folder. Anyway, to write a value to the registry, you need to use RegSetValueEx() once the key is opened/created.
    :
    : This is just some snippets from one of my progs:
    : [code]
    : #define REGKEY_SOFTWARE "Software\MyAppName"
    : #define REGKEY_DIRECTORY_PATH "Path"
    :
    : //this is used to create reg keys
    : BOOL CPlugODlg::CreateRegistryKeys()
    : {
    : HKEY hKey;
    : DWORD dwDisp;
    : if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
    : REGKEY_SOFTWARE,
    : 0,
    : NULL,
    : REG_OPTION_NON_VOLATILE,
    : KEY_QUERY_VALUE | KEY_SET_VALUE,
    : NULL,
    : &hKey,
    : &dwDisp ))
    : {
    : return FALSE;
    : }
    :
    : if (dwDisp == REG_OPENED_EXISTING_KEY)
    : {
    : RegCloseKey(hKey);
    : return TRUE;
    : }
    : RegCloseKey(hKey);
    :
    : return TRUE;
    : }
    :
    : //this is used to open keys
    : HKEY CPlugODlg::OpenRegKey(CString REG_KEY)
    : {
    : HKEY hKey;
    :
    : if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    : REG_KEY,
    : 0,
    : KEY_QUERY_VALUE | KEY_READ | KEY_WRITE,
    : &hKey ) != ERROR_SUCCESS)
    : {
    : return NULL;
    : }
    : return hKey;
    : }
    :
    : // this is used to set registry values
    : BOOL CPlugODlg::WriteRegistrySettings()
    : {
    : DWORD rc;
    : HKEY hKey;
    : char x_plug_dir[MAX_PATH];
    :
    : strcpy (x_plug_dir, REGVAL_XPLUGIN_DIRECTORY);
    :
    : rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    : REGKEY_SOFTWARE,
    : 0,
    : KEY_QUERY_VALUE | KEY_SET_VALUE,
    : &hKey );
    :
    : if (rc != ERROR_SUCCESS)
    : {
    : return FALSE;
    : }
    :
    : rc = RegSetValueEx( hKey, _T(REGKEY_DIRECTORY_PATH), 0, REG_SZ, (const unsigned char *)x_plug_dir, strlen(x_plug_dir) );
    : assert( rc == ERROR_SUCCESS );
    :
    : RegCloseKey( hKeyPlugO );
    :
    : return TRUE;
    : }
    :
    : //this is used to read registry values
    : BOOL CPlugODlg::ReadRegistrySettings()
    : {
    : DWORD ret;
    : DWORD dwDisp;
    : HKEY hKeyReg;
    : TCHAR data[MAX_PATH]="N/A";
    : DWORD data_length = sizeof(data);
    : if ( (hKeyReg = OpenRegKey(REGKEY_SOFTWARE)) != NULL )
    : {
    : data_length = sizeof(data);
    : ret = RegQueryValueEx(hKeyReg, _T(REGKEY_DIRECTORY_PATH), NULL, &dwDisp, (unsigned char *)&data, &data_length);
    :
    : if (ret != ERROR_SUCCESS)
    : {
    : //return FALSE;
    : }
    :
    : if (dwDisp == REG_SZ)
    : {
    : XtraPluginDirectory.Empty();
    : XtraPluginDirectory.Insert(0, (LPCTSTR)data);
    : }
    : RegCloseKey( hKeyReg );
    : }
    : return TRUE;
    : }
    :
    : //this is used to clear registry values
    : BOOL CPlugODlg::ClearRegValues()
    : {
    : HKEY hKey;
    : LONG val;
    : BOOL ret=FALSE;
    :
    : if ( (hKey = OpenRegKey(REGKEY_SOFTWARE)) == NULL ) return FALSE;
    :
    : val = RegDeleteValue(hKey, REGKEY_DIRECTORY_PATH);
    : if (val == ERROR_SUCCESS)
    : ret = TRUE;
    : RegCloseKey(hKey);
    : return ret;
    : }
    : [/code]
    :

    hiya, thanks for all of that. i have copied those notes for studying.
    so this i what i tried tonight:

    #define REGKEY_SOFTWARE "Software\Local AppWizard-Generated Applications\Dialogs"

    #define REGKEY_DIRECTORY_PATH "\Software\Microsoft\Windows\CurrentVersion\Run"

    DWORD rc;
    HKEY hKey;
    char x_plug_dir[200];

    strcpy (x_plug_dir, REGKEY_DIRECTORY_PATH);

    rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    REGKEY_SOFTWARE,
    0,
    KEY_QUERY_VALUE | KEY_SET_VALUE,
    &hKey );

    if (rc != ERROR_SUCCESS)
    {
    AfxMessageBox( "RegOpenKeyEx() Function Failed.." );
    }

    rc = RegSetValueEx( hKey,
    _T(REGKEY_DIRECTORY_PATH),
    0, REG_SZ,
    (const unsigned char *)x_plug_dir,
    strlen(x_plug_dir) );

    if (rc != ERROR_SUCCESS)
    {
    AfxMessageBox( "RegSetValueEx() Function Failed.." );
    }

    RegCloseKey( hKey );

    i have put this code in the OnInitDialog() function of my dialog app, thought this was a good place to put it.

    but it fails on both of the calls to the functions..

    am i still doing something wrong??

    thanks again,
    grahamoj.
  • [b][red]This message was edited by DB1 at 2002-10-28 23:13:28[/red][/b][hr]

    :
    : hiya, thanks for all of that. i have copied those notes for studying.
    : so this i what i tried tonight:
    :
    : #define REGKEY_SOFTWARE "Software\Local AppWizard-Generated Applications\Dialogs"
    :
    : #define REGKEY_DIRECTORY_PATH "\Software\Microsoft\Windows\CurrentVersion\Run"
    :
    : DWORD rc;
    : HKEY hKey;
    : char x_plug_dir[200];
    :
    : strcpy (x_plug_dir, REGKEY_DIRECTORY_PATH);
    :
    : rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    : REGKEY_SOFTWARE,
    : 0,
    : KEY_QUERY_VALUE | KEY_SET_VALUE,
    : &hKey );
    :
    : if (rc != ERROR_SUCCESS)
    : {
    : AfxMessageBox( "RegOpenKeyEx() Function Failed.." );
    : }
    :
    : rc = RegSetValueEx( hKey,
    : _T(REGKEY_DIRECTORY_PATH),
    : 0, REG_SZ,
    : (const unsigned char *)x_plug_dir,
    : strlen(x_plug_dir) );
    :
    : if (rc != ERROR_SUCCESS)
    : {
    : AfxMessageBox( "RegSetValueEx() Function Failed.." );
    : }
    :
    : RegCloseKey( hKey );
    :
    : i have put this code in the OnInitDialog() function of my dialog app, thought this was a good place to put it.
    :
    : but it fails on both of the calls to the functions..
    :
    : am i still doing something wrong??
    :
    : thanks again,
    : grahamoj.
    :

    OK, RegOpenKeyEx(...) opens an existing key, but doesn't create one if none is there. If the function fails, that is because the key you specified doesn't exist. RegOpenKeyEx is good for opening keys that you know exist, or if you just want to check if it exists (or you can use RegQueryInfoKey). In the code you where you have "if(rc != ERROR_SUCCESS)" should just return there because there is no use going on to set a value if there is no open key...that is why the second call fails as well. Also, even if the first call succeeded, the second would fail anyway because you are trying to open one key, then write to another.

    You can use RegCreateKeyEx(...) to open a key if it exists, or create it if it doesn't. Once the key is opened/created, you can either create new keys under it, or set a value.


    For your needs to just add a value under an existing key, here is a simplified example:
    [code]

    #define REGKEY_TO_OPEN "Software\Microsoft\Windows\CurrentVersion\Run"
    #define REGNAME_TO_WRITE "MyProgram"
    #define REGVAL_TO_WRITE "myprog.exe" // can also use full path here
    {
    HKEY hKey;

    // Open the key, we know this one exists
    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    REGKEY_TO_OPEN,
    0,
    KEY_SET_VALUE, // we only need SET_VALUE access for this
    &hKey ) != ERROR_SUCCESS)
    {
    // Note: You can use the FormatMessage function here
    // with the FORMAT_MESSAGE_FROM_SYSTEM flag to get
    // a generic description of the error.
    AfxMessageBox("Failed to open the key");
    // just quit now
    return 0;
    }

    // OK, the key is opened, lets write a value...
    if (RegSetValueEx( hKey,
    REGNAME_TO_WRITE,
    0,
    REG_SZ, // writing a string
    (const unsigned char *)REGVAL_TO_WRITE,
    strlen(REGVAL_TO_WRITE) ) != ERROR_SUCCESS)
    {
    // Note: You can use the FormatMessage function here
    AfxMessageBox("Failed to write the value");
    }

    // Clean up
    RegCloseKey(hKey);

    return 0;
    }
    [/code]




  • : [b][red]This message was edited by DB1 at 2002-10-28 23:13:28[/red][/b][hr]
    :
    : :
    : : hiya, thanks for all of that. i have copied those notes for studying.
    : : so this i what i tried tonight:
    : :
    : : #define REGKEY_SOFTWARE "Software\Local AppWizard-Generated Applications\Dialogs"
    : :
    : : #define REGKEY_DIRECTORY_PATH "\Software\Microsoft\Windows\CurrentVersion\Run"
    : :
    : : DWORD rc;
    : : HKEY hKey;
    : : char x_plug_dir[200];
    : :
    : : strcpy (x_plug_dir, REGKEY_DIRECTORY_PATH);
    : :
    : : rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    : : REGKEY_SOFTWARE,
    : : 0,
    : : KEY_QUERY_VALUE | KEY_SET_VALUE,
    : : &hKey );
    : :
    : : if (rc != ERROR_SUCCESS)
    : : {
    : : AfxMessageBox( "RegOpenKeyEx() Function Failed.." );
    : : }
    : :
    : : rc = RegSetValueEx( hKey,
    : : _T(REGKEY_DIRECTORY_PATH),
    : : 0, REG_SZ,
    : : (const unsigned char *)x_plug_dir,
    : : strlen(x_plug_dir) );
    : :
    : : if (rc != ERROR_SUCCESS)
    : : {
    : : AfxMessageBox( "RegSetValueEx() Function Failed.." );
    : : }
    : :
    : : RegCloseKey( hKey );
    : :
    : : i have put this code in the OnInitDialog() function of my dialog app, thought this was a good place to put it.
    : :
    : : but it fails on both of the calls to the functions..
    : :
    : : am i still doing something wrong??
    : :
    : : thanks again,
    : : grahamoj.
    : :
    :
    : OK, RegOpenKeyEx(...) opens an existing key, but doesn't create one if none is there. If the function fails, that is because the key you specified doesn't exist. RegOpenKeyEx is good for opening keys that you know exist, or if you just want to check if it exists (or you can use RegQueryInfoKey). In the code you where you have "if(rc != ERROR_SUCCESS)" should just return there because there is no use going on to set a value if there is no open key...that is why the second call fails as well. Also, even if the first call succeeded, the second would fail anyway because you are trying to open one key, then write to another.
    :
    : You can use RegCreateKeyEx(...) to open a key if it exists, or create it if it doesn't. Once the key is opened/created, you can either create new keys under it, or set a value.
    :
    :
    : For your needs to just add a value under an existing key, here is a simplified example:
    : [code]
    :
    : #define REGKEY_TO_OPEN "Software\Microsoft\Windows\CurrentVersion\Run"
    : #define REGNAME_TO_WRITE "MyProgram"
    : #define REGVAL_TO_WRITE "myprog.exe" // can also use full path here
    : {
    : HKEY hKey;
    :
    : // Open the key, we know this one exists
    : if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    : REGKEY_TO_OPEN,
    : 0,
    : KEY_SET_VALUE, // we only need SET_VALUE access for this
    : &hKey ) != ERROR_SUCCESS)
    : {
    : // Note: You can use the FormatMessage function here
    : // with the FORMAT_MESSAGE_FROM_SYSTEM flag to get
    : // a generic description of the error.
    : AfxMessageBox("Failed to open the key");
    : // just quit now
    : return 0;
    : }
    :
    : // OK, the key is opened, lets write a value...
    : if (RegSetValueEx( hKey,
    : REGNAME_TO_WRITE,
    : 0,
    : REG_SZ, // writing a string
    : (const unsigned char *)REGVAL_TO_WRITE,
    : strlen(REGVAL_TO_WRITE) ) != ERROR_SUCCESS)
    : {
    : // Note: You can use the FormatMessage function here
    : AfxMessageBox("Failed to write the value");
    : }
    :
    : // Clean up
    : RegCloseKey(hKey);
    :
    : return 0;
    : }
    : [/code]
    :
    :
    :
    :
    :
    hiya, thanks for that. worked fine. i had more time today to study the notes and realised what i was doing wrong.

    thanks for your time and patience.

    grahamoj.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories