Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I finally found <a href="http://www.codeproject.com/KB/system/alt_control_delete.aspx?fid=14112&amp;df=90&amp;mpp=25&amp;noise=3&amp;sort=Position&amp;view=Quick&amp;select=2634100&amp;fr=26" rel="nofollow">this C++ code</a> on CodeProject, which works well when launched as <strong>System user</strong>. Therefore, I converted the code into a dll, and called the function from my code.</p> <p>Here is the c++ code (you can use the <em>ErrorExit example function</em> that uses <code>GetLastError</code> from MSDN in case a problem occured):</p> <pre><code>#include "windows.h" #include &lt;strsafe.h&gt; __declspec(dllexport) BOOL SimulateAltControlDel() { HDESK hdeskCurrent; HDESK hdesk; HWINSTA hwinstaCurrent; HWINSTA hwinsta; // // Save the current Window station // hwinstaCurrent = GetProcessWindowStation(); if (hwinstaCurrent == NULL) return FALSE; // // Save the current desktop // hdeskCurrent = GetThreadDesktop(GetCurrentThreadId()); if (hdeskCurrent == NULL) return FALSE; // // Obtain a handle to WinSta0 - service must be running // in the LocalSystem account // hwinsta = OpenWindowStation("winsta0", FALSE, WINSTA_ACCESSCLIPBOARD | WINSTA_ACCESSGLOBALATOMS | WINSTA_CREATEDESKTOP | WINSTA_ENUMDESKTOPS | WINSTA_ENUMERATE | WINSTA_EXITWINDOWS | WINSTA_READATTRIBUTES | WINSTA_READSCREEN | WINSTA_WRITEATTRIBUTES); if (hwinsta == NULL) return FALSE; // // Set the windowstation to be winsta0 // if (!SetProcessWindowStation(hwinsta)) return FALSE; // // Get the default desktop on winsta0 // hdesk = OpenDesktop("Winlogon", 0, FALSE, DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD | DESKTOP_READOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_WRITEOBJECTS); if (hdesk == NULL) return FALSE; // // Set the desktop to be "default" // if (!SetThreadDesktop(hdesk)) return FALSE; PostMessage(HWND_BROADCAST,WM_HOTKEY,0,MAKELPARAM(MOD_ALT|MOD_CONTROL,VK_DELETE)); // // Reset the Window station and desktop // if (!SetProcessWindowStation(hwinstaCurrent)) return FALSE; if (!SetThreadDesktop(hdeskCurrent)) return FALSE; // // Close the windowstation and desktop handles // if (!CloseWindowStation(hwinsta)) return FALSE; if (!CloseDesktop(hdesk)) return FALSE; return TRUE; } </code></pre> <p>You also need to add a .def file to the project to export the function correctly (the project is named AltCtrlDelCpp) and tell the linker that the definition file of the module is this file</p> <pre><code>;altctrldel.def LIBRARY AltCtrlDelCpp ;CODE PRELOAD MOVEABLE DISCARDABLE ;DATA PRELOAD MOVEABLE EXPORTS SimulateAltControlDel </code></pre> <p>Then, in the .NET solution, you add the dll to the project, configures it so that it is always copied in the output directory, and you just use DllImport to import the function:</p> <p><strong>C# Code</strong> </p> <pre><code>[DllImport(@"AltCtrlDelCpp.dll")] static extern bool SimulateAltControlDel(); </code></pre> <p><strong>VB.NET Code</strong></p> <pre><code>&lt;DllImport("AltCtrlDelCpp.dll")&gt; _ Private Function SimulateAltControlDel() As Boolean </code></pre> <p>In VB.NET, you also need to add the attribute to the Sub Main :</p> <pre><code>&lt;MTAThread()&gt; _ Sub Main() </code></pre> <p>Then you just have to call the <code>SimulateAltControlDel</code> function and there you go. Please note that I had this work only for a <strong>Console Apps</strong>, it didn't work in winform apps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload