3
나는 C#에서 smaall 응용 프로그램을 작성했습니다. 이 앱이 내 로그인으로 워크 스테이션의 잠금을 해제하려면 잠그길 원합니다.잠긴 워크 스테이션에서 열린 응용 프로그램을 사용하여 Windows 7에 로그온
워크 스테이션을 잠그는 것은 매우 간단하며 잘 작동합니다.
문제는 워크 스테이션의 잠금을 해제하려고 할 때 시작됩니다.
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);
}
}
}
LogonUser 메서드를 사용하고 있지만 true 또는 false 결과 만 제공하고 실제로는 화면 잠금을 해제하지 않습니다.
Windows 7에서이 작업을 수행하는 방법은 무엇입니까?
앱의 강아지는 PC에 꽂혀있는 전자 키의 존재를 감지하고 워크 스테이션을 잠 그거나 잠금 해제하는 것입니다.
불가능합니다. Windows는이 기능을 지원하지 않습니다. 이는 의도적 인 것으로 잠금 해제는 사용자가 수행해야합니다 (Ctrl + Alt + Del 등). –