在指定时间自动启动c ++应用程序 [英] Automatically start c++ application at specified time
本文介绍了在指定时间自动启动c ++应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在没有任何用户干预的情况下使视觉c ++应用程序在指定时间自动启动。
我想做什么这在Windows环境中将在Visual Studio 11 Ultimate中开发c ++应用程序。
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.
推荐答案
你不能创建一个在特定时间开始的应用程序,因为应用程序必须已运行以检查时间。您可以让应用程序休眠直到特定时间过去(例如,使用WaitForSingleObject()
)。但这要求应用程序启动一次。
常见的解决方案是让 Windows任务计划程序 [ ^ ]启动你的应用。
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. usingWaitForSingleObject()
). But this requires that the app is started once.
The common solution is to let the Windows Task Scheduler[^] start your app.
使用任务计划程序
ref this http://msdn.microsoft.com/en-us/library/aa446828%28v=vs.85%29.aspx [ ^ ]
Use Task Scheduler
ref this http://msdn.microsoft.com/en-us/library/aa446828%28v=vs.85%29.aspx[^]
全部微软代码有一些错误。我找到了这个stackoverflow页面,请帮助:
The all microsoft codes had some bugs. I found this stackoverflow page, please help with this:
#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:
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;
}
代码有效。有人能告诉我在哪里添加这些代码吗?我最后添加了它们但得到了错误:LPSYSTEMTIME在没有初始化的情况下使用
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
这篇关于在指定时间自动启动c ++应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文