Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert System.Diagnostics.ProcessStartInfo from dot net 3 to 1.1

Posted on 2008-10-14
7
Medium Priority
?
1,246 Views
Last Modified: 2013-12-17
I have written a console application in visual studio 2008. The problem is that the clients doesnt have dot net framework 3.0, they only have 1.1. The app I have written simply starts a program as another user. It was smooth and easy to write in vs 2008, but the ProcessStartInfo doesnt have the password and user properties in vs 2003. What is the easiest and best way to make the same result in vs 2003. If its possible, I dont want to use the runas and sanur chell-commands.
System.Diagnostics.ProcessStartInfo myProcess = new System.Diagnostics.ProcessStartInfo("appToStart.exe", "arguments");
System.Security.SecureString password = new System.Security.SecureString();
string uspw = psw;
foreach (char c in uspw)
{
    password.AppendChar(c);
}
password.MakeReadOnly();
 
myProcess.WorkingDirectory = workingDirectory;
myProcess.UserName = user;
myProcess.Password = password;
myProcess.Domain = domain;
myProcess.UseShellExecute = false;
                
System.Diagnostics.Process.Start(myProcess);

Open in new window

error.bmp
0
Comment
Question by:einarbrown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:William
ID: 22712267
Can you check if they have .net 2.0?
2.0 will give you what you need as well as 3~3.5.

Most everyone already has this. 3.0 and 3.5 are still unavail on most of my client machines as well.
0
 
LVL 11

Expert Comment

by:William
ID: 22712271
Just looked at you pic - DOH !!!
It says you need 2.0....  sorry
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22712675
under what account will your application run which you start by the following command
System.Diagnostics.Process.Start(myProcess);

the logged in user or some other user?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 22712732
if you need to run the process a different user account then you have to think about impersonation of other user account so that you can start you application
look at the following links as they are using impersonation for console applications
http://bytes.com/forum/thread269282.html
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.dotnet.security&tid=f2e490f4-22fb-4548-931f-107da454853c&cat=en_US_220f002b-dd2c-4152-bb1e-36a90df03f71&lang=en&cr=US&sloc=&p=1
0
 
LVL 6

Author Comment

by:einarbrown
ID: 22738165
Thanks for your help! I will try ragi0017 suggestion today. Ill be back!
0
 
LVL 6

Author Comment

by:einarbrown
ID: 22738872
Thanks ragi0017 to point me to the solotion. It wasnt that easy that I thought, byt now it works. Pasted the code bellow.

BillyDvD: Thanks for your being willing to help. I know, our organisation is way behind in windows client environment. They are very worried for new stuff. We got office 2002 and IE 6 :(
public class ConsoleApplicationCredentials
	{
				
		public ConsoleApplicationCredentials()
		{
			//
			// TODO: Add constructor logic here
			//
		}
 
		//importerade metoder
		[DllImport("advapi32.dll", SetLastError=true)]
		public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, 
			int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
 
		[DllImport("kernel32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
		private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, 
			int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr *Arguments);
 
		[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
		public extern static bool CloseHandle(IntPtr handle);
 
		[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
		public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, 
			int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
 
 
		// GetErrorMessage formats and returns an error message
		// corresponding to the input errorCode.
		public unsafe static string GetErrorMessage(int errorCode)
		{
			int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
			int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
			int FORMAT_MESSAGE_FROM_SYSTEM  = 0x00001000;
 
			//int errorCode = 0x5; //ERROR_ACCESS_DENIED
			//throw new System.ComponentModel.Win32Exception(errorCode);
 
			int messageSize = 255;
			String lpMsgBuf = "";
			int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
 
			IntPtr ptrlpSource = IntPtr.Zero;
			IntPtr prtArguments = IntPtr.Zero;
        
			int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, &prtArguments);
			if (0 == retVal)
			{
				throw new Exception("Failed to format message for error code " + errorCode + ". ");
			}
 
			return lpMsgBuf;
		}
 
	}
 
 
//----------------------------------------------------------------
class Class1
	{
		
	
		static void Main(string[] args)
		{
					ConsoleApplicationCredentials cAC = new ConsoleApplicationCredentials();
 
					const int LOGON32_PROVIDER_DEFAULT = 0;
					//This parameter causes LogonUser to create a primary token.
					const int LOGON32_LOGON_INTERACTIVE = 2;
					const int SecurityImpersonation = 2;
					IntPtr tokenHandle = new IntPtr(0);
					IntPtr dupeTokenHandle = new IntPtr(0);
					tokenHandle = IntPtr.Zero;
					dupeTokenHandle = IntPtr.Zero;
 
					// Call LogonUser to obtain a handle to an access token.
					bool returnValue = ConsoleApplicationCredentials.LogonUser(user, domain, psw, 
						LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
						ref tokenHandle);
 
					bool retVal = ConsoleApplicationCredentials.DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
					if (false == retVal)
					{
						ConsoleApplicationCredentials.CloseHandle(tokenHandle);
						Console.WriteLine("Exception thrown in trying to duplicate token.");        
						return;
					}
					// The token that is passed to the following constructor must 
					// be a primary token in order to use it for impersonation.
					WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
					WindowsImpersonationContext impersonatedUser = newId.Impersonate();
//---Now Im logged in as another user.---
					
ProcessStartInfo myProcess = new System.Diagnostics.ProcessStartInfo("appToStart.exe", "arguments");
					myProcess.UseShellExecute = false;
					System.Diagnostics.Process.Start(myProcess);
 
 
					// Stop impersonating the user.
					impersonatedUser.Undo();
            
					// Free the tokens.
					if (tokenHandle != IntPtr.Zero)
						ConsoleApplicationCredentials.CloseHandle(tokenHandle);
					if (dupeTokenHandle != IntPtr.Zero) 
						ConsoleApplicationCredentials.CloseHandle(dupeTokenHandle);
					
				}
			}
					}        
	}

Open in new window

0
 
LVL 6

Author Closing Comment

by:einarbrown
ID: 31505877
Thanks allot!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question