Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to know how can we make a visual c++ app to start automatically at a specified time, without any user intervention.

I want to do this in Windows environment and will be developing c++ app in Visual Studio 11 Ultimate.
Posted
Comments
Mohibur Rashid 14-Mar-13 3:47am    
you better take a look at schedule task http://windows.microsoft.com/en-us/windows7/schedule-a-task(windows 7)

also look in this page for details http://technet.microsoft.com/en-us/library/cc725744.aspx#BKMK_sys_perms
Harshil Sharma 14-Mar-13 8:11am    
I got the info of steps of creating a ashedule, but these Technet samples are just not updated.

You can't make an app that starts itself at a specific time, because the app must already run to check the time. You can let your app sleep until a specific time has elapsed (e.g. using WaitForSingleObject()). But this requires that the app is started once.

The common solution is to let the Windows Task Scheduler[^] start your app.
 
Share this answer
 
 
Share this answer
 
The all microsoft codes had some bugs. I found this stackoverflow page, please help with this:


C++
#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <objidl.h>
#include <wchar.h>
#include <stdio.h>


int main(int argc, char **argv)
{
  HRESULT hr = S_OK;
  ITaskScheduler *pITS;


  /////////////////////////////////////////////////////////////////
  // Call CoInitialize to initialize the COM library and then 
  // call CoCreateInstance to get the Task Scheduler object. 
  /////////////////////////////////////////////////////////////////
  hr = CoInitialize(NULL);
  if (SUCCEEDED(hr))
  {
     hr = CoCreateInstance(CLSID_CTaskScheduler,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_ITaskScheduler,
                           (void **) &pITS);
     if (FAILED(hr))
     {
        CoUninitialize();
        return 1;
     }
  }
  else
  {
     return 1;
  }


  /////////////////////////////////////////////////////////////////
  // Call ITaskScheduler::NewWorkItem to create new task.
  /////////////////////////////////////////////////////////////////
  LPCWSTR pwszTaskName;
  ITask *pITask;
  IPersistFile *pIPersistFile;
  pwszTaskName = L"Test Task";

  hr = pITS->NewWorkItem(pwszTaskName,         // Name of task
                         CLSID_CTask,          // Class identifier 
                         IID_ITask,            // Interface identifier
                         (IUnknown**)&pITask); // Address of task 
                                                                                                                                                                                            //  interface


  pITS->Release();                               // Release object
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling NewWorkItem, error = 0x%x\n",hr);
     return 1;
  }

  /////////////////////////////////////////////////////////////////
  //Set Comment, Name, Working dir, Params
  /////////////////////////////////////////////////////////////////
  pITask->SetComment(L"This is a comment");
  pITask->SetApplicationName(L"C:\\Windows\\System32\\notepad.exe");
  pITask->SetWorkingDirectory(L"C:\\Windows\\System32");
  pITask->SetParameters(L"");

  ///////////////////////////////////////////////////////////////////
  // Call ITask::CreateTrigger to create new trigger.
  ///////////////////////////////////////////////////////////////////

  ITaskTrigger *pITaskTrigger;
  WORD piNewTrigger;
  hr = pITask->CreateTrigger(&piNewTrigger,
                             &pITaskTrigger);
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::CreatTrigger: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    CoUninitialize();
    return 1;
  }

//////////////////////////////////////////////////////
  // Define TASK_TRIGGER structure. Note that wBeginDay,
  // wBeginMonth, and wBeginYear must be set to a valid 
  // day, month, and year respectively.
  //////////////////////////////////////////////////////

  TASK_TRIGGER pTrigger;
  ZeroMemory(&pTrigger, sizeof (TASK_TRIGGER));

  LPSYSTEMTIME lpSystemTime;
  GetLocalTime(lpSystemTime);


  // Add code to set trigger structure?
  pTrigger.wBeginDay = lpSystemTime->wDay;                  // Required
  pTrigger.wBeginMonth = lpSystemTime->wMonth;                // Required
  pTrigger.wBeginYear =lpSystemTime->wYear;              // Required
  pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER); 
  pTrigger.wStartHour = lpSystemTime->wHour;
  pTrigger.wStartMinute = lpSystemTime->wMinute + 2;
  pTrigger.TriggerType = TASK_TIME_TRIGGER_DAILY;
  pTrigger.Type.Daily.DaysInterval = 1;


  ///////////////////////////////////////////////////////////////////
  // Call ITaskTrigger::SetTrigger to set trigger criteria.
  ///////////////////////////////////////////////////////////////////

  hr = pITaskTrigger->SetTrigger (&pTrigger);
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITaskTrigger::SetTrigger: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    pITaskTrigger->Release();
    CoUninitialize();
    return 1;
  }




  /////////////////////////////////////////////////////////////////
  // Call IUnknown::QueryInterface to get a pointer to 
  // IPersistFile and IPersistFile::Save to save 
  // the new task to disk.
  /////////////////////////////////////////////////////////////////

  hr = pITask->QueryInterface(IID_IPersistFile,
                              (void **)&pIPersistFile);

  pITask->Release();
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling QueryInterface, error = 0x%x\n",hr);
     return 1;
  }


  hr = pIPersistFile->Save(NULL,
                           TRUE);
  pIPersistFile->Release();
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling Save, error = 0x%x\n",hr);
     return 1;
  }


  CoUninitialize();
  printf("Created task.\n");
  return 0;
}</stdio.h></wchar.h></objidl.h></msterr.h></mstask.h></ole2.h></initguid.h></windows.h>


The person says that after adding:

<pre lang="c++">pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);


and this:

C++
hr = pITask->SetAccountInformation(L"", 
            NULL);


  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::SetAccountInformation: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    CoUninitialize();
    return 1;
  }


the code works. Can someone tell me where to add these codes? I added them at last but got the error: LPSYSTEMTIME used without initializing
 
Share this answer
 
Comments
Andreas Gieriet 18-Mar-13 9:26am    
This is not a solution that you post. Remove it and "improve" your question if needed.

Please read the other solutions. Why the heck does one want to do a "self-registerting" app. Do that as manual or automated installation step, but not by starting the app itself and registering itself to the task scheduler.

BTW: What do you mean by "The all Microsoft code has some bugs"? Yeah, that's software as any other software and therefore has bugs. But it sounds ridiculous if you make such a claim when you don't understand the posted code...

And: Ask the author of that article if you want an answer.

Andi

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