Problem Using user32.dll in C# (Error 1008 An attempt was made to reference a token that does not exist.)

余生长醉 提交于 2019-12-11 17:15:20

问题


Hello legendary coders.

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

here is my code:

[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());
    }
}

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.


回答1:


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.

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;
               }

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.



来源:https://stackoverflow.com/questions/57737883/problem-using-user32-dll-in-c-sharp-error-1008-an-attempt-was-made-to-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!