c#中如何调用CreateProcessWithLogonW函数

hunhunnowant 2008-05-27 03:43:58
查了msdn,是一个Advapi32.lib中的函数,怎么调用?
求个例子,谢谢
...全文
259 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hunhunnowant 2008-07-07
  • 打赏
  • 举报
回复
未处理 System.AccessViolationException
Message="尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
Source="WindowsApplication1"
StackTrace:
在 WindowsApplication1.Form1.CreateProcessWithLogonW(String lpUsername, String lpDomain, String lpPassword, Int32 dwLogonFlags, String lpApplicationName, String lpCommandLine, Int32 dwCreationFlags, IntPtr lpEnvironment, String lpCurrentDirectory, String lpStartupInfo, String lpProcessInfo)
在 WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) 位置 d:\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs:行号 232
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WindowsApplication1.Program.Main() 位置 d:\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Program.cs:行号 17
在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
hunhunnowant 2008-07-07
  • 打赏
  • 举报
回复
我用了CreateProcessWithLogonW函数,调用过程如下
CreateProcessWithLogonW(UserName, Domain, Password, LOGON_WITH_PROFILE, Url, null, CREATE_DEFAULT_ERROR_MODE, IntPtr.Zero, "c:\\", null, null);

运行时候会catch到错误“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”这是怎么回事?
IcyPlayer 2008-07-07
  • 打赏
  • 举报
回复
这是函数原型,你看看,和你写的有出入
CreateProcessWithLogonW(
Username, // user's name
Domain, // user's domain
PassWord, // user's password
LOGON_WITH_PROFILE, // logon option
Program, // executable module name
Parameter, // command-line string
CREATE_UNICODE_ENVIRONMENT, // creation flags
lpvEnv, // new environment block
szUserProfile, // current directory name
&si, // startup information
&pi // process information
);

c#下这样使用更好:
char[] keys = password.ToCharArray();
SecureString secureString;
fixed (char* pass = keys){
secureString = new SecureString(pass, 9);
}
Process process = new Process();
process.StartInfo.FileName = @"c:\\epm-dm.exe";
process.StartInfo.UserName = "administrator";
process.StartInfo.Password = secureString;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
hunhunnowant 2008-07-07
  • 打赏
  • 举报
回复
不行
zlkingdom 2008-07-07
  • 打赏
  • 举报
回复
你的C盘是不是系统盘?换一个盘符试下
hunhunnowant 2008-07-07
  • 打赏
  • 举报
回复
有人帮帮忙吗?

110,561

社区成员

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

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

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