Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rebooting from a C program

Posted on 1999-06-22
28
Medium Priority
?
262 Views
Last Modified: 2009-07-29
I would like my C program to reboot the
computer.  How can this be done?
0
Comment
Question by:Bob99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +7
28 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1262804
sys 64738
;)
0
 
LVL 3

Expert Comment

by:BudVVeezer
ID: 1262805
If your target platform is Windows, you can use:

ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN, 0);
0
 

Author Comment

by:Bob99
ID: 1262806
I could not get my C program to execute that command.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:BudVVeezer
ID: 1262807
You have to have #include <winuser.h>

and be sure to have linked in the import library user32.lib

Try that and see.

~Aaron
 
 

0
 

Author Comment

by:Bob99
ID: 1262808
I'm using  MicroSoft C.  This version doesn't support the winuser.h
include file.  Also, the program I'm writting is strictly a DOS prompt
program.
0
 
LVL 3

Expert Comment

by:BudVVeezer
ID: 1262809
Ahh, I see.  Well, then you would use one of the interrupts in assembly.  That's the only way I have heard of.  And...I've never programmed for DOS...sorry!  I'll take a look around and see if I can find something.

~Aaron
0
 
LVL 2

Expert Comment

by:obg
ID: 1262810
Try jumping to adress ffff:0000 by doing
  ((void far (*)())0xFFFF0000)();

0
 
LVL 1

Expert Comment

by:Lab_Rat
ID: 1262811
Arrrrrggggghhhhhhhhhh!

I've got it somewhere, 3 lines of assembly!
Even resets Windows.
I'll have a look.
0
 
LVL 2

Expert Comment

by:obg
ID: 1262812
Even NT? - Would be interresting.
0
 
LVL 1

Expert Comment

by:Lab_Rat
ID: 1262813
Err, cant find it, but you may like to convert this assembly program.
This is the OFFICIAL way of doing it, not neccesserally the best.

1: %TITLE "Lab_Rats Reboot"
2:
3: IDEAL
4: DOSSEG
5: MODEL small
6: STACK 256
7:
8:
9: WarmBoot  EQU 1234h ;Skips cold boot tests
10: ColdBoot EQU 1234d ;Or any value <>1234h
11:
12:
13: ;We just need to tell the assembler were ResetFlah is located
13:
15: SEGMENT BIOSData at 0040h
16: ORG 0072h
17: LABEL ResetFlag Word
18: ENDS
19:
20:
21: ;Now we tell the assembler were the Reset routine is located.
22:
23: SEGMENT BIOS para at 0F000h
24: ORG 0E05Bh
25: LABEL Reset FAR
26: ENDS
27:
28:
29: CODESEG
30:
31: Start:
32: mov ax,BIOSData ;Address BIOSData segment
33: mov ds,dx  ;with ds, you know it makes sense.
34:
35: ASSUME DS:BIOSData
36: mov [ResetFlag],ColdBoot ; Set the ResetFlag
37: jmp far Reset ; Jump to it boy!
38:
39: END Start ;Viola!

0
 
LVL 2

Expert Comment

by:obg
ID: 1262814
Well, this is almost exactly the same as I've done above. The advantage with your code is the warm boot capability by setting 0040:0072 to 1234h, but that can easily be implemented by inserting *(short far *)0x00400072 = 0x1234; before the call. A major disadvantage with your code is that you jump directly F000:E05B, wich works on ALMOST every BIOS. The reset vector is located at FFFF:0, where there is a JMP to F000:E05B (on most systems). I did not know it could kill Windows as well... The fact that I do a CALL (function call in C) instead of a JMP is of no importance, since one of the first things done in the bootstrap code is resetting the stack.
0
 

Author Comment

by:Bob99
ID: 1262815
obg,
I cut and pasted your line into my program.  I get an error when the program compiles.
0
 
LVL 2

Expert Comment

