0

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 Answers1

0

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.