473,831 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Start process as another user?

How can I start a process as another user?

I want my starter app to install framework 2.0 if its not installed, admin
rights is needed for it.
So can I start it as the domain/administrator somehow?

Regards
Fredrik
Nov 21 '05 #1
5 3910
Found some code on the net to do this.

"Fredrik Melin" <me*@n.o.spam.d acsa.net.remove .as.needed> wrote in message
news:u0******** *****@TK2MSFTNG P10.phx.gbl...
How can I start a process as another user?

I want my starter app to install framework 2.0 if its not installed, admin
rights is needed for it.
So can I start it as the domain/administrator somehow?

Regards
Fredrik

Nov 21 '05 #2
would you mind sharing that code?

Fredrik Melin wrote:
Found some code on the net to do this.

"Fredrik Melin" <me*@n.o.spam.d acsa.net.remove .as.needed> wrote in message
news:u0******** *****@TK2MSFTNG P10.phx.gbl...
How can I start a process as another user?

I want my starter app to install framework 2.0 if its not installed, admin
rights is needed for it.
So can I start it as the domain/administrator somehow?

Regards
Fredrik


Nov 21 '05 #3
Sure

Imports System.Runtime. InteropServices

Public Class ProcInfo
#Region "Structs"
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure PROCESS_INFORMA TION
Dim hProcess As System.IntPtr
Dim hThread As System.IntPtr
Dim dwProcessId As Integer
Dim dwThreadId As Integer
End Structure
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure STARTUPINFO
Dim cb As Integer
Dim lpReserved As System.IntPtr
Dim lpDesktop As System.IntPtr
Dim lpTitle As System.IntPtr
Dim dwX As Integer
Dim dwY As Integer
Dim dwXSize As Integer
Dim dwYSize As Integer
Dim dwXCountChars As Integer
Dim dwYCountChars As Integer
Dim dwFillAttribute As Integer
Dim dwFlags As Integer
Dim wShowWindow As Short
Dim cbReserved2 As Short
' you had this as a byte, but it is LPBYTE or byte*
' so should be an IntPtr
Dim lpReserved2 As System.IntPtr
Dim hStdInput As System.IntPtr
Dim hStdOutput As System.IntPtr
Dim hStdError As System.IntPtr
End Structure
#End Region
#Region "APIINFO"
Private Const LOGON_NETCREDEN TIALS_ONLY As Integer = &H2
Private Const NORMAL_PRIORITY _CLASS As Integer = &H20
Private Const CREATE_DEFAULT_ ERROR_MODE As Integer = &H4000000
Private Const CREATE_NEW_CONS OLE As Integer = &H10
Private Const CREATE_NEW_PROC ESS_GROUP As Integer = &H200
Private Const LOGON_WITH_PROF ILE As Integer = &H1
Private Declare Unicode Function CreateProcessWi thLogon Lib "Advapi32"
Alias "CreateProcessW ithLogonW" _
(ByVal lpUsername As String, _
ByVal lpDomain As String, _
ByVal lpPassword As String, _
ByVal dwLogonFlags As Integer, _
ByVal lpApplicationNa me As String, _
ByVal lpCommandLine As String, _
ByVal dwCreationFlags As Integer, _
ByVal lpEnvironment As System.IntPtr, _
ByVal lpCurrentDirect ory As System.IntPtr, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInfo As PROCESS_INFORMA TION) As Integer
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
System.IntPtr) As Integer
#End Region

Public Function StartProcess(By Val filename As String, ByVal parameter
As String, ByVal username As String, ByVal password As String, ByVal domain
As String) As Boolean
Dim szApp As String = filename
Dim szCmdLine As String = parameter
Dim szUser As String = username
Dim szPass As String = password
Dim szDomain As String = domain
Dim siStartup As STARTUPINFO
Dim piProcess As PROCESS_INFORMA TION
siStartup.cb = Marshal.SizeOf( siStartup)
siStartup.dwFla gs = 0
Dim ret As Integer = CreateProcessWi thLogon(szUser, szDomain,
szPass, LOGON_WITH_PROF ILE, szApp, szCmdLine, _
NORMAL_PRIORITY _CLASS Or CREATE_DEFAULT_ ERROR_MODE Or
CREATE_NEW_CONS OLE Or CREATE_NEW_PROC ESS_GROUP, _
IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)
If ret = 0 Then
MessageBox.Show (New
System.Componen tModel.Win32Exc eption(Marshal. GetLastWin32Err or()).Message)
End If
CloseHandle(piP rocess.hProcess )
CloseHandle(piP rocess.hThread)
End Function
End Class
"Tamas Nyilanszky" <sn************ ****@gmail.com> wrote in message
news:OL******** ******@TK2MSFTN GP12.phx.gbl...
would you mind sharing that code?

