在 C# 中使用 user32.dll 的问题(错误 1008 尝试引用不存在的令牌.) [英] Problem Using user32.dll in C# (Error 1008 An attempt was made to reference a token that does not exist.)
问题描述
你好传奇程序员.
通过我的上一个问题 我尝试在 C# 语言的 windows 通用应用程序 (UWP) 中使用 user32.dll
,但在尝试时遇到错误使用我从 .dll
Flowing by my previous question I tried to use user32.dll
in windows universal application (UWP) in C# language but I encountered an error while trying to use the method I imported from that .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()
方法,并在按钮的事件侦听器中使用了它.Images
是一个 Dictionary
并且每件事都是 Fine 除非调用方法 LockWorkStation()
> 它总是返回 false
,所以抛出的错误是 1008 我在 Title 中提到过 问题是 为什么? 和 怎么能我分配一个令牌?
as you can see I imported LockWorkStation()
mthod from user32.dll
and I used it in the event listener of a button. the Images
is a Dictionary<string,string>
and every thing is Fine unless the call to method LockWorkStation()
it always return false
and so the thrown error is 1008 I mentioned it in the Title The question is Why? and how can I assign a token?
注意:任何方式,任何方式锁定屏幕都是令人钦佩的.
Note: any way,any way to lock the screen is admirable.
推荐答案
所以在搜索了很多并且无法从通用 Windows 应用程序平台直接锁定屏幕后,我向本地 Web 服务器发送了一个 Web 请求,然后我做了那个Web 服务器使用 user32.dll
并锁定屏幕.
So after searching a lot and being hopeless from directly lock the screen from the universal windows application platform I send a web request to a local web server and I made that web server to use the user32.dll
and lock the screen.
这是 UWP 应用中的代码:
here is the code in the UWP app:
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;
}
Web 服务器中有代码:
and there is the code in the web server:
[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";
}
注意:您可以找到如何制作一个简单的网络服务器 这里
但是:您必须安装网络服务器并授予用户访问权限.
Note: you can find how to make a simple web server here
But: you must install the web server and grant it's access for users.
这篇关于在 C# 中使用 user32.dll 的问题(错误 1008 尝试引用不存在的令牌.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!