如何在程序里模拟在cmd里用管理员权限运行一条指令?

yangw150 2014-01-03 11:44:55
win7下,我想执行net user administrator /active:yes这样一条指令,用system()函数执行时返回错误,返回错误是“发生系统错误 5 拒绝访问”,原来是没有管理员权限,用手动右键点击cmd.exe,选择使用管理员权限运行就ok了,但在代码里如何能模拟管理员权限的cmd来运行一条指令呢?
...全文
6818 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
google搜索: shellexecuteex runas
zhuobattle 2014-01-03
  • 打赏
  • 举报
回复
runas /noprofile /user:mymachine\administrator cmd 用runas试下,具体使用如何看下帮助。
yangw150 2014-01-03
  • 打赏
  • 举报
回复
啊,,哈哈 搞定啦,你那个命令差一点点就成了,改成这样就好了 ShellExecute(0,"runas", LPCSTR("cmd.exe"),LPCSTR("/c net user administrator /active:yes"),"",SW_HIDE); 多谢各位的帮助。。。
赵4老师 2014-01-03
  • 打赏
  • 举报
回复
引用 5 楼 yangw150 的回复:
啊,大神,你来啦, ShellExecuteA(0,"runas","cmd /c net user administrator /active:yes","","",1); 这个命令不行哦,我试过了
生成一个临时文本文件tmp.bat,内容为
runas /noprofile /user:mymachine\administrator "cmd /c \"net user administrator /active:yes\""
FILE *f; f=fopen("tmp.bat","r"); fprintf(f,"runas /noprofile /user:mymachine\\administrator \"cmd /c \\\"net user administrator /active:yes\\\"\"\n"); fclose(f); WinExec("cmd /c tmp.bat",SW_HIDE); remove("tmp.bat"); 再试试看。
yangw150 2014-01-03
  • 打赏
  • 举报
回复
啊,大神,你来啦, ShellExecuteA(0,"runas","cmd /c net user administrator /active:yes","","",1); 这个命令不行哦,我试过了
赵4老师 2014-01-03
  • 打赏
  • 举报
回复
ShellExecuteA(0,"runas","cmd /c net user administrator /active:yes","","",1); 试试看。 CreateProcessAsUser The CreateProcessAsUser function creates a new process and its primary thread. The new process then executes a specified executable file. The CreateProcessAsUser function is similar to the CreateProcess function, except that the new process runs in the security context of the user represented by the hToken parameter. By default, the new process is non-interactive, that is, it runs on a desktop that is not visible and cannot receive user input. Also, by default, the new process inherits the environment of the calling process, rather than the environment associated with the specified user. BOOL CreateProcessAsUser( HANDLE hToken, // handle to a token representing the logged-on user LPCTSTR lpApplicationName, // pointer to name of executable module LPTSTR lpCommandLine, // pointer to command line string LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes BOOL bInheritHandles, // whether new process inherits handles DWORD dwCreationFlags, // creation flags LPVOID lpEnvironment, // pointer to new environment block LPCTSTR lpCurrentDirectory, // pointer to current directory name LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION );
yangw150 2014-01-03
  • 打赏
  • 举报
回复
ShellExecuteA(0,"runas","cmd","","",1); 这样是以管理员权限打开了cmd窗口,但怎么运行net user administrator /active:yes指令呢?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