你好传奇程序员。
通过我的前一个问题,我尝试用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());
}
}如您所见,我从LockWorkStation()导入了user32.dll方法,并在按钮的事件侦听器中使用了它。Images是一个Dictionary<string,string>,每件事都是精细的,除非对方法的调用总是返回false,所以抛出的错误是1008,我在标题中提到过,问题是,为什么?和,我如何分配一个令牌?<代码>E 218
注:无论如何,以任何方式锁定屏幕,都是令人钦佩的。
发布于 2019-09-01 15:06:42
因此,在搜索了大量内容之后,由于无法从通用的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;
}web服务器中有代码:
[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";
}注意:您可以找到如何制作一个简单的web服务器这里
但是:您必须安装web服务器并为用户授予它的访问权限。
https://stackoverflow.com/questions/57737883
复制相似问题