by:obg
ID: 1262816
Well, I don't... I have no M$ C, but Borland C++ 3.1 and Gnu C works just fine. What is the error? I can't see much that could be wrong... Maybe if you specified that the address is a long, by adding an 'L' like 0xFFFF0000L ?
0
 
LVL 8

Expert Comment

by:jhurst
ID: 1262817
system("\\windows\\rundll.exe user.exe,exitwindows");

0
 
LVL 2

Expert Comment

by:obg
ID: 1262818
Yeah jhurst, that would work nice in a DOS environment... I'd still like to know what was wrong with my answer, and with the warm boot facilities that Lab_Rat brought up, it should be the correct way.
0
 
LVL 1

Expert Comment

by:Lab_Rat
ID: 1262819

void main(void)
{
system("\\windows\\rundll.exe user.exe,exitwindows");
}

void system(char *s)
{
if(strcmp(s,"\\windows\\rundll.exe user.exe,exitwindows"))
     ((void far (*)())0xFFFF0000)();
}

I should be writing the kernal for MS.
:)
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1262820
At least we'd have some laughs in between crashes ;)
0
 

Author Comment

by:Bob99
ID: 1262821
I'm using a 1989 MSC compiler and linker.  Environment is DOS 6.0.
0
 
LVL 8

Accepted Solution

by:
jhurst earned 600 total points
ID: 1262822
well despite the rather snide rmark from obg he is wrong and my previous solution will not work in a DOS environment as you hasv discoved.  The DOS solution is much simpler and follows:

0
 
LVL 8

Expert Comment

by:jhurst
ID: 1262823
well despite the rather snide rmark from obg he is wrong and my previous solution will not work in a DOS environment as you hasv discoved.  The DOS solution is much simpler and follows:

void re_boot(void)
  { /* we need to jump to 0:ffff - this is the bios re-boot location */
  void far (*j) (void) =MK_FP(0xffff,0); /* so make a pointer to it that looks */
     /* like a function */
  j(); /* do it */
  }

0
 
LVL 2

Expert Comment

by:obg
ID: 1262824
WHAT!?!? What do you mean by me being wrong...? And by the way, you are doing exactly same thing I am doing, jhurst, apart from using a little bit more user friendly solution with MK_FP. I still do not have MSC, but maybe I could tell what was wrong with my code if someone would post the error message... I am NOT wrong! You are wrong, however, but only in your comment saying that the reset vector is at 0:ffff, which should be ffff:0. Aarrrrgghh...
0
 
LVL 1

Expert Comment

by:BSoeters
ID: 1262825
Bob99:

If your compiler supports inline assembly statements, and if you're willing to do a cold boot, than do something like this:

asm {
jmp far 0xffff:0
}

or

__asm jmp far 0xffff:0

OBG has given the exact same answer. Wonder what was wrong with his code in the first place. Maybe the 32 bit pointer style....
0
 
LVL 3

Expert Comment

by:sumant032199
ID: 1262826
Use Turbo C++ library function
#include <dos.h>
geninterrupt(0x18);

If you are working in DOS mode computer simply reboots.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1262827
void main(void)
{
      _asm
            {
            push      0ffffh;
            xor      ax,      ax;
            mov      ds,      ax;
            dec      ax;
            push      ax;
            inc      ax;
            push      ax;
            mov      word      ptr ds:[472h],0;
            retf;
            }
}
0
 
LVL 2

Expert Comment

by:obg
ID: 1262828
Ok, yet another proposal for the same procedure... This one is porely written though. Why would you want to push 0FFFFh the first thing you do? Cut off that line, and it will work just as well. And try writing 1234h to 0:472h for a warm reboot.

I was still the first! :-(
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1262829
Hey obg, what happens when you return far?
if (!strcmp("poorly", "porely"))
   {
   puts("obg is a good speller!");
   }
0
 
LVL 2

Expert Comment

by:obg
ID: 1262830
Well, well... - Nobody's perfect. :-)
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6820322
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.
Moondancer
Moderator @ Experts Exchange
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

670 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