using System; using System.Runtime.InteropServices; namespace Vanara.PInvoke { public static partial class NetApi32 { /// Flags used by AT structures. [PInvokeData("lmat.h", MSDNShortId = "eb0bf696-53ca-432a-b04c-5e0b6a61a0fd")] [Flags] public enum AtJobFlags : byte { /// /// If you set this flag, the job runs, and continues to run, on each day for which a corresponding bit is set in the DaysOfMonth /// member or the DaysOfWeek member. The job is not deleted after it executes. /// /// If this flag is clear, the job runs only once for each bit set in these members. The job is deleted after it executes once. /// /// JOB_RUN_PERIODICALLY = 0x01, /// /// If this flag is set, it indicates that the schedule service failed to successfully execute the job the last time it was /// scheduled to run. /// JOB_EXEC_ERROR = 0x02, /// /// If this flag is set, it indicates that the job is scheduled to execute on the current day; the value of the JobTime member is /// greater than the current time of day at the computer where the job is queued. /// JOB_RUNS_TODAY = 0x04, /// /// If you set this flag, the job executes at the first occurrence of JobTime member at the computer where the job is queued. /// Setting this flag is equivalent to setting the bit for the current day in the DaysOfMonth member. /// JOB_ADD_CURRENT_DATE = 0x08, /// If you set this flag, the job does not run interactively. If this flag is clear, the job runs interactively. JOB_NONINTERACTIVE = 0x10, /// Valid input flags. JOB_INPUT_FLAGS = (JOB_RUN_PERIODICALLY | JOB_ADD_CURRENT_DATE | JOB_NONINTERACTIVE), /// Valid output flags. JOB_OUTPUT_FLAGS = (JOB_RUN_PERIODICALLY | JOB_EXEC_ERROR | JOB_RUNS_TODAY | JOB_NONINTERACTIVE) } /// /// [ NetScheduleJobAdd is no longer available for use as of Windows 8. Instead, use the Task Scheduler 2.0 Interfaces. /// ] /// /// The NetScheduleJobAdd function submits a job to run at a specified future time and date. This function requires that the /// schedule service be started on the computer to which the job is submitted. /// /// /// /// A pointer to a constant string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. /// If this parameter is NULL, the local computer is used. /// /// /// A pointer to an AT_INFO structure describing the job to submit. For more information about scheduling jobs using different job /// properties, see the following Remarks section and Network Management Function Buffers. /// /// /// A pointer that receives a job identifier for the newly submitted job. This entry is valid only if the function returns successfully. /// /// /// If the function succeeds, the return value is NERR_Success. /// If the function fails, the return value is a system error code. For a list of error codes, see System Error Codes. /// /// /// /// Normally only members of the local Administrators group on the computer where the schedule job is being added can successfully /// execute this function. If the server name passed in the string pointed to by the Servername parameter is a remote server, then /// only members of the local Administrators group on the remote server can successfully execute this function. /// /// /// If the following registry value has the least significant bit set (for example, 0x00000001), then users belonging to the Server /// Operators group can also successfully execute this function. /// /// HKLM\System\CurrentControlSet\Control\Lsa\SubmitControl /// /// The following are examples of how to schedule jobs using different properties supported by the NetScheduleJobAdd function. /// /// To schedule a job that executes once: /// /// /// Set the DaysOfMonth member of the AT_INFO structure to zero. /// /// /// Set the DaysOfWeek member of the AT_INFO structure to zero. /// /// /// Set the JobTime member of the AT_INFO structure to the time the job should execute. /// /// /// /// The job executes at the time specified by the JobTime member of the AT_INFO structure pointed to by the Buffer parameter. /// After the job executes, it is deleted. /// /// To schedule and delete a job that executes multiple times: /// /// /// Set the appropriate bits in the DaysOfMonth member of the AT_INFO structure or /// /// /// Set the appropriate bits in the DaysOfWeek member of the AT_INFO structure. /// /// /// Set the JobTime member of the AT_INFO structure to the time the job should execute. /// /// /// Note You do not need to set both the DaysOfMonth and the DaysOfWeek members of the AT_INFO structure. /// /// The job executes at the time specified by the JobTime member of the AT_INFO structure pointed to by the Buffer parameter, /// once for each day set in the DaysOfMonth or DaysOfWeek members of the AT_INFO structure. After each job executes, /// the corresponding bit is cleared. When the last bit is cleared, the job is deleted. /// /// To schedule a job that executes periodically: /// /// /// Set the appropriate bits in the DaysOfMonth member of the AT_INFO structure or /// /// /// Set the appropriate bits in the DaysOfWeek member of the AT_INFO structure. /// /// /// Set the JobTime member of the AT_INFO structure to the time the job should execute. /// /// /// Set the job submission flag JOB_RUN_PERIODICALLY in the Flags member of the AT_INFO structure. /// /// /// Note You do not need to set both the DaysOfMonth and the DaysOfWeek members of the AT_INFO structure. /// /// The job will execute periodically, at the time specified by the JobTime member of the AT_INFO structure pointed to by the /// Buffer parameter, on each day set in the DaysOfMonth or DaysOfWeek member of the AT_INFO structure. The job will /// not be deleted as a result of the repeated executions. The only way to delete the job is by an explicit call to the /// NetScheduleJobDel function. /// /// See the AT_INFO structure for a description of the DaysOfWeek, DaysOfMonth, and job property bitmasks. /// /// On Windows 2000, the earlier AT service and the Task Scheduler were combined. The Task Scheduler service was only accurate to the /// minute. Therefore, the NetScheduleJobAdd function only uses hours and minutes specified in the JobTime member of /// the AT_INFO structure when a job is scheduled to run. /// /// /// Starting with Windows Vista, the precision for the Task Scheduler was increased to the second. Therefore, the /// NetScheduleJobAdd function uses only the hours, minutes, and seconds specified in the JobTime member of the AT_INFO /// structure when a job is scheduled to run. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmat/nf-lmat-netschedulejobadd NET_API_STATUS NET_API_FUNCTION // NetScheduleJobAdd( IN LPCWSTR Servername, IN LPBYTE Buffer, OUT LPDWORD JobId ); [DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("lmat.h", MSDNShortId = "813d13ba-abe1-4b14-88c7-87ba88a42a3b")] public static extern Win32Error NetScheduleJobAdd([Optional] string Servername, in AT_INFO Buffer, out uint JobId); /// /// [ NetScheduleJobDel is no longer available for use as of Windows 8. Instead, use the Task Scheduler 2.0 Interfaces. /// ] /// /// The NetScheduleJobDel function deletes a range of jobs queued to run at a computer. This function requires that the /// schedule service be started at the computer to which the job deletion request is being sent. /// /// /// /// A pointer to a constant string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. /// If this parameter is NULL, the local computer is used. /// /// The minimum job identifier. Jobs with a job identifier smaller than MinJobId will not be deleted. /// The maximum job identifier. Jobs with a job identifier larger than MaxJobId will not be deleted. /// /// If the function succeeds, the return value is NERR_Success. /// If the function fails, the return value is a system error code. For a list of error codes, see System Error Codes. /// /// /// /// Normally only members of the local Administrators group on the computer where the schedule job is being deleted can successfully /// execute this function. If the server name passed in the string pointed to by the Servername parameter is a remote server, then /// only members of the local Administrators group on the server can successfully execute this function. /// /// /// If the following registry value has the least significant bit set (for example, 0x00000001), then users belonging to the Server /// Operators group can also successfully execute this function. /// /// HKLM\System\CurrentControlSet\Control\Lsa\SubmitControl /// Call the NetScheduleJobEnum function to retrieve the job identifier for one or more scheduled jobs. /// The NetScheduleJobDel function deletes all jobs whose job identifiers are in the range MinJobId through MaxJobId. /// /// To delete all scheduled jobs at the server, you can call NetScheduleJobDel specifying MinJobId equal to 0 and MaxJobId /// equal to – 1. To delete one job, specify the job's identifier for both the MinJobId parameter and the MaxJobId parameter. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmat/nf-lmat-netschedulejobdel NET_API_STATUS NET_API_FUNCTION // NetScheduleJobDel( IN LPCWSTR Servername, IN DWORD MinJobId, IN DWORD MaxJobId ); [DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("lmat.h", MSDNShortId = "5ae668ab-f51d-457e-a239-2ec16a0e5a55")] public static extern Win32Error NetScheduleJobDel([Optional] string Servername, uint MinJobId, uint MaxJobId); /// /// [ NetScheduleJobEnum is no longer available for use as of Windows 8. Instead, use the Task Scheduler 2.0 Interfaces. /// ] /// /// The NetScheduleJobEnum function lists the jobs queued on a specified computer. This function requires that the schedule /// service be started. /// /// /// /// A pointer to a constant string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. /// If this parameter is NULL, the local computer is used. /// /// /// A pointer to the buffer that receives the data. The return information is an array of AT_ENUM structures. The buffer is allocated /// by the system and must be freed using a single call to the NetApiBufferFree function. Note that you must free the buffer even if /// the function fails with ERROR_MORE_DATA. /// /// /// A value that indicates the preferred maximum length of the returned data, in bytes. If you specify MAX_PREFERRED_LENGTH, the /// function allocates the amount of memory required for the data. If you specify another value in this parameter, it can restrict /// the number of bytes that the function returns. If the buffer size is insufficient to hold all entries, the function returns /// ERROR_MORE_DATA. For more information, see Network Management Function Buffers and Network Management Function Buffer Lengths. /// /// A pointer to a value that receives the count of elements actually enumerated. /// /// A pointer to a value that receives the total number of entries that could have been enumerated from the current resume position. /// Note that applications should consider this value only as a hint. /// /// /// A pointer to a value that contains a resume handle which is used to continue a job enumeration. The handle should be zero on the /// first call and left unchanged for subsequent calls. If this parameter is NULL, then no resume handle is stored. /// /// /// If the function succeeds, the return value is NERR_Success. /// If the function fails, the return value is a system error code. For a list of error codes, see System Error Codes. /// /// /// /// Normally only members of the local Administrators group on the computer where the schedule job is being enumerated can /// successfully execute this function. If the server name passed in the string pointed to by the Servername parameter is a remote /// server, then only members of the local Administrators group on the server can successfully execute this function. /// /// /// If the following registry value has the least significant bit set (for example, 0x00000001), then users belonging to the Server /// Operators group can also successfully execute this function. /// /// HKLM\System\CurrentControlSet\Control\Lsa\SubmitControl /// /// Each entry returned contains an AT_ENUM structure. The value of the JobId member can be used when calling functions that /// require a job identifier parameter, such as the NetScheduleJobDel function. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmat/nf-lmat-netschedulejobenum NET_API_STATUS NET_API_FUNCTION // NetScheduleJobEnum( IN LPCWSTR Servername, OUT LPBYTE *PointerToBuffer, IN DWORD PrefferedMaximumLength, OUT LPDWORD EntriesRead, // OUT LPDWORD TotalEntries, IN OUT LPDWORD ResumeHandle ); [DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("lmat.h", MSDNShortId = "e3384414-6a15-4979-bed4-6f94f046474a")] public static extern Win32Error NetScheduleJobEnum([Optional] string Servername, out SafeNetApiBuffer PointerToBuffer, uint PrefferedMaximumLength, out uint EntriesRead, out uint TotalEntries, ref uint ResumeHandle); /// /// [ NetScheduleJobGetInfo is no longer available for use as of Windows 8. Instead, use the Task Scheduler 2.0 Interfaces. /// ] /// /// The NetScheduleJobGetInfo function retrieves information about a particular job queued on a specified computer. This /// function requires that the schedule service be started. /// /// /// /// A pointer to a constant string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. /// If this parameter is NULL, the local computer is used. /// /// A value that indicates the identifier of the job for which to retrieve information. /// /// A pointer to the buffer that receives the AT_INFO structure describing the specified job. This buffer is allocated by the system /// and must be freed using the NetApiBufferFree function. For more information, see Network Management Function Buffers and Network /// Management Function Buffer Lengths. /// /// /// If the function succeeds, the return value is NERR_Success. /// If the function fails, the return value is a system error code. For a list of error codes, see System Error Codes. /// /// /// /// Normally only members of the local Administrators group on the computer where the schedule job is being enumerated can /// successfully execute this function. If the server name passed in the string pointed to by the Servername parameter is a remote /// server, then only members of the local Administrators group on the server can successfully execute this function. /// /// /// If the following registry value has the least significant bit set (for example, 0x00000001), then users belonging to the Server /// Operators group can also successfully execute this function. /// /// HKLM\System\CurrentControlSet\Control\Lsa\SubmitControl /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmat/nf-lmat-netschedulejobgetinfo NET_API_STATUS NET_API_FUNCTION // NetScheduleJobGetInfo( IN LPCWSTR Servername, IN DWORD JobId, OUT LPBYTE *PointerToBuffer ); [DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("lmat.h", MSDNShortId = "44589715-edab-4737-9e49-6f491fd44c28")] public static extern Win32Error NetScheduleJobGetInfo([Optional] string Servername, uint JobId, out SafeNetApiBuffer PointerToBuffer); /// /// /// The AT_ENUM structure contains information about a submitted job. The NetScheduleJobEnum function uses this structure to /// enumerate and return information about an entire queue of submitted jobs. /// /// /// /// /// For more information about setting the bit flags to schedule jobs that execute once, jobs that execute multiple times, and jobs /// that execute periodically without deletion, see the NetScheduleJobAdd function. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmat/ns-lmat-_at_enum typedef struct _AT_ENUM { DWORD JobId; DWORD_PTR // JobTime; DWORD DaysOfMonth; UCHAR DaysOfWeek; UCHAR Flags; LPWSTR Command; } AT_ENUM, *PAT_ENUM, *LPAT_ENUM; [PInvokeData("lmat.h", MSDNShortId = "ed7c5171-b8aa-4a9a-8f31-4d914bcad0b1")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct AT_ENUM { /// /// Type: DWORD /// The job identifier of a submitted (queued) job. /// public uint JobId; /// /// Type: DWORD_PTR /// /// A pointer to the time of day at which the job is scheduled to run. The time is the local time at a computer on which the /// schedule service is running; it is measured from midnight, and is expressed in milliseconds. /// /// public IntPtr JobTime; /// /// Type: DWORD /// /// A set of bit flags representing the days of the month. For each bit that is set, the scheduled job will run at the time /// specified by the JobTime member, on the corresponding day of the month. Bit 0 corresponds to the first day of the /// month, and so on. /// /// /// The value of the bitmask is zero if the job was scheduled to run only once, at the first occurrence specified in the /// JobTime member /// /// public uint DaysOfMonth; /// /// Type: UCHAR /// /// A set of bit flags representing the days of the week. For each bit that is set, the scheduled job will run at the time /// specified by the JobTime member, on the corresponding day of the week. Bit 0 corresponds to Monday, and so on. /// /// /// The value of the bitmask is zero if the job was scheduled to run only once, at the first occurrence specified in the /// JobTime member. /// /// public byte DaysOfWeek; /// /// Type: UCHAR /// A set of bit flags describing job properties. This member can be one or more of the following values. /// /// /// Value /// Meaning /// /// /// JOB_RUN_PERIODICALLY /// This flag is equal to its original value, that is, the value when the job was submitted. /// /// /// JOB_EXEC_ERROR /// /// If this flag is set, it indicates that the schedule service failed to successfully execute the job the last time it was /// scheduled to run. /// /// /// /// JOB_RUNS_TODAY /// /// If this flag is set, it indicates that the job is scheduled to execute on the current day; the value of the JobTime member is /// greater than the current time of day at the computer where the job is queued. /// /// /// /// JOB_NONINTERACTIVE /// This flag is equal to its original value, that is, the value when the job was submitted. /// /// /// public AtJobFlags Flags; /// /// Type: LPWSTR /// A pointer to a Unicode string that contains the name of the command, batch program, or binary file to execute. /// public string Command; } /// /// /// The AT_INFO structure contains information about a job. The NetScheduleJobAdd function uses the structure to specify /// information when scheduling a job. The NetScheduleJobGetInfo function uses the structure to retrieve information about a job that /// has already been submitted. /// /// /// /// /// For more information about scheduling jobs that execute once, jobs that execute multiple times, and jobs that execute /// periodically without deletion, see NetScheduleJobAdd. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmat/ns-lmat-_at_info typedef struct _AT_INFO { DWORD_PTR JobTime; DWORD // DaysOfMonth; UCHAR DaysOfWeek; UCHAR Flags; LPWSTR Command; } AT_INFO, *PAT_INFO, *LPAT_INFO; [PInvokeData("lmat.h", MSDNShortId = "eb0bf696-53ca-432a-b04c-5e0b6a61a0fd")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct AT_INFO { /// /// Type: DWORD_PTR /// /// A pointer to a value that indicates the time of day at which the job is scheduled to run. The time is the local time at a /// computer on which the schedule service is running; it is measured from midnight, and is expressed in milliseconds. /// /// public IntPtr JobTime; /// /// Type: DWORD /// /// A set of bit flags representing the days of the month. For each bit that is set, the scheduled job will run at the time /// specified by the JobTime member, on the corresponding day of the month. Bit 0 corresponds to the first day of the /// month, and so on. /// /// /// The value of the bitmask is zero if the job was scheduled to run only once, at the first occurrence specified by the /// JobTime member. /// /// public uint DaysOfMonth; /// /// Type: UCHAR /// /// A set of bit flags representing the days of the week. For each bit that is set, the scheduled job will run at the time /// specified by the JobTime member, on the corresponding day of the week. Bit 0 corresponds to Monday, and so on. /// /// /// The value of the bitmask is zero if the job was scheduled to run only once, at the first occurrence specified by the /// JobTime member. /// /// public byte DaysOfWeek; /// /// Type: UCHAR /// A set of bit flags describing job properties. /// When you submit a job using a call to the NetScheduleJobAdd function, you can specify one of the following values. /// /// /// Value /// Meaning /// /// /// JOB_RUN_PERIODICALLY /// /// If you set this flag, the job runs, and continues to run, on each day for which a corresponding bit is set in the DaysOfMonth /// member or the DaysOfWeek member. The job is not deleted after it executes. If this flag is clear, the job runs only once for /// each bit set in these members. The job is deleted after it executes once. /// /// /// /// JOB_ADD_CURRENT_DATE /// /// If you set this flag, the job executes at the first occurrence of JobTime member at the computer where the job is queued. /// Setting this flag is equivalent to setting the bit for the current day in the DaysOfMonth member. /// /// /// /// JOB_NONINTERACTIVE /// If you set this flag, the job does not run interactively. If this flag is clear, the job runs interactively. /// /// /// /// When you call NetScheduleJobGetInfo to retrieve job information, the function can return one or more of the following values. /// /// /// /// Value /// Meaning /// /// /// JOB_RUN_PERIODICALLY /// This flag is equal to its original value, that is, the value when the job was submitted. /// /// /// JOB_EXEC_ERROR /// /// If this flag is set, it indicates that the schedule service failed to successfully execute the job the last time it was /// scheduled to run. /// /// /// /// JOB_RUNS_TODAY /// /// If this flag is set, it indicates that the job is scheduled to execute on the current day; the value of the JobTime member is /// greater than the current time of day at the computer where the job is queued. /// /// /// /// JOB_NONINTERACTIVE /// This flag bit is equal to its original value, that is, the value when the job was submitted. /// /// /// public AtJobFlags Flags; /// /// Type: LPWSTR /// A pointer to a Unicode string that contains the name of the command, batch program, or binary file to execute. /// public string Command; } } }