VC++ 任务计划自启动 for XP only

/*vc6++ sdk环境编译,建立控制台程序,复制到classview代码里。XP里可以自动运行。
【This code is Task schedul 1.0 for Win XP】整理来自官网示例http://msdn.microsoft.com/en-us/library/windows/desktop/aa383581(v=vs.85).aspx*/

#include "stdafx.h"
#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <objidl.h>
#include <wchar.h>
#include <stdio.h>


void WGetUserName(WCHAR * username, int len)
{
    // Get and display the name of the computer.
    char   szName[255]; 
    ULONG   nLen=sizeof(szName); 
    ::GetUserNameA(szName,&nLen);

    for(unsigned int i = 0; i < strlen(szName); i++)
    {
        username[i] = szName[i];
    }
    username[strlen(szName)] = '\0';
}



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;
    }

    /
    /// Get user name of the computer
    /
    WCHAR username[512];
    memset(username, 0, sizeof(username));
    WGetUserName(username, sizeof(username));

    /
    // Call ITaskScheduler::NewWorkItem to create new task.
    /
    WCHAR pwszTaskName[512] = L"Veno Server WD_TEST_";
    ITask *pITask;

    wcscat(pwszTaskName, username);
    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;
    }

    ///
    // Call ITask::SetApplicationName to specify the Application name
    // for Test Task.
    ///
    LPCWSTR pwszApplicationName = L"C:\\Windows\\System32\\notepad.exe "; 
    hr = pITask->SetApplicationName(pwszApplicationName);

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

    /
    // set flag Only if logged on /
    /
    hr = pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
    if (FAILED(hr))
    {
        wprintf(L"Failed calling ITask::EditWorkItem, ");
        wprintf(L"error = 0x%x\n",hr);
        CoUninitialize();
        return 1;
    }

    
    // set username for only for logged on
    hr = pITask->SetAccountInformation( username, NULL); //L"NT AUTHORITY\\SYSTEM"; 
    if (FAILED(hr))
    {
        wprintf(L"Failed calling ITask::SetApplicationName: ");
        wprintf(L"error = 0x%x\n",hr);
        pITask->Release();
        CoUninitialize();
        return 1;
    }


    ///
    // 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));

    // Add code to set trigger structure?
    pTrigger.wBeginDay =1;                  // Required
    pTrigger.wBeginMonth =1;                // Required
    pTrigger.wBeginYear =1999;              // Required
    pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER); 
    pTrigger.wStartHour = 13;
    pTrigger.TriggerType = TASK_EVENT_TRIGGER_AT_LOGON;   ///TASK_EVENT_TRIGGER_AT_SYSTEMSTART   
    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 IPersistFile::Save to save trigger to disk.
    ///

    IPersistFile *pIPersistFile;
    hr = pITask->QueryInterface(IID_IPersistFile,
        (void **)&pIPersistFile);
    hr = pIPersistFile->Save(NULL,
        TRUE);

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

    wprintf(L"The trigger was created and IPersistFile::Save was \n");
    wprintf(L"called to save the new trigger to disk.\n"); 

    ///
    // Release resources.
    ///

    pITask->Release();
    pITaskTrigger->Release();
    pIPersistFile->Release();
    CoUninitialize();
    printf("Created task.\n");
    return 0;
}

转自

http://www.voidcn.com/article/p-zlahoawb-xx.html

VC 6.0 定时执行任务编写实例,程序运行时显示托盘图标和右键菜单,请设置好时间和需要执行的外部文件路径及文件名,默认情况下是执行外部任务,可选择执行关机、重启、注销等任务,然后启动定时程序,在仅剩1分钟时,程序会显示自动重启警告:计算机将于1分钟内重启,是否取消重启?按"是"则取消自动重启.若允许自动关闭,请保存尚未关闭的程序!"。   本定时程序时间的设置可精确到日期、时间、秒,编写步骤:   ①设置任务执行的日期时间.   ②选择关机,注销,重启,任务模式.   ③选择了任务模式,需要设置任务路径.   ④按下"启动定时"按钮.   ⑤关机前1分钟不段提示是否关机或重启,如果用户一直未取消,电脑将于1分钟内关机或重启.有如下代码可参考:   if(shutTime < CTime::GetCurrentTime())   {    MessageBox("设置的时间不能早于当前系统时间!");   }   else   {    CString bb;    cButton->GetWindowText(bb);    if(bb == "启动定时")    {    if(radio == 1)    {    MessageBox("定时关机已启动!","定时关机已启动");    }    else if(radio == 2)    {    MessageBox("定时注销已启动!","定时注销已启动");    }    else if(radio == 3)    {    MessageBox("定时重启已启动!","定时重启已启动");    }else if(radio == 4)    {    CString command = "";    GetDlgItemText(IDC_EDIT2,command);    if(command == "")    {    MessageBox("请选择要执行的文件!");    return ;    }else    {    MessageBox("定时任务已启动!","定时任务已启动");    }    }    SetTimer(1,1000,0);    AfxGetApp()->m_pMainWnd->ShowWindow(SW_HIDE);       //cButton->SetButtonStyle(WS_DISABLED);    cButton->SetWindowText("停止定时");    }else    {    KillTimer(1);    cButton->SetWindowText("启动定时");    }   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值