Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4053900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:30:22+00:00 2026-05-20T14:30:22+00:00

I’ve written a smaall app in c#. I want this app to lock on

  • 0

I’ve written a smaall app in c#. I want this app to lock on unlock a workstation on my own login.

Locking the workstation is quite simple and it works fine.

The problem starts when i want to unlock the workstation.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Security.Principal;
using System.Security.Permissions;
using System.Drawing;


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    [DllImport("User32.dll")]
    public static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("User32.dll")]
    public static extern void ReleaseDC(IntPtr dc);

    IntPtr desktopDC;
    Graphics g;

    public static class Logon
    {
        [DllImport("User32.Dll", EntryPoint = "LockWorkStation"), Description("Locks the workstation's display. Locking a workstation protects it from unauthorized use.")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool LockWorkStation();

        /// <exception cref="Win32Exception">if the lock fails more information can be found in this Exception class</exception>
        public static void LockWorkstation()
        {
            if (!LockWorkStation())
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        [DllImport("advapi32.dll", EntryPoint = "LogonUser")]
        public static extern bool LogonUser(

        string lpszUsername,

        string lpszDomain,

        string lpszPassword,

        int dwLogonType,

        int dwLogonProvider,

        ref IntPtr phToken);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Logon.LockWorkstation();
        timer1.Start();   
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        Login();
    }

    private void Login()
    {
        string sUsername = "adam";
        string sDomain = System.Environment.MachineName;
        string sPassword = "sernik";

        const int LOGON32_PROVIDER_DEFAULT = 0;
        // create token

        const int LOGON32_LOGON_INTERACTIVE = 2;
        //const int SecurityImpersonation = 2;


        IntPtr pExistingTokenHandle = new IntPtr(0);
        IntPtr pDuplicateTokenHandle = new IntPtr(0);
        pExistingTokenHandle = IntPtr.Zero;
        pDuplicateTokenHandle = IntPtr.Zero;


        bool a = Logon.LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);

        g.DrawString(a.ToString(), new Font(FontFamily.GenericSansSerif, 80), Brushes.Red, 100, 100);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Login();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        desktopDC = GetDC(IntPtr.Zero);

        g = Graphics.FromHdc(desktopDC);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        g.Dispose();
        ReleaseDC(desktopDC);
    }
}
}

I was trying the LogonUser method but it only gives my true or false result and don’t acually unlocks the screen.

How to do this under windows 7?

The puprose of the app is to detect presence of an electronic key plugged into PC and lock or unlock workstation then.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:30:22+00:00Added an answer on May 20, 2026 at 2:30 pm

    The puprose of the app is to detect presence of an electronic key plugged into PC and lock or unlock workstation then.

    Prior to Windows Vista you could do something like this with GINA, since you mention Windows 7 you’ll have to use the new infrastructure. Either way, you can’t acheive this sort of behavior by simply running a program on the machine in question, you need to tie into the authentication systems of Windows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.