Fredrik Melin wrote:
Found some code on the net to do this.

"Fredrik Melin" <me*@n.o.spam.d acsa.net.remove .as.needed> wrote in
message news:u0******** *****@TK2MSFTNG P10.phx.gbl...
How can I start a process as another user?

I want my starter app to install framework 2.0 if its not installed,
admin rights is needed for it.
So can I start it as the domain/administrator somehow?

Regards
Fredrik


Nov 21 '05 #4
Note, this code is for VB.NET 2003, in 2005 there is easier ways to do it.

"Fredrik Melin" <me*@n.o.spam.d acsa.net.remove .as.needed> wrote in message
news:OF******** ********@TK2MSF TNGP15.phx.gbl. ..
Sure

Imports System.Runtime. InteropServices

Public Class ProcInfo
#Region "Structs"
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure PROCESS_INFORMA TION
Dim hProcess As System.IntPtr
Dim hThread As System.IntPtr
Dim dwProcessId As Integer
Dim dwThreadId As Integer
End Structure
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure STARTUPINFO
Dim cb As Integer
Dim lpReserved As System.IntPtr
Dim lpDesktop As System.IntPtr
Dim lpTitle As System.IntPtr
Dim dwX As Integer
Dim dwY As Integer
Dim dwXSize As Integer
Dim dwYSize As Integer
Dim dwXCountChars As Integer
Dim dwYCountChars As Integer
Dim dwFillAttribute As Integer
Dim dwFlags As Integer
Dim wShowWindow As Short
Dim cbReserved2 As Short
' you had this as a byte, but it is LPBYTE or byte*
' so should be an IntPtr
Dim lpReserved2 As System.IntPtr
Dim hStdInput As System.IntPtr
Dim hStdOutput As System.IntPtr
Dim hStdError As System.IntPtr
End Structure
#End Region
#Region "APIINFO"
Private Const LOGON_NETCREDEN TIALS_ONLY As Integer = &H2
Private Const NORMAL_PRIORITY _CLASS As Integer = &H20
Private Const CREATE_DEFAULT_ ERROR_MODE As Integer = &H4000000
Private Const CREATE_NEW_CONS OLE As Integer = &H10
Private Const CREATE_NEW_PROC ESS_GROUP As Integer = &H200
Private Const LOGON_WITH_PROF ILE As Integer = &H1
Private Declare Unicode Function CreateProcessWi thLogon Lib "Advapi32"
Alias "CreateProcessW ithLogonW" _
(ByVal lpUsername As String, _
ByVal lpDomain As String, _
ByVal lpPassword As String, _
ByVal dwLogonFlags As Integer, _
ByVal lpApplicationNa me As String, _
ByVal lpCommandLine As String, _
ByVal dwCreationFlags As Integer, _
ByVal lpEnvironment As System.IntPtr, _
ByVal lpCurrentDirect ory As System.IntPtr, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInfo As PROCESS_INFORMA TION) As Integer
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
System.IntPtr) As Integer
#End Region

Public Function StartProcess(By Val filename As String, ByVal parameter
As String, ByVal username As String, ByVal password As String, ByVal
domain As String) As Boolean
Dim szApp As String = filename
Dim szCmdLine As String = parameter
Dim szUser As String = username
Dim szPass As String = password
Dim szDomain As String = domain
Dim siStartup As STARTUPINFO
Dim piProcess As PROCESS_INFORMA TION
siStartup.cb = Marshal.SizeOf( siStartup)
siStartup.dwFla gs = 0
Dim ret As Integer = CreateProcessWi thLogon(szUser, szDomain,
szPass, LOGON_WITH_PROF ILE, szApp, szCmdLine, _
NORMAL_PRIORITY _CLASS Or CREATE_DEFAULT_ ERROR_MODE Or
CREATE_NEW_CONS OLE Or CREATE_NEW_PROC ESS_GROUP, _
IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)
If ret = 0 Then
MessageBox.Show (New
System.Componen tModel.Win32Exc eption(Marshal. GetLastWin32Err or()).Message)
End If
CloseHandle(piP rocess.hProcess )
CloseHandle(piP rocess.hThread)
End Function
End Class
"Tamas Nyilanszky" <sn************ ****@gmail.com> wrote in message
news:OL******** ******@TK2MSFTN GP12.phx.gbl...
would you mind sharing that code?

