windows service 调用 SetLocalTime 时提示出错

shizhu820228 2010-12-27 04:46:25
在控制台中使用如下代码:

[DllImport("Kernel32.dll", SetLastError = true)]
public static extern bool SetLocalTime(ref SystemTime sysTime);

代码正常调用

在Windows service 中使用出错。
parameter type 'ref TimeProtectService.Service1.SystemTime' is less accessible
than method 'TimeProtectService.Service1.SetLocalTime(ref TimeProtectService.Service1.SystemTime)'

在网上查了一下说,Windows service 中大部分API都与控制台或WINFORM 不同。

我想问,怎么能够正确调用该方法? 或 有什么方法可以代替如上方法?(方法主要是用来修改系统时间的。)
...全文
233 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
rabbitlzx 2010-12-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 shizhu820228 的回复:]
在Windows service 中使用出错。
parameter type 'ref TimeProtectService.Service1.SystemTime' is less accessible
than method 'TimeProtectService.Service1.SetLocalTime(ref TimeProtectService.Service1.SystemTime)'
[/Quote]

你仔细看下错误信息 就会知道了,他说参数类型SystemTime的可见性比方法SetLocalTime的可见性要低
说明你自己重新定义了SystemTime结构 并且可见性为private,这样当外部调用方法SetLocalTime时无法给第一个参数赋值,因为其为private的,对外不可见

Error Message
Inconsistent accessibility: parameter type 'type' is less accessible than method 'method'


The return type and each of the types referenced in the formal parameter list of a method must be at least as accessible as the method itself. Make sure the types used in method signatures are not accidentally private due to the omission of the public modifier. For more information, see Access Modifiers (C# Programming Guide).

rabbitlzx 2010-12-28
  • 打赏
  • 举报
回复
参数中的SystemTime 在哪里定义的?如果你在web service里重新定义了 是用下面的方式么



[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SystemTime
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}


shizhu820228 2010-12-28
  • 打赏
  • 举报
回复
谢谢7楼的兄弟,大感谢了哈,太神奇了,能否赐教一下原因。马上结贴
rabbitlzx 2010-12-28
  • 打赏
  • 举报
回复

把public 改成private呢
[DllImport("Kernel32.dll", SetLastError = true)]
public private static extern bool SetLocalTime(ref SystemTime sysTime);
zj514394212 2010-12-28
  • 打赏
  • 举报
回复
你的程序权限够吗?修改系统时间要一定的权限,你看下
shizhu820228 2010-12-28
  • 打赏
  • 举报
回复
独自等待,没有相关的问题贴吗?
shizhu820228 2010-12-28
  • 打赏
  • 举报
回复
谢谢楼上的。就是不想做成WINFORM程序,不然也不会有这个问题了。就是想开机后启动服务进行的。
xxiju203 2010-12-28
  • 打赏
  • 举报
回复
做成winForm程序.
shizhu820228 2010-12-28
  • 打赏
  • 举报
回复
高手都在呢,帮忙给说一下思路呗
shizhu820228 2010-12-27
  • 打赏
  • 举报
回复
懂的朋友帮下忙啊。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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