using System; using System.Runtime.InteropServices; using Vanara.Extensions; namespace Vanara.PInvoke { public static partial class NetApi32 { /// An administrator's intervention is required. public const string ALERT_ADMIN_EVENT = "ADMIN"; /// An entry was added to the error log. public const string ALERT_ERRORLOG_EVENT = "ERRORLOG"; /// A user or application received a broadcast message. public const string ALERT_MESSAGE_EVENT = "MESSAGE"; /// A print job completed or a print error occurred. public const string ALERT_PRINT_EVENT = "PRINTING"; /// An application or resource was used. public const string ALERT_USER_EVENT = "USER"; /// Name of mailslot to send alert notifications public const string ALERTER_MAILSLOT = "\\\\.\\MAILSLOT\\Alerter"; /// A bitmask describing the status of the print job. [PInvokeData("lmalert.h", MSDNShortId = "f2fd87bc-abde-43c0-b29d-d43cc5f038b8")] [Flags] public enum PRJOB { /// The print job is in the queue waiting to be scheduled. PRJOB_QS_QUEUED = 0, /// The print job is in the queue, but it has been paused. (When a job is paused, it cannot be scheduled.) PRJOB_QS_PAUSED = 1, /// The print job is in the process of being spooled. PRJOB_QS_SPOOLING = 2, /// The job is currently printing. PRJOB_QS_PRINTING = 3, /// The job has completed printing. PRJOB_COMPLETE = 0x4, /// The destination printer requires an operator's intervention. PRJOB_INTERV = 0x8, /// There is an error at the destination printer. PRJOB_ERROR = 0x10, /// The destination printer is offline. PRJOB_DESTOFFLINE = 0x20, /// The destination printer is paused. PRJOB_DESTPAUSED = 0x40, /// A printing alert should be raised. PRJOB_NOTIFY = 0x80, /// The destination printer is out of paper. PRJOB_DESTNOPAPER = 0x100, /// The printing job is being deleted. PRJOB_DELETED = 0x8000, } /// /// The ALERT_OTHER_INFO macro returns a pointer to the alert-specific data in an alert message. The data follows a STD_ALERT /// structure, and can be an ADMIN_OTHER_INFO, a PRINT_OTHER_INFO, or a USER_OTHER_INFO structure. /// /// Pointer to a STD_ALERT structure that was specified in a call to the NetAlertRaise function. /// /// The ALERT_OTHER_INFO macro is defined as follows: /// /// See NetAlertRaise for a code sample that uses the ALERT_OTHER_INFO macro to retrieve a pointer to the /// ADMIN_OTHER_INFO structure. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/nf-lmalert-alert_other_info void ALERT_OTHER_INFO( x ); [PInvokeData("lmalert.h", MSDNShortId = "e7bcc306-4b44-4230-96aa-a4717bb1fb11")] public static IntPtr ALERT_OTHER_INFO(IntPtr x) => x.Offset(Marshal.SizeOf(typeof(STD_ALERT))); /// /// The ALERT_VAR_DATA macro returns a pointer to the variable-length portion of an alert message. Variable-length data can /// follow an ADMIN_OTHER_INFO, a PRINT_OTHER_INFO, or a USER_OTHER_INFO structure. /// /// /// Pointer to an ADMIN_OTHER_INFO, a PRINT_OTHER_INFO, or a USER_OTHER_INFO structure that was specified in a /// call to the NetAlertRaise function or the NetAlertRaiseEx function. /// /// /// The ALERT_VAR_DATA macro is defined as follows: /// /// See NetAlertRaise and NetAlertRaiseEx for code samples that use the ALERT_VAR_DATA macro to retrieve a pointer to the /// variable-length data in an alert message. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/nf-lmalert-alert_var_data void ALERT_VAR_DATA( p ); [PInvokeData("lmalert.h", MSDNShortId = "ff71fb3d-8c01-47ac-93f2-108b1f49e2da")] public static IntPtr ALERT_VAR_DATA(IntPtr p) where T : struct => p.Offset(Marshal.SizeOf(typeof(T))); /// /// [This function is not supported as of Windows Vista because the alerter service is not supported.] /// The NetAlertRaise function notifies all registered clients when a particular event occurs. /// /// To simplify sending an alert message, you can call the extended function NetAlertRaiseEx instead. NetAlertRaiseEx does not /// require that you specify a STD_ALERT structure. /// /// /// /// /// A pointer to a constant string that specifies the alert class (type of alert) to raise. This parameter can be one of the /// following predefined values, or a user-defined alert class for network applications. The event name for an alert can be any text string. /// /// /// /// Name /// Meaning /// /// /// ALERT_ADMIN_EVENT /// An administrator's intervention is required. /// /// /// ALERT_ERRORLOG_EVENT /// An entry was added to the error log. /// /// /// ALERT_MESSAGE_EVENT /// A user or application received a broadcast message. /// /// /// ALERT_PRINT_EVENT /// A print job completed or a print error occurred. /// /// /// ALERT_USER_EVENT /// An application or resource was used. /// /// /// /// /// /// A pointer to the data to send to the clients listening for the interrupting message. The data should begin with a fixed-length /// STD_ALERT structure followed by additional message data in one ADMIN_OTHER_INFO, ERRLOG_OTHER_INFO, PRINT_OTHER_INFO, or /// USER_OTHER_INFO structure. Finally, the buffer should include any required variable-length information. For more information, see /// the code sample in the following Remarks section. /// /// /// The calling application must allocate and free the memory for all structures and variable data. For more information, see Network /// Management Function Buffers. /// /// /// The size, in bytes, of the message buffer. /// /// If the function succeeds, the return value is NERR_Success. /// /// If the function fails, the return value is a system error code and a can be one of the following error codes. For a list of all /// possible error codes, see System Error Codes. /// /// /// /// Return code /// Description /// /// /// ERROR_INVALID_PARAMETER /// /// A parameter is incorrect. This error is returned if the AlertEventName parameter is NULL or an empty string, the Buffer parameter /// is NULL, or the BufferSize parameter is less than the size of the STD_ALERT structure plus the fixed size for the additional /// message data structure. /// /// /// /// ERROR_NOT_SUPPORTED /// The request is not supported. This error is returned on Windows Vista and later since the Alerter service is not supported. /// /// /// /// /// No special group membership is required to successfully execute the NetAlertRaise function. /// /// The alerter service must be running on the client computer when you call the NetAlertRaise function, or the function fails /// with ERROR_FILE_NOT_FOUND. /// /// Examples /// /// The following code sample demonstrates how to raise an administrative alert by calling the NetAlertRaise function and /// specifying STD_ALERT and ADMIN_OTHER_INFO structures. First, the sample calculates the size of the message buffer. Then it /// allocates the buffer with a call to the GlobalAlloc function. The code assigns values to the members of the STD_ALERT and /// the ADMIN_OTHER_INFO portions of the buffer. The sample retrieves a pointer to the ADMIN_OTHER_INFO structure by /// calling the ALERT_OTHER_INFO macro. It also retrieves a pointer to the variable data portion of the buffer by calling the /// ALERT_VAR_DATA macro. Finally, the code sample frees the memory allocated for the buffer with a call to the GlobalFree function. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/nf-lmalert-netalertraise NET_API_STATUS NET_API_FUNCTION // NetAlertRaise( LPCWSTR AlertType, LPVOID Buffer, DWORD BufferSize ); [DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("lmalert.h", MSDNShortId = "11367a72-c21d-4044-98cf-a7a30cc43a8b")] public static extern Win32Error NetAlertRaise([MarshalAs(UnmanagedType.LPWStr)] string AlertType, IntPtr Buffer, uint BufferSize); /// /// [This function is not supported as of Windows Vista because the alerter service is not supported.] /// /// The NetAlertRaiseEx function notifies all registered clients when a particular event occurs. You can call this extended /// function to simplify the sending of an alert message because NetAlertRaiseEx does not require that you specify a STD_ALERT structure. /// /// /// /// /// A pointer to a constant string that specifies the alert class (type of alert) to raise. This parameter can be one of the /// following predefined values, or a user-defined alert class for network applications. (The event name for an alert can be any text string.) /// /// /// /// Name /// Meaning /// /// /// ALERT_ADMIN_EVENT /// An administrator's intervention is required. /// /// /// ALERT_ERRORLOG_EVENT /// An entry was added to the error log. /// /// /// ALERT_MESSAGE_EVENT /// A user or application received a broadcast message. /// /// /// ALERT_PRINT_EVENT /// A print job completed or a print error occurred. /// /// /// ALERT_USER_EVENT /// An application or resource was used. /// /// /// /// /// /// A pointer to the data to send to the clients listening for the interrupting message. The data should consist of one /// ADMIN_OTHER_INFO, ERRLOG_OTHER_INFO, PRINT_OTHER_INFO, or USER_OTHER_INFO structure followed by any required variable-length /// information. For more information, see the code sample in the following Remarks section. /// /// /// The calling application must allocate and free the memory for all structures and variable data. For more information, see Network /// Management Function Buffers. /// /// /// The number of bytes of variable information in the buffer pointed to by the VariableInfo parameter. /// A pointer to a constant string that specifies the name of the service raising the interrupting message. /// /// If the function succeeds, the return value is NERR_Success. /// /// If the function fails, the return value is a system error code and a can be one of the following error codes. For a list of all /// possible error codes, see System Error Codes. /// /// /// /// Return code /// Description /// /// /// ERROR_INVALID_PARAMETER /// /// A parameter is incorrect. This error is returned if the AlertEventName parameter is NULL or an empty string, the ServiceName /// parameter is NULL or an empty string, the VariableInfo parameter is NULL, or the VariableInfoSize parameter is greater than 512 /// minus the size of the STD_ALERT structure. /// /// /// /// ERROR_NOT_SUPPORTED /// The request is not supported. This error is returned on Windows Vista and later since the Alerter service is not supported. /// /// /// /// /// No special group membership is required to successfully execute the NetAlertRaiseEx function. /// /// The alerter service must be running on the client computer when you call the NetAlertRaiseEx function, or the function /// fails with ERROR_FILE_NOT_FOUND. /// /// Examples /// /// The following code sample demonstrates how to raise the following types of interrupting messages (alerts) by calling the /// NetAlertRaiseEx function: /// /// /// /// An administrative alert by specifying an ADMIN_OTHER_INFO structure /// /// /// A print alert by specifying a PRINT_OTHER_INFO structure /// /// /// A user alert by specifying a USER_OTHER_INFO structure /// /// /// /// In each instance the code assigns values to the members of the relevant alert information structure. Following this, the sample /// retrieves a pointer to the portion of the message buffer that follows the structure by calling the ALERT_VAR_DATA macro. The code /// also fills in the variable-length strings in this portion of the buffer. Finally, the sample calls NetAlertRaiseEx to send /// the alert. /// /// /// Note that the calling application must allocate and free the memory for all structures and variable-length data in an alert /// message buffer. /// /// /// To pass a user-defined structure and valid strings in a user alert, you must create an event message file and link it with your /// application. You must also register the application in the EventMessageFile subkey in the EventLog section of the /// registry. If you do not register the application, the user alert will contain the information you pass in the variable-length /// strings that follow the USER_OTHER_INFO structure. For more information about EventMessageFile, see Event Logging. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/nf-lmalert-netalertraiseex NET_API_STATUS NET_API_FUNCTION // NetAlertRaiseEx( LPCWSTR AlertType, LPVOID VariableInfo, DWORD VariableInfoSize, LPCWSTR ServiceName ); [DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("lmalert.h", MSDNShortId = "9762f0d6-0022-4e05-b2d8-6223d7bbb2c8")] public static extern Win32Error NetAlertRaiseEx([MarshalAs(UnmanagedType.LPWStr)] string AlertType, IntPtr VariableInfo, uint VariableInfoSize, [MarshalAs(UnmanagedType.LPWStr)] string ServiceName); /// /// The ADMIN_OTHER_INFO structure contains error message information. The NetAlertRaise and NetAlertRaiseEx functions use the /// ADMIN_OTHER_INFO structure to specify information when raising an administrator's interrupting message. /// /// /// /// Variable-length data follows the ADMIN_OTHER_INFO structure in the alert message buffer if you specify one or more strings /// in the alrtad_numstrings member. The calling application must allocate and free the memory for all structures and /// variable-length data in an alert message buffer. /// /// See NetAlertRaise and NetAlertRaiseEx for code samples that demonstrate how to raise an administrative alert. /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/ns-lmalert-_admin_other_info typedef struct _ADMIN_OTHER_INFO { DWORD // alrtad_errcode; DWORD alrtad_numstrings; } ADMIN_OTHER_INFO, *PADMIN_OTHER_INFO, *LPADMIN_OTHER_INFO; [PInvokeData("lmalert.h", MSDNShortId = "43119dcf-7d04-4e3b-b1dc-20e814fbdc2f")] [StructLayout(LayoutKind.Sequential)] public struct ADMIN_OTHER_INFO { /// Specifies the error code for the new message written to the message log. public uint alrtad_errcode; /// Specifies the number (0-9) of consecutive Unicode strings written to the message log. public uint alrtad_numstrings; } /// /// The ERRLOG_OTHER_INFO structure contains error log information. The NetAlertRaise and NetAlertRaiseEx functions use the /// ERRLOG_OTHER_INFO structure to specify information when adding a new entry to the error log. /// /// /// The calling application must allocate and free the memory for all structures and variable-length data in an alert message buffer. /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/ns-lmalert-_errlog_other_info typedef struct _ERRLOG_OTHER_INFO { // DWORD alrter_errcode; DWORD alrter_offset; } ERRLOG_OTHER_INFO, *PERRLOG_OTHER_INFO, *LPERRLOG_OTHER_INFO; [PInvokeData("lmalert.h", MSDNShortId = "832ebe88-e1c4-4ce3-8057-922419b577f7")] [StructLayout(LayoutKind.Sequential)] public struct ERRLOG_OTHER_INFO { /// Specifies the error code that was written to the error log. public uint alrter_errcode; /// Specifies the offset for the new entry in the error log. public uint alrter_offset; } /// /// The PRINT_OTHER_INFO structure contains information about a print job. The NetAlertRaise and NetAlertRaiseEx functions use /// the PRINT_OTHER_INFO structure to specify information when a job has finished printing, or when a printer needs intervention. /// /// /// /// Additional variable-length data follows the PRINT_OTHER_INFO structure in the alert message buffer. The information is in /// the form of contiguous null-terminated character strings, as follows. /// /// /// /// String /// Meaning /// /// /// computername /// The computer that submitted the print job. /// /// /// username /// The user who submitted the print job. /// /// /// queuename /// The print queue to which the job was submitted. /// /// /// destination /// The printer destination (device) to which the print job was routed. /// /// /// status /// The status of the print job. /// /// /// /// The calling application must allocate and free the memory for all structures and variable-length data in an alert message buffer. /// /// See NetAlertRaiseEx for a code sample that demonstrates how to raise a print alert. /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/ns-lmalert-_print_other_info typedef struct _PRINT_OTHER_INFO { DWORD // alrtpr_jobid; DWORD alrtpr_status; DWORD alrtpr_submitted; DWORD alrtpr_size; } PRINT_OTHER_INFO, *PPRINT_OTHER_INFO, *LPPRINT_OTHER_INFO; [PInvokeData("lmalert.h", MSDNShortId = "f2fd87bc-abde-43c0-b29d-d43cc5f038b8")] [StructLayout(LayoutKind.Sequential)] public struct PRINT_OTHER_INFO { /// /// Type: DWORD /// The identification number of the print job. /// public uint alrtpr_jobid; /// /// Type: DWORD /// A bitmask describing the status of the print job. /// You can obtain the overall status of the job by checking PRJOB_QSTATUS (bits 0 and 1). /// Possible values for the print job status are listed in the Lmalert.h header file. /// /// /// Value /// Meaning /// /// /// PRJOB_QS_QUEUED 0 /// The print job is in the queue waiting to be scheduled. /// /// /// PRJOB_QS_PAUSED 1 /// The print job is in the queue, but it has been paused. (When a job is paused, it cannot be scheduled.) /// /// /// PRJOB_QS_SPOOLING 2 /// The print job is in the process of being spooled. /// /// /// PRJOB_QS_PRINTING 3 /// The job is currently printing. /// /// /// /// If the print job is in the PRJOB_QS_PRINTING state, you can check bits 2 through 8 for the device's status (PRJOB_DEVSTATUS). /// Bit 15 is also meaningful. /// /// Possible values for the device's status are listed in the Lmalert.h header file. /// /// /// Value /// Meaning /// /// /// PRJOB_COMPLETE 0x4 /// The job has completed printing. /// /// /// PRJOB_INTERV 0x8 /// The destination printer requires an operator's intervention. /// /// /// PRJOB_ERROR 0x10 /// There is an error at the destination printer. /// /// /// PRJOB_DESTOFFLINE 0x20 /// The destination printer is offline. /// /// /// PRJOB_DESTPAUSED 0x40 /// The destination printer is paused. /// /// /// PRJOB_NOTIFY 0x80 /// A printing alert should be raised. /// /// /// PRJOB_DESTNOPAPER 0x100 /// The destination printer is out of paper. /// /// /// PRJOB_DELETED 0x8000 /// The printing job is being deleted. /// /// /// public PRJOB alrtpr_status; /// /// Type: DWORD /// /// The time at which the print job was submitted. This value is stored as the number of seconds that have elapsed since /// 00:00:00, January 1, 1970, GMT. /// /// public uint alrtpr_submitted; /// /// Type: DWORD /// The size, in bytes, of the print job. /// public uint alrtpr_size; } /// /// The STD_ALERT structure contains the time and date when a significant event occurred. The structure also contains an alert /// class and the name of the application that is raising the alert message. You must specify the STD_ALERT structure when you /// send an alert message using the NetAlertRaise function. /// /// /// /// The STD_ALERT structure must be followed by one ADMIN_OTHER_INFO, ERRLOG_OTHER_INFO, PRINT_OTHER_INFO, or USER_OTHER_INFO /// structure. These structures can optionally be followed by variable-length data. The calling application must allocate the memory /// for all structures and variable-length data in an alert message buffer. /// /// /// See NetAlertRaise for a code sample that raises an administrative alert using a STD_ALERT structure and an /// ADMIN_OTHER_INFO structure. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/ns-lmalert-_std_alert typedef struct _STD_ALERT { DWORD // alrt_timestamp; WCHAR alrt_eventname[EVLEN + 1]; WCHAR alrt_servicename[SNLEN + 1]; } STD_ALERT, *PSTD_ALERT, *LPSTD_ALERT; [PInvokeData("lmalert.h", MSDNShortId = "daa4594f-e59e-4f05-8183-677bee4ea446")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct STD_ALERT { /// /// Type: DWORD /// /// The time and date of the event. This value is stored as the number of seconds that have elapsed since 00:00:00, January 1, /// 1970, GMT. /// /// public uint alrt_timestamp; /// /// Type: WCHAR[EVLEN + 1] /// /// A Unicode string indicating the alert class (type of event). This parameter can be one of the following predefined values, or /// another alert class that you have defined for network applications. (The event name for an alert can be any text string.) /// /// /// /// Name /// Meaning /// /// /// ALERT_ADMIN_EVENT /// An administrator's intervention is required. /// /// /// ALERT_ERRORLOG_EVENT /// An entry was added to the error log. /// /// /// ALERT_MESSAGE_EVENT /// A user or application received a broadcast message. /// /// /// ALERT_PRINT_EVENT /// A print job completed or a print error occurred. /// /// /// ALERT_USER_EVENT /// An application or resource was used. /// /// /// [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)] public string alrt_eventname; /// /// Type: WCHAR[SNLEN + 1] /// A Unicode string indicating the service application that is raising the alert message. /// [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)] public string alrt_servicename; } /// /// The USER_OTHER_INFO structure contains user error code information. The NetAlertRaise and NetAlertRaiseEx functions use /// the USER_OTHER_INFO structure to specify information about an event or condition of interest to a user. /// /// /// /// Additional variable-length data follows the USER_OTHER_INFO structure in the alert message buffer. The information is in /// the form of contiguous null-terminated character strings, as follows. /// /// /// /// String /// Meaning /// /// /// username /// The user who created the session. /// /// /// computername /// The computer that created the session. /// /// /// /// The calling application must allocate and free the memory for all structures and variable-length data in an alert message buffer. /// /// See NetAlertRaiseEx for a code sample that demonstrates how to raise a user alert. /// // https://docs.microsoft.com/en-us/windows/desktop/api/lmalert/ns-lmalert-_user_other_info typedef struct _USER_OTHER_INFO { DWORD // alrtus_errcode; DWORD alrtus_numstrings; } USER_OTHER_INFO, *PUSER_OTHER_INFO, *LPUSER_OTHER_INFO; [PInvokeData("lmalert.h", MSDNShortId = "2f6bd906-fdab-410a-8856-4482e047371f")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct USER_OTHER_INFO { /// Specifies the error code for the new message in the message log. public uint alrtus_errcode; /// Specifies the number (0-9) of consecutive Unicode strings in the message log. public uint alrtus_numstrings; } } }