Fredrik Melin wrote:
Found some code on the net to do this.

"Fredrik Melin" <me*@n.o.spam.d acsa.net.remove .as.needed> wrote in
message news:u0******** *****@TK2MSFTNG P10.phx.gbl...
How can I start a process as another user?

I want my starter app to install framework 2.0 if its not installed,
admin rights is needed for it.
So can I start it as the domain/administrator somehow?

Regards
Fredrik

Nov 21 '05 #5
thanks appreciate it :)
Fredrik Melin wrote:
Sure

Imports System.Runtime. InteropServices

Public Class ProcInfo
#Region "Structs"
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure PROCESS_INFORMA TION
Dim hProcess As System.IntPtr
Dim hThread As System.IntPtr
Dim dwProcessId As Integer
Dim dwThreadId As Integer
End Structure
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure STARTUPINFO
Dim cb As Integer
Dim lpReserved As System.IntPtr
Dim lpDesktop As System.IntPtr
Dim lpTitle As System.IntPtr
Dim dwX As Integer
Dim dwY As Integer
Dim dwXSize As Integer
Dim dwYSize As Integer
Dim dwXCountChars As Integer
Dim dwYCountChars As Integer
Dim dwFillAttribute As Integer
Dim dwFlags As Integer
Dim wShowWindow As Short
Dim cbReserved2 As Short
' you had this as a byte, but it is LPBYTE or byte*
' so should be an IntPtr
Dim lpReserved2 As System.IntPtr
Dim hStdInput As System.IntPtr
Dim hStdOutput As System.IntPtr
Dim hStdError As System.IntPtr
End Structure
#End Region
#Region "APIINFO"
Private Const LOGON_NETCREDEN TIALS_ONLY As Integer = &H2
Private Const NORMAL_PRIORITY _CLASS As Integer = &H20
Private Const CREATE_DEFAULT_ ERROR_MODE As Integer = &H4000000
Private Const CREATE_NEW_CONS OLE As Integer = &H10
Private Const CREATE_NEW_PROC ESS_GROUP As Integer = &H200
Private Const LOGON_WITH_PROF ILE As Integer = &H1
Private Declare Unicode Function CreateProcessWi thLogon Lib "Advapi32"
Alias "CreateProcessW ithLogonW" _
(ByVal lpUsername As String, _
ByVal lpDomain As String, _
ByVal lpPassword As String, _
ByVal dwLogonFlags As Integer, _
ByVal lpApplicationNa me As String, _
ByVal lpCommandLine As String, _
ByVal dwCreationFlags As Integer, _
ByVal lpEnvironment As System.IntPtr, _
ByVal lpCurrentDirect ory As System.IntPtr, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInfo As PROCESS_INFORMA TION) As Integer
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
System.IntPtr) As Integer
#End Region

Public Function StartProcess(By Val filename As String, ByVal parameter
As String, ByVal username As String, ByVal password As String, ByVal domain
As String) As Boolean
Dim szApp As String = filename
Dim szCmdLine As String = parameter
Dim szUser As String = username
Dim szPass As String = password
Dim szDomain As String = domain
Dim siStartup As STARTUPINFO
Dim piProcess As PROCESS_INFORMA TION
siStartup.cb = Marshal.SizeOf( siStartup)
siStartup.dwFla gs = 0
Dim ret As Integer = CreateProcessWi thLogon(szUser, szDomain,
szPass, LOGON_WITH_PROF ILE, szApp, szCmdLine, _
NORMAL_PRIORITY _CLASS Or CREATE_DEFAULT_ ERROR_MODE Or
CREATE_NEW_CONS OLE Or CREATE_NEW_PROC ESS_GROUP, _
IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)
If ret = 0 Then
MessageBox.Show (New
System.Componen tModel.Win32Exc eption(Marshal. GetLastWin32Err or()).Message)
End If
CloseHandle(piP rocess.hProcess )
CloseHandle(piP rocess.hThread)
End Function
End Class
"Tamas Nyilanszky" <sn************ ****@gmail.com> wrote in message
news:OL******** ******@TK2MSFTN GP12.phx.gbl...
would you mind sharing that code?

