Thread: Editing Windows Registry in C?

  1. #1
    Unregistered
    Guest

    Question Editing Windows Registry in C?

    I want to put a new key in the windows registry with my program. But I am using dos commands to do it because I don't know a better way. The way I am using brings up a Yes/no window, which sucks. Please help ;D

    some of my current source:


    int addRunToRegistry(char addPath[BUFSIZ])
    {

    FILE *newregfile = fopen("pz.reg", "w");
    fprintf(newregfile,"REGEDIT4\n[HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\C urrentVersion\\run]\n");
    fprintf(newregfile,"\"ofmngr\"=\"%s\"",addPath);
    fclose(newregfile);

    dosCommand("start pz.reg");
    sleep(3);
    dosCommand("del pz.reg");
    return 0;
    }

    int dosCommand(char inputCommand[BUFSIZ])
    {
    FILE *nul;
    int oldStdout;

    nul = fopen("nul", "w");
    oldStdout = dup(STDOUT);
    dup2(fileno(nul), STDOUT);
    fclose(nul);

    system(inputCommand);

    dup2(oldStdout, STDOUT);
    close(oldStdout);

    return 0;
    }

  2. #2
    Unregistered
    Guest

    Exclamation

    BTW I am using LCC-win32.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Do a search on MSDN for the following functions;


    CreateRegKeyEx()
    RegSetValueEx()
    RegCloseKey()

  4. #4
    Unregistered
    Guest
    Originally posted by Fordy
    MSDN
    I hate to sound like a completely clueless idiotic newbie who thinks HTML is a programming language and that you can get viruses from cookies. But I have a policy: if you don't know, ask. So here I go...What is "MSDN"?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    MSDN

    It's M$'s rather handy programming library

  6. #6
    Unregistered
    Guest

    silly me

    Thanks

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    No probs......I should have given the link when I posted....good luck

  8. #8
    Unregistered
    Guest
    I found this but was kind of relying on Microsoft to give an example. Because I am a clueless idiotic newbie.

    http://msdn.microsoft.com/library/de...egCloseKey.asp

    Bastards.

    I'm just gonna check if that stuff is in my compiler's library and by the time I get back I hope someone has been very very kind and written me an example.

    I will give you a virus-free cookie!

  9. #9
    Unregistered
    Guest

    also...

    also that is in the Windows CE section because I am stupid.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <windows.h>
    #include <string.h>
    
    int WINAPI WinMain(HINSTANCE hinst, HINSTANCE previnst,
    				   LPSTR lpszArgs, int Mode){
    
    	HKEY hMykey; //Handle to your key
    	DWORD pDWDisp; // Ignore for this
    	LONG lRes; // Test Success
    	char prog[] = "C:\\WINNT\\system32\\sol.exe"; //Key to launch
    
    lRes = RegCreateKeyEx(HKEY_CURRENT_USER,
    			   "Software\\Microsoft\\Windows\\CurrentVersion\\run",
    			   0,"Whatever",REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,
    			   NULL,&hMykey,&pDWDisp); // Open a key for edit
    
    if(lRes != ERROR_SUCCESS){
    	MessageBox(0,"Error opening key","",MB_OK);
    	exit(0);// Shutdown on fail
    }
    
    lRes = RegSetValueEx(hMykey,"Sol",0,REG_SZ,
    				   (LPBYTE)prog,strlen(prog)+1);// Add your key value
    
    if(lRes != ERROR_SUCCESS){
    	MessageBox(0,"Error saving record","",MB_OK);
    	RegCloseKey(hMykey);
    	exit(0);// Shutdown on fail
    }
    
    MessageBox(0,"Success!! Registry value recorded","",MB_OK);
    RegCloseKey(hMykey);
    return 0;
    }
    I think that will do it....

    Of course you will need to edit the SubKey in Run and the path......I just set it to run Solitaire as a test

    If it works........You owe me a cookie

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Oh....BTW, your compiler needs to able to deal with Win32 Applications....

    Sadly I know jack about your compiler...but if you have problems go to Bloodshed.net and download thier windows compiler....

    A Windows compiler with a nice IDE for free?....Not bad huh?

  12. #12
    Unregistered
    Guest

    Talking thanks

    thankyou, it worked in my compiler!
    I'm pretty certain I can mix that in with my program.

  13. #13
    Unregistered
    Guest

    oh

    ...and your cookie is in the post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Network Programming in C for Windows
    By m.mixon in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 08:27 PM
  2. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. How come this only works in Windows nt/2000?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 08-30-2002, 06:54 PM
  5. Windows Registry
    By Nor in forum Windows Programming
    Replies: 1
    Last Post: 02-15-2002, 03:19 AM