Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi guys i have written a code in c++ which checks whether autorebootlist.txt file is present or not.
if file is present then delete autorebootlist.text file and then reboot the system
if file not present then system should not reboot at all.

i have written a code but there is some problem while executing like while debugging i came to know that debugger doesnnot enter to if ans else statement.
Can any one rectify it.

C++
 #include "stdafx.h"
 #include<iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <conio.h>
 #include "Shlwapi.h"
 #include "Tlhelp32.h"
 #include<WtsApi32.h>
 #include <Powrprof.h>
 #include<windows.h>
 #include<winnt.h>
 #include <aclapi.h>
 #include <Psapi.h>
 #include <wtsapi32.h>
 #include<string>
 #include<string.h>
 #pragma warning(disable: 4996)
 #pragma comment(lib, "user32.lib")
 #pragma comment(lib, "advapi32.lib")
 #include "Global.h"

//#include "LimitSingleInstance.H"
//using namespace System;
using namespace std;
CLog objlog;

BOOL MySystemShutdown();

int _tmain(int argc, _TCHAR* argv[])
{
    std::string CmdParams[100];

    HANDLE hMutex = CreateMutexA(NULL, FALSE, "my mutex");
    DWORD dwMutexWaitResult = WaitForSingleObject(hMutex, 0);
    if (dwMutexWaitResult != WAIT_OBJECT_0)
    {
        MessageBox(HWND_DESKTOP, TEXT("This application is already running"), TEXT("Information"), MB_OK | MB_ICONINFORMATION);
        CloseHandle(hMutex);
    }
    //strcpy(szInput, CmdParams[0].c_str());
    WIN32_FIND_DATA search_data;

    char Path[1024];
    //char Dir[1024];
    char ExePath[1024];
    GetModuleFileName(NULL, Path, sizeof(Path));
    strcpy(ExePath, Path);
    PathRemoveFileSpec(Path);
    ofstream outputFile(strcat(Path, "\\rebootstatus.txt"), std::ofstream::out);

    memset(&search_data, 0, sizeof(WIN32_FIND_DATA));

    HANDLE handle = FindFirstFile(strcat(Path, "\\autorebootlist.txt"), &search_data);

    while (handle != INVALID_HANDLE_VALUE)
    {


       
        if (FindNextFile(handle, &search_data) == FALSE)
        {
            objlog.WriteToLog("MSG", "autoreboot", "reboot done");
            system("DEL /F autorebootlist.txt");

            std::remove("restarting.exe");
            MySystemShutdown();
            return 0;
        }
        else
        {

            //ofstream outputFile(strcat(Path, "\\rebootstatus.txt"), std::ofstream::out);
            objlog.WriteToLog("MSG", "autoreboot", "reboot not done");
            //printf("sorry file not found");
            outputFile << "autorebootlist.txt file missing!!! sorry you cannot reboot!!!";
            outputFile.close();
        }

    }
}


BOOL MySystemShutdown()
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    // Get a token for this process.
    if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return(FALSE);
    // Get the LUID for the shutdown privilege.
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);
    tkp.PrivilegeCount = 1;  // one privilege to set
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    // Get the shutdown privilege for this process.
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
        (PTOKEN_PRIVILEGES)NULL, 0);
    if (GetLastError() != ERROR_SUCCESS)
        return FALSE;
    // Shut down the system and force all applications to close.
    if (!ExitWindowsEx(EWX_REBOOT,
        SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
        SHTDN_REASON_MINOR_UPGRADE |
        SHTDN_REASON_FLAG_PLANNED))
        return FALSE;
    return TRUE;
}
Posted
Updated 26-May-15 23:42pm
v3
Comments
[no name] 27-May-15 3:46am    
Do you have autorebootlist.text where your debug exe is?
Member 11460314 27-May-15 3:57am    
yes it is kept at the same location where exe is present

1 solution

If you mean this if-else:
C++
while (handle != INVALID_HANDLE_VALUE)
{


    return 0;    //<--- Remove this!
    if (FindNextFile(handle, &search_data) == FALSE)
    {
        objlog.WriteToLog("MSG", "autoreboot", "reboot done");
        system("DEL /F autorebootlist.txt");

        std::remove("restarting.exe");
        MySystemShutdown();
        return 0;
    }
    else
    {

        //ofstream outputFile(strcat(Path, "\\rebootstatus.txt"), std::ofstream::out);
        objlog.WriteToLog("MSG", "autoreboot", "reboot not done");
        //printf("sorry file not found");
        outputFile << "autorebootlist.txt file missing!!! sorry you cannot reboot!!!";
        outputFile.close();
    }

}

Of course the program will exit before to enter your if-else statement.
But you have to check better the warnings that the compiler gives you. I'm sure you got "Unreachable code", but you ignored it!.

The code to do what you ask should be:
C++
strcat(Path, "\\rebootstatus.txt");

FILE *fp = fopen(Path, "r");

if (fp)
{
    fclose(fp);
    objlog.WriteToLog("MSG", "autoreboot", "reboot done");
    system("DEL /F autorebootlist.txt");

    std::remove("restarting.exe");    //If this is actual executable you *cannot* remove it!!!
    MySystemShutdown();
    return 0;
}
else
{
    objlog.WriteToLog("MSG", "autoreboot", "reboot not done");
    //printf("sorry file not found");
    outputFile << "autorebootlist.txt file missing!!! sorry you cannot reboot!!!";
      outputFile.close();
  }
 
Share this answer
 
v3
Comments
Member 11460314 27-May-15 4:57am    
hey i guess return 0 is just a misprint. i removed it and tried to execute it as well but still it was unsuccesful
Frankie-C 27-May-15 6:09am    
What means? Removing the return 0, now the program enters the if-else?
Member 11460314 27-May-15 6:17am    
yes dear program enters if else but it doesnot delete the text file nor it checks the conditions that i am actually looking for.

for example if autorebootlist.txt is present then,
step1 : delete the file
step2: reboot the system

another scenario:
if autorebootlist.txt file is absent
step1: text file missing message pops up
step2: machine should not reboot at all
Frankie-C 27-May-15 7:01am    
I have updated solution using a simple fopen to check for file existence.
Please read the note: if you are trying to remove current executable it will *not* work because the file of a runnning task is locked by the OS.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900