I would like to send a user name and a password to another application, and process the input.
so I have Application A which has a window that requires a username and a password.
and we have Aplication B that is running. Application B needs to search for application A, login window, and send the user name to a textbox in it and the password, and then process those unputs through the Ok button.
Are there any libraries that can handle those sorts of requirements?
Any help weather it be website or dll references or examples would be great
NOTE:-
APPLICATION A is not something I built, or have access to its code or anything, I can start it, thats about it.
here is the process just to make things clear since some are confused:-
Application B is an EXE application, when clicked, it does some logic, then it starts Application A.
As soon as Application A starts, the user will prompted with a dialog box to enter user name, and password This is not something I made, it is what the application does. My question is can I access this dialog window, and send inputs to it.
FORM CODE
public partial class Form1 : Form {
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
public Form1() {
InitializeComponent();
var process = new ProcessStartInfo(#"arcmap.exe");
var pr=Process.Start(process);
SetForegroundWindow(pr.Handle);
SendKeys.Send("ne{TAB}ne{ENTER}");
}
}
}
This doesn't sound like a proper design strategy to me. Why not merging the two applications to one and passing the requested values between different application forms?
If for some reason you need to use two different applications, simply open application B AFTER the user has entered his login credentials in application A, and pass those values as parameters to your second app.
You can also consider using a TCP class in order to virtually connect the two of your apps using sockets.
You'll need to start Arcmap (this is a fairly easy task using Process.Start(string path);, then give it some time to boot using Thread.Sleep(int miliseconds), and then it gets tricky.
You'd have to get the Arcmap process (probably by name) and set it as foreground window by importing this method:
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
And then calling it this way:
Process process = Process.GetProcessesByName("arcmapProcessName").FirstOrDefault();
SetForegroundWindow(process);
And later you just send keys using
SendKeys.Send("login{TAB}password{ENTER}");
Then you would have to reference the SendInput function to programatically inject keyboard keypresses into the input stream. Perhaps, since it's a lot of hassle to reinvent the wheel and you're asking for an external library anyway, you could use C# Input Simulator.
Related
I have a winforms system that uses the same user and password to start windows session using the Active Directory credentials (This to not create a user and password for each system that is made).
What I want to do is that when the user for example closes his windows session or type the Ctrl + Alt + Del keys and select Lock or Switch User at the same time he closes the winforms system and returns the login the next time he logs in windows or unlock your session.
How can I do that?
The system is made in C # using VS2012 professional.
If you need more information, please let me know.
I hope you understand what I'm looking for.
EDITED
I have this code for Lock the session of windows, so I only look for when this event is launched in the current session and send call for closing my system or Form.
[DllImport("user32")]
public static extern void LockWorkStation();
LockWorkStation();
I want to be able to paste the results from my application to another application. Been Googling and all I can find is the ApplicationCommands.Paste. That will paste it into my application not another application.
Background: My application needs to interact with a very old application. This application is not being developed and DOES NOT have any API calls. Meaning any answers like "Link your application via DLLs" will only be excepted if it also includes a time machine :P So the solution is to simple paste results from my app to the old application.
For workflow reasons the client wants my Application to disappear and automatically paste the results into an open text box in (if it fails then manually pasting the results in).
Hiding the window is simple, but how do I find current active application and then paste it in? Is this even possible? Also note this application is NOT .NET (VB 6 if I am correct).
You can bring up the other app and send a key combination... something like:
[DllImport ("User32.dll")]
static extern int SetForegroundWindow(IntPtr hwnd);
public static void PasteToApplication(string appName)
{
var proc = Process.GetProcessesByName(appName).FirstOrDefault();
if(proc != null)
{
var handle = proc.MainWindowHandle;
SetForegroundWindow(handle);
SendKeys.SendWait("^v");
}
}
This should bring up the other app's window and send a ctrl-v command. With a bit of experimentation, you could find out the exact handle of the control you want to send the paste to, and set focus on it aswell
While looking for a way to change the user agent string for the webBrowser control, I found this nifty method:
[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;
public static void ChangeUserAgent(string Agent)
{
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0);
}
Basically, I needed a way to change the user agent until I want to change it again.
The usual:
webBrowser1.Navigate ("http://www.whatsmyuseragent.com", "_self" , null, "User-Agent: Luke's Web Browser");
Only works for one request.
However, I keep reading everywhere that the first method only works once per session. In my case, it works as many times as I want it to. So my guess is that this is related to the instance of Internet Explorer on the computer?
So my questions are:
What version does the end user need to have installed on their computer for this method to work as intended? IE. change as much as I want.
Since this is related to the Internet Explorer installed on the computer, does changing the user agent in my application effect the browser?
If the user has Internet Explorer open, will this method still work?
Thanks!
We use the "UrlMkSetSessionOption" function quite a bit. We have a "custom web browser shell" which is really just an IE user control embedded into a full screen WinForms program. We change the user agent to identify to our web server that this is our "custom" browser shell. But to answer your specific questions:
We've used this with both IE8 on XP and IE9 on Win7. I think it is version independent, but we always use the latest version.
As far as we can tell, changing this setting only affects IE running in the process that invoked the method. So if a user launches IE from the desktop, the user agent is unchanged. If you restart the program, the user agent is unchanged.
It works with and without standalone IE instances running. The user agent for those standalone instances remain unchanged.
I have an app with a manifest that requires running as administrator, but part of the app is to map a drive using WNetAddConnection2 which I believe requires it to be run in the normal user context due to credentials etc. Is there a way to execute this bit of code in the normal user context without creating a separate process.
EDIT
From the comments I have got this far but it doesnt work. I expected it not to as I dont really understand quite how I should use this. Perhaps it best if I open a new question?
class Program
{
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("advapi32.DLL")]
public static extern bool RevertToSelf();
static void Main(string[] args)
{
IntPtr phToken = IntPtr.Zero;
ImpersonateLoggedOnUser(phToken);
MapDrives();
RevertToSelf();
}
}
EDIT
If the current user has admin privileges then the main process is elevated with the manifest, in the code which is elevated I want to run a command in the users non-elevated space as this appears to have different environment variables etc. I believe once a thread is started it cant change itself, it needs to run a new one.
take a look on A small C# Class for impersonating a User code project article. It implements an IDisposable class (that releases the authentication token after its use). I've seen .NET code leaking due to not releasing the impersonation tokens.
You can impersonate a user only for a block of code that will access the network resource you need to access as a different user. Your code will look like
using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
/* code that executes under the new context */
...
}
I hope it helps.
First you need to obtain the user token that you want to start the app as, you can do this using WTSQueryUserToken. If the user is not yet logged on you can use LogonUser Win32 API to obtain a new one in a new session. To get all the sessions on your computer you can use WTSEnumerateSessions.
Then once you have the token you can use CreateProcessAsUser or else ImpersonateLoggedOnUser Win32 APIs.
Please make sure to call CloseHandle on the handles you obtain, they are especially bad leaks for this type of work.
Im not sure this is a way to do this without creating a new process, ImpersonateLoggedOnUser will only work from a service, and I dont want to provide credentials.
correct me if I am wrong
I have a console application written in C# that is scheduled to run every 15 minutes or so using the built-in Windows Task Scheduler.
Every time it runs, the black console box pops up for the duration of its execution and then closes. I am not writing anything to the console. Is there a way to make this run in the background?
Project > Properties> Application tab > change Output type to "Windows application".
No more console window.
Easy!
It seems hard to believe, but it works as a charm. I have used this for some setup projects, when you want to perform custom tasks with no signs of it.
Create the project as a Windows application project (this is the hard part).
Never make calls to any form. Just keep on in exactly as in your console application
class Program
{
static void Main(string[] args)
{
// Just don't call Application.Run(new frmMain(args));
// ... your code
}
}
This is because windows application projects are no really different than console, except because of the first form and references.
It is totally hidden execution. Try it!
You can use the Windows API to minimize the console box. Otherwise you can make it a Windows EXE file that does not actually load a form and call System.Windows.Forms.Application.Run().
Code to minimize the console:
[DllImport( "user32.dll" )]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public const int SW_SHOWMINIMIZED = 2;
IntPtr winHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(winHandle, SW_SHOWMINIMIZED);
What about implementing the app into a windows service? You can set the interval to 15 mins and run the operation in the timer_tick.
If you already have a Windows Console app created you can simply change the Output type of your app to Windows Application.
Under your project:
Go to Properties > Application
Select "Windows Application" as the Output type.
This would be the least impact and you can keep your Windows Task Scheduler running the same task.
If it doesn't write anything to the console you could make it a service.
http://msdn.microsoft.com/en-us/library/9k985bc9%28VS.80%29.aspx
It will only show up if it's scheduled to run as the same user that's currently logged in. Create another user on the machine with a ridiculously long password, set it to be an Administrator (only if needed) and schedule the task to run as that user.
This is easy. Set the task to run under an account that is not your login account.