Solved

Need Help In Source Code, C++!

Posted on 2004-10-30
425 Views
Last Modified: 2012-06-27
Okay!  I need the source code for restarting the computer when the program loads.

I got some code in the file already, but when everything is operated I need it to restart/shutdown the computer.  How would I go about doing this?  Thx!
-Worth 50 Points

I also need help with something else, when the program loads, I need it to add a registry entry but I don't know how to go about doing that.  If someone can give me the source code for adding registry entries, greatly appreciated.  Thx!
-Worth 50 Points

0
Question by:Xygus
    7 Comments
     
    LVL 19

    Expert Comment

    by:mrwad99
    Regarding shutting the machine down, you say you have got code already to do this.  It should resemble something like that shown at http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20413564.html in C++.  If you want this code to be executed on machine startup, all you need to do is put this code in an EXE then get the EXE to run at startup via a shortcut to the startup group under the start menu or similar.

    Regarding the registry side of things, you need to look at the functions given at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/regcreatekeyex.asp.  As an example, you can alter the wallpaper currently being displayed by using this code:

    HKEY  hkey2;
    char lpdata[] = "A_picture.bmp";

    RegCreateKeyEx(HKEY_CURRENT_USER, "Control Panel\\Desktop",
                0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey2, NULL);

    RegSetValueEx (hkey2, "Wallpaper", 0, REG_SZ, (LPBYTE) lpdata, sizeof(lpdata));  

    RegCreateKeyEx will create the stated key, or if it is already in existance, open it.  In order to set a value to a key, you must open it first.  RegSetValueEx then sets the value of the key Wallpaper in HKEY_CURRENT_USER\Control Panel\Desktop to A_picture.bmp

    You should be able to figure out how to add an entry to the section of the registry responsible for getting an app to start automatically; tip: it is HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run...

    HTH
    0
     
    LVL 2

    Accepted Solution

    by:
    //Try this out

    /*
    The program below works in TurboC (or MS C).  In some compilers, you
    have to fold the address to 20 bits, but here the segment and offset
    are separate.*/

    #define MAGIC           0               /* for cold restart */
    /* #define MAGIC           0x1234          /* for warm restart */

    #define BOOT_SEG      0xffffL
    #define BOOT_OFF      0x0000L
    #define BOOT_ADR      ((BOOT_SEG << 16) | BOOT_OFF)

    #define DOS_SEG            0x0040L
    #define RESET_FLAG      0x0072L
    #define RESET_ADR      ((DOS_SEG << 16) | RESET_FLAG)

    main()
    {
          void ((far *fp)()) = (void (far *)()) BOOT_ADR;

          *(int far *)RESET_ADR = MAGIC;
          (*fp)();
          return 0;      /* never gets here, but keeps compiler happy */
    }


    //Let me know if it works
    0
     
    LVL 2

    Expert Comment

    by:mannujam
    depends which compiler u r using
    in VC++ u can use InitiateSystemShutdown
    And the other one i gave runs quiet well in pure dos not in TurboC
    0
     
    LVL 2

    Expert Comment

    by:mannujam
    // this code runs  well in puredos, if u can get some help  from it
    #include<dos.h>
    #define reboot 0x19
    void rebootpc()

    {
    union REGS inregs,outregs;
    int86(reboot, &inregs, &outregs); // Call BIOS
    }
    void main()


    {
    rebootpc();
    }
    0
     
    LVL 19

    Expert Comment

    by:mrwad99
    mannujam: that is some interesting stuff !  Where did you learn how to do that ?!
    0
     

    Author Comment

    by:Xygus
    Well, your asnwer was great, thou I had to make a few changes cause I was using a different compiler called Dev C++, but after all it was a good comment and it works great.  The first reply that I had to this questioned at first thought it was the answer I needed, but then I seen your reply and it was just what I was expecting from this question when I first asked it.

    Thxs alot!

    -Cause only one of my questions was answered, I have only awarded 50 points.

    0
     
    LVL 19

    Expert Comment

    by:mrwad99
    >> Cause only one of my questions was answered, I have only awarded 50 points.

    What do think I was talking about in my first post RE: RegSetValueEx ???
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now