|
Lame attempt at self promotion. Based on the votes so far I doubt anyone will even see this "article"
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Hi all,
I want to execute an executable file under particular user account in my C# application.
can any body tell me how to do the same?
Thanking You,
Sunil G.
|
|
|
|
|
you could check the "runas" command
specify the executable and particular user as arguments.
|
|
|
|
|
using Process.Start [^], it supports launching a process under a particular user;
MSDN states:
Use this overload to create a new process and its primary thread by specifying its file name, command-line arguments, user name, password, and domain. The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).
I are Troll
|
|
|
|
|
I want to execute using current logged on user token.
|
|
|
|
|
Then you'd only need to start it, without giving credentials at all - and the process will be started under the credentials of the current (signed in) user
I are Troll
|
|
|
|
|
You have got an eMail. (I hope ).
Hope that helps you.
Greetings
Covean
|
|
|
|
|
Yeh i got ur mail.
Thank u very much.
|
|
|
|
|
questions should be answered within the thread, and not by e-mail.
That way anyone can benefit.
|
|
|
|
|
Yes there you are right but I send him more than 300 lines of code.
I think this is tooo long for this forum.
But here a short version (with out DLLImports, structs, ...):
public static void StartProcessOnActiveConsole(string szApplication, string szArguments, string szDirectory)
{
IntPtr hUserToken = IntPtr.Zero;
IntPtr hDuplicatedUserToken = IntPtr.Zero;
IntPtr lpEnvironment = IntPtr.Zero;
IntPtr hProcess = IntPtr.Zero;
IntPtr hThread = IntPtr.Zero;
try
{
uint dwSessionId = 0xFFFFFFFF;
dwSessionId = WTSGetActiveConsoleSessionId();
if (dwSessionId == 0xFFFFFFFF)
return;
if(!WTSQueryUserToken(dwSessionId, ref hUserToken))
throw new Exception("WTSQueryUserToken failed. GetLastError() = " + Marshal.GetLastWin32Error());
if(!DuplicateTokenEx(hUserToken, 0x02000000 , IntPtr.Zero, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenPrimary, ref hDuplicatedUserToken))
throw new Exception("DuplicateTokenEx failed. GetLastError() = " + Marshal.GetLastWin32Error());
if(!CreateEnvironmentBlock(ref lpEnvironment, hDuplicatedUserToken, false))
throw new Exception("CreateEnvironmentBlock failed. GetLastError() = " + Marshal.GetLastWin32Error());
STARTUPINFO startupInfo = new STARTUPINFO();
startupInfo.dwSize = (uint)Marshal.SizeOf(startupInfo);
startupInfo.pReserved = IntPtr.Zero;
startupInfo.szDesktop = "winsta0\\default";
startupInfo.pTitle = IntPtr.Zero;
startupInfo.dwX = 0;
startupInfo.dwY = 0;
startupInfo.dwXSize = 0;
startupInfo.dwYSize = 0;
startupInfo.dwXCountChars = 0;
startupInfo.dwYCountChars = 0;
startupInfo.dwFillAttribute = 0;
startupInfo.dwFlags = 128 ;
startupInfo.wShowWindow = 0;
startupInfo.wReserved2 = 0;
startupInfo.lpReserved2 = IntPtr.Zero;
startupInfo.hStdInput = IntPtr.Zero;
startupInfo.hStdOutput = IntPtr.Zero;
startupInfo.hStdError = IntPtr.Zero;
PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION();
StringBuilder szCommandLineBuilder = new StringBuilder(32768);
szCommandLineBuilder.Append("\"" + szApplication + "\" " + (string)((szArguments == null) ? string.Empty : szArguments));
string szDir = ((szDirectory == null) || (szDirectory == string.Empty)) ? null : szDirectory;
if(!CreateProcessAsUser(hDuplicatedUserToken, null, szCommandLineBuilder, IntPtr.Zero, IntPtr.Zero, false, 0x00000400 , lpEnvironment, szDir, startupInfo, ref processInformation))
throw new Exception("CreateProcessAsUser failed. GetLastError() = " + Marshal.GetLastWin32Error());
hProcess = processInformation.hProcess;
hThread = processInformation.hThread;
}
finally
{
}
}
Greetings
Covean
|
|
|
|
|
Hi!!
1) I want to develop a program to hook on winspool.drv and be noticed whenever StartPagePrinter and EndPagePrinter functions are called. 've searched extensively having no luck. Wonder if you could help me by providing some sample codes or tutorials because I'm not familiarized with hooking apis.
2)The final goal of my program is to append some information on the printed pages sent by any application, is that possible?
Thank you!
|
|
|
|
|
I need to reference a combobox value (selectedvalue) on another form to the current form. How can I do this please?
I tried setting the combobox modifier to 'Public' and while in NewForm using MainForm.myComboBox.selectedValue without success.
|
|
|
|
|
Its bad practise to make such properties public as it lessens the object orientation of the control containing the combo box and starts going down the route of coupling the 2nd form to the 1st.
The thing to do is to raise an event in the first form (the one containing the combo box) that the second form subscibes to. The event should probably (depending upon your requirements) be raised when the combo box seleceted value changes.
Another way to solver you problem is to implement a model-view-viewmodel pattern, but this requires much more work if you have exisiting code.
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|
Many thanks for that. I shall proceed accordingly.
|
|
|
|
|
Hi guys..
I have developed a console application that migrates data present in xml file to my database.. my console application asks the app name from the user and based on the name entered by the user the application migrates only those records corresponding to that app name.. Now my question is after the migration is done my console application exits.. but i dont want it to exit, instead it must ask the user whether to exit if the user enters "exit" then it must exit otherwise the console application should repeat its migration process from the beginning and ask the user the app name and so on it goes... how do i implement this logic???
Thanx in advance..
Regards,
Tash
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:54am.
|
|
|
|
|
Thanx it worked fine...
Regards,
Tash
|
|
|
|
|
Hey there Thashif
I'd use a while loop for that
Here's an example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
bool QuitProgram = false;
while (!QuitProgram)
{
Console.WriteLine("Please type your name or \"quit\" to exit");
string Response = Console.ReadLine();
if (Response.ToLower() == "quit")
{
QuitProgram = true;
}
else
{
DoWork(Response);
}
}
}
public static void DoWork(string Response)
{
Console.WriteLine("Hello {0}", Response);
}
}
}
Hope this helps
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Thanx alot.. ur code helped me alot... Million thanx
Regards,
Tash
|
|
|
|
|
Your welcome
PS: Please don't forget to mark the post good answer
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
I have win32 handle to some active window.
I need to simulate click on some button that exist on the window.
How can i do it ?
I know only the button text.
|
|
|
|
|
Use Spy++ (installed with Visual Studio) to find the window handle for the button, and then use InteropServices to post a click message with the hwnd of the button.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
No can do -
I need to simulate it in run time
|
|
|
|
|
Yanshof wrote: simulate it in run time
That's what John's method allows you to do.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
Spy++ gets you the hwnd.
You use the hwnd in your code.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|