Fredrik Melin wrote:
Found some code on the net to do this.

"Fredrik Melin" <me*@n.o.spam.d acsa.net.remove .as.needed> wrote in
message news:u0******** *****@TK2MSFTNG P10.phx.gbl...
How can I start a process as another user?

I want my starter app to install framework 2.0 if its not installed,
admin rights is needed for it.
So can I start it as the domain/administrator somehow?

Regards
Fredrik

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5844
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to Mandrake 9.2 and am reinstalling everything. This thing is kicking my ass and I can't seem to get past it. I could really use some help if anybody has any. Just for reference, this was my primary source for information on installation and...
1
2050
by: Marc | last post by:
I want to write a C#/ASP.NET application where a user can go to a web page, start running a job, close their browser, and then come back later and see the results. The purpose for this application is for a user to be able to use a page on our intranet to start running a time-consuming reporting and analysis job. I'm not sure how to start a process in ASP.NET and then "detach" it, or how to check that it's still running later. I think that maybe...
6
6852
by: Dmitri Shvetsov | last post by:
Hi, Can I start an external process from the Web Service? I'm using a code, compiler keeps silence, compiles ok and starts the project. When I trace in Debugger it doesn't start an external process. That's strange for me. I understand that it should be a new shell, but why I can't start it? Is it need to have a Windows application to start an external process? I created a very long batch files and a complicated script to work with...
12
4106
by: GTi | last post by:
I have small program that always must be running when a user is logged on. Since users can close this program I must create a program that always check if this program is running. So I created a program in C# (my first!!) and it works with one major problem. This program detects when the desktop program is not running and starts it again. All works as expected BUT The desktop program is running as a SYSTEM user. That is not what I...
4
12380
by: Paul | last post by:
Hi, I am trying to start a process hidden. My code: wordprocess = new System.Diagnostics.Process(); ; wordprocess.StartInfo = new System.Diagnostics.ProcessStartInfo(wcmd, args); wordprocess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; wordprocess.StartInfo.WorkingDirectory = TemplatePath.Substring(0, TemplatePath.Length - 1);
6
6497
by: uuyytt | last post by:
I have a Windows service from which I want to start Internet Explorer with a particular URL as an argument. I don't know why my code (shown below) doesn't work. I know that IExplore.exe starts as I can see it in my task manager, but it is not visible. Can anyone explain what is happening? My code: Process p = Process.Start("IExplore.exe", m_strURL);
4
2502
by: Jamey Shuemaker | last post by:
A2k2 with user-level security and all preventive measures, vis a vis, Security FAQ enabled or enacted. I've got three DBs, which due to size constraints can't, or rather, probably shouldn't be combined. All have front-end on C: drive, back-end in same folder on network share. All are secured with the same ..mdw. I'd like to make a master file that gives the user one single jumping off point to link to the other apps. I can accomplish...
3
11247
by: SugarDaddy | last post by:
Here's my problem. I have an NT service (really a .NET service) running as local system. I have a .NET form running on the user account. The form and the service communicate via an IPC Channel so the form can control the service and do various things. Both applications share the same set of dlls. When performing an update of some of the dlls, both the service and the form must be shut down (that's just how it's implemented -- didn't
2
4930
by: TonyJ | last post by:
Hello!! Below I have written the main method, the C-tor and the OnStart. Who is calling OnStart? It must be the operating system that calls it after the C-tor has finished 1. Main is always started first 2. The C-tor is then called 3. The operating system is then calling OnStart
2
12203
by: Kerem Gümrükcü | last post by:
Hi, i have a Process.Start call that fails with 267 Error Code if invoked with another user and password other than the applications user. It is a ordinary piece of code just create a Process Object , fill its StartInfo and then go a process.Start(), It works fine, unless i try to execute the process with another user. Why, what do i have to take care of,...i gt the 267 Win32Exception Code with the Exception thrown,...
0
10777
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7748
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5619
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.