簡體   English   中英

如何 PInvoke SHQueryUserNotificationState 以在 Powershell 中使用?

[英]How do I PInvoke SHQueryUserNotificationState for use in Powershell?

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public enum QUERY_USER_NOTIFICATION_STATE {
        QUNS_NOT_PRESENT = 1,
        QUNS_BUSY = 2,
        QUNS_RUNNING_D3D_FULL_SCREEN = 3,
        QUNS_PRESENTATION_MODE = 4,
        QUNS_ACCEPTS_NOTIFICATIONS = 5,
        QUNS_QUIET_TIME = 6
    };
public class UserWindows {
    [DllImport("shell32.dll")]
    public static extern int SHQueryUserNotificationState(out QUERY_USER_NOTIFICATION_STATE pquns);
}
"@;

$state = [UserWindows]::QUERY_USER_NOTIFICATION_STATE
[UserWindows]::SHQueryUserNotificationState([ref]$state)

我嘗試了幾種不同的方法,但似乎無法讓它完全正常工作。 這是我現在遇到的例外。

Exception calling "SHQueryUserNotificationState" with "1" argument(s): "Cannot 
convert null to type "QUERY_USER_NOTIFICATION_STATE" due to enumeration values that 
are not valid. Specify one of the following enumeration values and try again. The 
possible enumeration values are "QUNS_NOT_PRESENT,QUNS_BUSY,QUNS_RUNNING_D3D_FULL_SCRE
EN,QUNS_PRESENTATION_MODE,QUNS_ACCEPTS_NOTIFICATIONS,QUNS_QUIET_TIME"."
At D:\pintest.ps1:20 char:1
+ [UserWindows]::SHQueryUserNotificationState([ref]$state)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : PSInvalidCastException

我無法弄清楚如何將枚舉引用傳遞給SHQueryUserNotificationState以便它指向QUERY_USER_NOTIFICATION_STATE的特定成員

供參考 - https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shqueryusernotificationstate

P/Invoke 定義是正確的。

像這樣稱呼它:

$state = [QUERY_USER_NOTIFICATION_STATE]::QUNS_NOT_PRESENT
$hresult = [UserWindows]::SHQueryUserNotificationState([ref]$state)
if( $hresult -ge 0 ) {
    # HRESULT >= 0 indicates success
    "User notification state: $state"
}
else {
    # HRESULT < 0 indicates failure
    "Could not query user notification state (HRESULT: $hresult)"
}

可能的 output:

User notification state: QUNS_ACCEPTS_NOTIFICATIONS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM