Restart a computer using function pointers in C

后端 未结 3 894
一整个雨季
一整个雨季 2021-01-28 00:37

I was learning about function pointers in C when I came across a program that restarts the computer upon execution.

void (*f) (void);
f=(void (*)(void) MK_FP(0xF         


        
3条回答
  •  鱼传尺愫
    2021-01-28 01:10

    This is a system dependent (ie: non-portable) approach to restarting the machine. What is the intended OS for this program to run on? Is it DOS (I'm assuming). Try running it inside a virtual machine and find out.

    If you want this to work on other platforms, you'll need to write some platform specific stuff, like so:

    #ifdef __linux__
      system("shutdown -P now");
    #elif _WIN32
      #if (WINVER == NTDDI_WIN7) // Windows 7
        system("C:\\WINDOWS\\System32\\shutdown /s");
      #endif
      #if ((WINVER <= NTDDI_WINXPSP3) && (WINVER >= NTDDI_WINXP)) // Windows XP
        system("C:\\WINDOWS\\System32\\shutdown -s");
      #endif
    #else
      #error System not recognized
    #endif
    

提交回复
热议问题