Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi every one
the following c++ code shut down pc ,can any one convert those lines to c#
C++
TOKEN_PRIVILEGES tkp;//windows.h
HANDLE hToken;//system.windows.Form
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);//advapi32.dll
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);//advapi32.dll
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);//advapi32
ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,0);//User32.dll

c# require those lines(some thing wrong in the following line)and should be above the above previous c++ code when it converted 2 c#

C#
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int operationFlag, int operationReason);

[DllImport("windowsx.dll")]
public static extern int AdjustTokenPrivileges(int hToken,bool FALSE,(PTOKEN_PRIVILEGES) tkp,DWORD ii,
PTOKEN_PRIVILEGES NULL, PDWORD jj);
Posted
Updated 21-May-10 21:48pm
v2
Comments
byrock55 22-May-10 6:40am    
Reason for my vote of 1
2

1 solution

See this[^] thread. Some of the answers contain the C# version of your C++ code.
You can also find different approaches there: using WMI, using shotdown.exe, etc. :)
 
Share this answer
 
Comments
khalidSabtan 22-May-10 11:33am    
works fine
thanks
Nuri Ismail 22-May-10 12:33pm    
You're welcome! :)
Johnny J. 24-May-10 9:05am    
If the answer solved your problem then you should mark it as correct answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900