CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Posts
    2

    C++ register value error

    Hello all, I got some problem with these functions: RegCreateKey, RegSetValueEx,RegQueryValueEx,RegOpenKeyEx and I dont need a URL to msdn. The problem is that its working 2/3 :-).
    Output in the register should be like this:
    Name: Test
    Type: REG_SZ
    Data: C:\WINDOWS\system32\test.exe

    But I get this:
    Name: Test
    Type: REG_SZ
    Data: [][][][][][][][][][][][][][]

    My code is below, do anyone see the problem?
    hope you dont mind to help me :-).
    //KaZu



    #include <windows.h>
    #include <stdio.h>
    #include <winuser.h>

    #define BUFSIZE 120
    int test_key(void);
    int create_key(char *);
    int main(void)
    {
    char *path = new char[40];
    path ="C:\\WINDOWS\\system32\\test.exe";

    int test,create; // just holders to get the return value from functions

    test=test_key(); /*check if key is available for opening*/

    if (test==2)/*create key*/
    {
    //*the path in which the file needs to be*/
    create=create_key(path);
    }


    int test_key(void) // function to check if we could open the register and check the value for specified name
    {
    int check;
    HKEY hKey;
    char path[BUFSIZE];
    DWORD buf_length=BUFSIZE;
    int reg_key;

    reg_key=RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEX("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),0,KEY_QUERY_VALUE,&hKey); // Open register
    if(reg_key!=ERROR_SUCCESS)// If the value isnt ERROR_SUCCESS it have failed and returns a error, set the check = 1
    {
    check=1;
    return check;
    }

    reg_key=RegQueryValueEx(hKey,TEXT("test"),NULL,NULL,(LPBYTE)path,&buf_length); // Get value for"test"

    if((reg_key!=0)||(buf_length>BUFSIZE))
    check=2;

    if(reg_key==0)
    check=0;

    RegCloseKey(hKey); // Just close the key
    return check;
    }


    int create_key(char *path)
    {
    int reg_key,check;
    HKEY hkey;

    reg_key=RegCreateKey(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),&hkey); // Create a key
    if(reg_key==0)
    {
    RegSetValueEx((HKEY)hkey,TEXT("test"),0,REG_SZ,((LPBYTE)path),(sizeof(path))+1); // Set the value
    check=0;
    return check;
    }
    if(reg_key!=0)
    check=1;

    RegCloseKey(hkey); // close the key
    return check;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: C++ register value error

    What does this C:\WINDOWS\system32\test.exe do and why do you want to run it every time with Windows?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    May 2009
    Posts
    2

    Re: C++ register value error

    Quote Originally Posted by VladimirF View Post
    What does this C:\WINDOWS\system32\test.exe do and why do you want to run it every time with Windows?
    I have already solved it. It´s a project for checking how register works in windows 32. Just PM me for info

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured