【问题标题】:Problem Using user32.dll in C# (Error 1008 An attempt was made to reference a token that does not exist.)在 C# 中使用 user32.dll 时出现问题(错误 1008 尝试引用不存在的令牌。)
【发布时间】:2020-01-04 08:46:51
【问题描述】:

你好传奇的编码员。

流经我的previous question 我尝试在 C# 语言的 Windows 通用应用程序 (UWP) 中使用user32.dll,但在尝试使用我从那个 .dll

导入的方法

这是我的代码:

[DllImport("user32.dll")]
public static extern bool LockWorkStation();
private async void btnLock_Click(object sender, RoutedEventArgs e)
{
    string path;
    if (Images.TryGetValue(selectedRadioButton.Name, out path))
    {
        StorageFile file = await StorageFile.GetFileFromPathAsync(path);
        await LockScreen.SetImageFileAsync(file);
        if (!LockWorkStation())
            throw new Exception(Marshal.GetLastWin32Error().ToString());
    }
}

如您所见,我从user32.dll 导入了LockWorkStation() mthod,并在按钮的事件侦听器中使用了它。 ImagesDictionary<string,string> 并且一切都Fine 除非调用方法 LockWorkStation() 它总是返回 false 并且抛出的错误是 1008 我在 中提到过标题问题是为什么?我如何分配令牌?

注意:任何方式,任何方式锁定屏幕都是令人钦佩的。

【问题讨论】:

  • 不,UWP 应用无法使用该功能。它在可以检测此类违规行为的沙箱中运行,这可能是“无效令牌”错误的来源。
  • 我可以使用服务来调用它还是目标机器上的 Web 服务器。 @HansPassant
  • 尝试设置最后一个错误为真然后得到窗口错误:pinvoke.net/default.aspx/user32.LockWorkStation
  • 我试过它返回 5 我认为这意味着(访问被拒绝)@jdweng​​span>
  • 是的,访问被拒绝。搜索“window lock screen access denied”

标签: c# uwp token user32 windows-8.1-universal


【解决方案1】:

所以在搜索了很多并且无法从通用 Windows 应用程序平台直接锁定屏幕后,我向本地 Web 服务器发送了一个 Web 请求,并让该 Web 服务器使用 user32.dll 并锁定屏幕。

这是 UWP 应用中的代码:

       try
               {
                   HttpClient httpClient = new HttpClient();
                   Uri uri = new Uri("http://localhost:8080/lock/");

                   HttpStringContent content = new HttpStringContent(
                       "{ \"pass\": \"theMorteza@1378App\" }",
                       UnicodeEncoding.Utf8,
                       "application/json");

                   HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
                       uri,
                       content);

                   httpResponseMessage.EnsureSuccessStatusCode();
               }
               catch (Exception ex)
               {
                   throw ex;
               }

并且在网络服务器中有代码:


        [DllImport("user32.dll")]
        public static extern bool LockWorkStation();
        private static string LockTheScreen(HttpListenerRequest request)
        {
            var inputStream = request.InputStream;
            try
            {

                using (StreamReader sr = new StreamReader(inputStream))
                {
                    JToken pass = JToken.Parse(sr.ReadToEnd());
                    if (pass.Value<string>("pass") == "theMorteza@1378App")
                    {
                        LockWorkStation();
                    }
                }
            }
            catch (Exception)
            {

                return "fail";
            }
            return "fail";
        }

注意:您可以找到如何制作简单的网络服务器here
但是:您必须安装网络服务器并授予用户访问权限。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2014-08-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多