netsheduleadd的用法(300分)

  • 主题发起人 主题发起人 hubdog
  • 开始时间 开始时间
H

hubdog

Unregistered / Unconfirmed
GUEST, unregistred user!
我想实现添加计划任务的功能,代码运行有错误
unit insjob;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TAT_INFO = record
JobTime: DWord;
DaysOfMonth: DWord;
DaysOfWeek: UCHAR;
Flags: UCHAR;
Command: PWideChar;
end;

PAT_INFO = ^TAT_INFO;
NET_API_STATUS = LongInt;

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.DFM}
function NetScheduleJobAdd(ServerName: PWideChar; Buffer: PAT_INFO; var JobID: Integer): NET_API_STATUS; external 'netapi32.dll' name 'NetScheduleJobAdd';

procedure TForm1.Button1Click(Sender: TObject);
var
ATInfo:PAT_Info;
jobid:integer;
begin
getmem(atinfo,sizeof(TAt_info));
atinfo^.jobtime:=3*60*60*1000+15*60*1000;//miliseconds from midnight to 3:15
atinfo^.DaysOfMonth:=4294967295;
atinfo^.DaysOfWeek:=255;
atinfo^.command:='c:/showok.exe';
atinfo^.flags:=0;
if NetScheduleJobAdd(nil,atinfo,jobid)=noerror then
showmessage('ok');
freemem(atinfo);
end;
帮我看看,多谢!
 
1.你的运行环境?NetScheduleJobAdd只能在NT下运行;
2.jobid是否应定义成PDWORD=^DWORD;
3.flags可能不能为0;
4.我用C++BUILDER做了一段程序通过了,你试试:

LPDWORD jobid=new DWORD;
NET_API_STATUS err;
AT_INFO at_info;
at_info.JobTime=3*60*60*1000+15*60*1000;//miliseconds from midnight to 3:15
at_info.DaysOfMonth=4294967295;
at_info.DaysOfWeek=255;
at_info.Command=L"C:/WINNT/system32/sol.exe";
at_info.Flags=JOB_RUN_PERIODICALLY
if(err=NetScheduleJobAdd(NULL,(LPBYTE )&at_info
,jobid))ShowMessage("ok");
else ShowMessage("error");

当然得记要
#inlude <lmat.h>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <windows.h>
#include <lmcons.h>
#include <lmaccess.h>
#include <lmerr.h>
#include <lmapibuf.h>
#include <lmuse.h>
 
to sonie:
能告诉我,JOB_RUN_PERIODICALLY,JOB_ADD_CURRENT_DATE,JOB_RUN_PERIODICALLY,JOB_EXEC_ERROR,
JOB_RUNS_TODAY常数值为多少,我这没有C++的头文件,
然后这分就是你的了

hehe,多谢!
 
hehe,这分太容易捡了,谢过huddog,不过你如果DEPHI版的程序通过,可要贴出来呵!我可是弄了半天没出来(秘密!)

#define JOB_RUN_PERIODICALLY 0x01 // set if EVERY


//
// Was there an error last time we tried to exec a program on behalf of
// this job.
// This flag is meaningfull on output only!
//
#define JOB_EXEC_ERROR 0x02 // set if error

//
// Will this job run today or tomorrow.
// This flag is meaningfull on output only!
//
#define JOB_RUNS_TODAY 0x04 // set if today

//
// Add current day of the month to DaysOfMonth input.
// This flag is meaningfull on input only!
//
#define JOB_ADD_CURRENT_DATE 0x08 // set if to add current date


//
// Will this job be run interactively or not. Windows NT 3.1 do not
// know about this bit, i.e. they submit interactive jobs only.
//
#define JOB_NONINTERACTIVE 0x10 // set for noninteractive
 
这是运行通过的Delphi程序
unit insjob;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TAT_INFO = record
JobTime: DWord;
DaysOfMonth: DWord;
DaysOfWeek: UCHAR;
Flags: UCHAR;
Command: PWideChar;
end;

PAT_INFO = ^TAT_INFO;
NET_API_STATUS = LongInt;

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

function NetScheduleJobAdd(ServerName: PWideChar; Buffer: PAT_INFO; var JobID: PDWord): NET_API_STATUS;stdcall;

var
Form1: TForm1;

implementation
{$R *.DFM}
function NetScheduleJobAdd; external 'netapi32.dll' name 'NetScheduleJobAdd';

procedure TForm1.Button1Click(Sender: TObject);
var
ATInfo:PAT_Info;
jobid:PDword;
begin
getmem(atinfo,sizeof(TAt_info));
getmem(jobid,sizeof(dword));
atinfo^.jobtime:=3*60*60*1000+15*60*1000;//miliseconds from midnight to 3:15
atinfo^.DaysOfMonth:=4294967295;
atinfo^.DaysOfWeek:=255;
atinfo^.command:='c:/showok.exe';
atinfo^.flags:=8;


if NetScheduleJobAdd(nil,atinfo,jobid)<>2 then
showmessage('ok');
freemem(jobid);
freemem(atinfo);
end;

end.
谢谢
 
后退
顶部