将PC从C ++关机到C# [英] ShutDown PC from c++ to c#

查看:89
本文介绍了将PC从C ++关机到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
以下c ++代码关闭了pc,任何人都可以将这些行转换为c#

Hi every one
the following c++ code shut down pc ,can any one convert those lines to 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#要求这些行(下一行有些错误),并且在转换2 c#时应位于上述先前的c ++代码之上.


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#

[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);

推荐答案

请参阅 ^ ]线程.一些答案包含 C ++ 代码的 C#版本.
您还可以在那里找到其他方法:使用 WMI ,使用 shotdown.exe 等:)
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. :)


这篇关于将PC从C ++关机到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