求助求助!关于Windows服务

oo渣渣oo 2003-09-11 06:56:59
我自己写了一个Windows的服务,作用是到了指定的时间自动备份一些重要的文件到本地硬盘和FTP服务器上,完成后自动关机.

现在备份操作都能正常完成,就是最后关机的指令不执行,实在不知道原因,请各位大虾指点指点,谢了先~~
...全文
21 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
tohen 2003-09-13
  • 打赏
  • 举报
回复
private static void DoExitWin(int flg)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}
oo渣渣oo 2003-09-12
  • 打赏
  • 举报
回复
顶一下
oo渣渣oo 2003-09-12
  • 打赏
  • 举报
回复
回复人: ehuar(彭华) ( ) 信誉:100 2003-09-12 11:13:00 得分:0


我是调用Shell ("Shutdown.exe -s -f -t 0")进行关机的,在服务里这样不行吗?

可以的!


但是我有服务里就是这样写的啊!为什么没有执行呢?

-------------------------------------------------

回复人: chainet(准备创业) ( ) 信誉:100 2003-09-12 11:14:00 得分:0


Regsvcs.exe 工具手动注册服务组件


windows里没有这个程序呀!再说我是想在安装程序里自动注册服务!
chainet 2003-09-12
  • 打赏
  • 举报
回复
Regsvcs.exe 工具手动注册服务组件
ehuar 2003-09-12
  • 打赏
  • 举报
回复
我是调用Shell ("Shutdown.exe -s -f -t 0")进行关机的,在服务里这样不行吗?

可以的!
oo渣渣oo 2003-09-12
  • 打赏
  • 举报
回复
我是调用Shell ("Shutdown.exe -s -f -t 0")进行关机的,在服务里这样不行吗?
孟子E章 2003-09-12
  • 打赏
  • 举报
回复
try:

using System;
using System.Runtime.InteropServices;

class shoutdown{
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );

[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private static void DoExitWin(int flg)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}

public static void Main()
{
Console.WriteLine("Sto per spegnere il computer...");
//EWX_SHUTDOWN,EWX_LOGOFF, EWX_REBOOT,
DoExitWin(EWX_SHUTDOWN);
}
}


孟子E章 2003-09-12
  • 打赏
  • 举报
回复
rundll32.exe user.exe,exitwindows
oo渣渣oo 2003-09-12
  • 打赏
  • 举报
回复
偶再顶顶...
oo渣渣oo 2003-09-12
  • 打赏
  • 举报
回复
我顶,向版主求助!
Montaque 2003-09-12
  • 打赏
  • 举报
回复
调用一个At命令实验一下。
oo渣渣oo 2003-09-12
  • 打赏
  • 举报
回复
ddd
mainone 2003-09-12
  • 打赏
  • 举报
回复
up
lxanadu 2003-09-12
  • 打赏
  • 举报
回复
notice
oo渣渣oo 2003-09-12
  • 打赏
  • 举报
回复
还是没人理,偶再顶
CMIC 2003-09-11
  • 打赏
  • 举报
回复
gz
oo渣渣oo 2003-09-11
  • 打赏
  • 举报
回复
另:做成安装程序后如何自动注册服务?

即在安装完毕后自动添加到Windows的服务列表中?

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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