sendkeys to open the command window

G

Guest

public void sendKeysTest()
{

Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("{ENTER}");
else
myProcess.Kill();

}
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

I have this code in my windowservice. Option to interact with desktop in
window service is also checked. When I click a button on the front end.
Inside the button click event, I call this method sendKeysTest, and
sendKeysTest is defined inside the window service. This program runs fine and
I can see cmd in my task manager that it is started, but I don't see the cmd
window. Is there any way I can see the cmd window. This code runs fine when I
run this code in windows form.

private void Button1_Click(object sender, System.EventArgs e)
{
try
{

runner.sendKeysTest();
}
catch(Exception ex)
{
string x = ex.Message;
}

}

Thanks,

Any help will be appreciated.
 
N

Nicholas Paldino [.NET/C# MVP]

Vinki,

Why are you trying to interact with the desktop from a service? What
exactly do you need to do? You shouldn't be interacting with the desktop
from a service, because you can have multiple desktops running on a box at
any one time (due to terminal services) or none at all (if no one is logged
in interactively).
 
G

Guest

I will uncheck the interacting with desktop option. All I am trying to do is
from my windows application. I am trying to call a method that exists in the
window service. That method needs to start a process and use sendkey to start
the process.

Nicholas Paldino said:
Vinki,

Why are you trying to interact with the desktop from a service? What
exactly do you need to do? You shouldn't be interacting with the desktop
from a service, because you can have multiple desktops running on a box at
any one time (due to terminal services) or none at all (if no one is logged
in interactively).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
public void sendKeysTest()
{

Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("{ENTER}");
else
myProcess.Kill();

}
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

I have this code in my windowservice. Option to interact with desktop in
window service is also checked. When I click a button on the front end.
Inside the button click event, I call this method sendKeysTest, and
sendKeysTest is defined inside the window service. This program runs fine
and
I can see cmd in my task manager that it is started, but I don't see the
cmd
window. Is there any way I can see the cmd window. This code runs fine
when I
run this code in windows form.

private void Button1_Click(object sender, System.EventArgs e)
{
try
{

runner.sendKeysTest();
}
catch(Exception ex)
{
string x = ex.Message;
}

}

Thanks,

Any help will be appreciated.
 
N

Nicholas Paldino [.NET/C# MVP]

Vinki,

If you want to have an application interact with a service, you should
use remoting or WCF to make calls into your service. You can't access the
service directly to make calls.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
I will uncheck the interacting with desktop option. All I am trying to do
is
from my windows application. I am trying to call a method that exists in
the
window service. That method needs to start a process and use sendkey to
start
the process.

Nicholas Paldino said:
Vinki,

Why are you trying to interact with the desktop from a service? What
exactly do you need to do? You shouldn't be interacting with the desktop
from a service, because you can have multiple desktops running on a box
at
any one time (due to terminal services) or none at all (if no one is
logged
in interactively).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
public void sendKeysTest()
{

Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("{ENTER}");
else
myProcess.Kill();

}
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

I have this code in my windowservice. Option to interact with desktop
in
window service is also checked. When I click a button on the front end.
Inside the button click event, I call this method sendKeysTest, and
sendKeysTest is defined inside the window service. This program runs
fine
and
I can see cmd in my task manager that it is started, but I don't see
the
cmd
window. Is there any way I can see the cmd window. This code runs fine
when I
run this code in windows form.

private void Button1_Click(object sender, System.EventArgs e)
{
try
{

runner.sendKeysTest();
}
catch(Exception ex)
{
string x = ex.Message;
}

}

Thanks,

Any help will be appreciated.
 
G

Guest

I am using remoting. I calling that sendKeyTest method using .net remoting.

Nicholas Paldino said:
Vinki,

If you want to have an application interact with a service, you should
use remoting or WCF to make calls into your service. You can't access the
service directly to make calls.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
I will uncheck the interacting with desktop option. All I am trying to do
is
from my windows application. I am trying to call a method that exists in
the
window service. That method needs to start a process and use sendkey to
start
the process.

Nicholas Paldino said:
Vinki,

Why are you trying to interact with the desktop from a service? What
exactly do you need to do? You shouldn't be interacting with the desktop
from a service, because you can have multiple desktops running on a box
at
any one time (due to terminal services) or none at all (if no one is
logged
in interactively).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

public void sendKeysTest()
{

Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("{ENTER}");
else
myProcess.Kill();

}
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

I have this code in my windowservice. Option to interact with desktop
in
window service is also checked. When I click a button on the front end.
Inside the button click event, I call this method sendKeysTest, and
sendKeysTest is defined inside the window service. This program runs
fine
and
I can see cmd in my task manager that it is started, but I don't see
the
cmd
window. Is there any way I can see the cmd window. This code runs fine
when I
run this code in windows form.

private void Button1_Click(object sender, System.EventArgs e)
{
try
{

runner.sendKeysTest();
}
catch(Exception ex)
{
string x = ex.Message;
}

}

Thanks,

Any help will be appreciated.
 
N

Nicholas Paldino [.NET/C# MVP]

So wait, you are running the program from the service, and then trying
to make a call to send a key combination from your client, to the service,
to the process that was spawned by the service?

Why not have the client run the program directly?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
I am using remoting. I calling that sendKeyTest method using .net remoting.

Nicholas Paldino said:
Vinki,

If you want to have an application interact with a service, you
should
use remoting or WCF to make calls into your service. You can't access
the
service directly to make calls.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
I will uncheck the interacting with desktop option. All I am trying to
do
is
from my windows application. I am trying to call a method that exists
in
the
window service. That method needs to start a process and use sendkey to
start
the process.

:

Vinki,

Why are you trying to interact with the desktop from a service?
What
exactly do you need to do? You shouldn't be interacting with the
desktop
from a service, because you can have multiple desktops running on a
box
at
any one time (due to terminal services) or none at all (if no one is
logged
in interactively).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

public void sendKeysTest()
{

Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("{ENTER}");
else
myProcess.Kill();

}
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

I have this code in my windowservice. Option to interact with
desktop
in
window service is also checked. When I click a button on the front
end.
Inside the button click event, I call this method sendKeysTest, and
sendKeysTest is defined inside the window service. This program runs
fine
and
I can see cmd in my task manager that it is started, but I don't see
the
cmd
window. Is there any way I can see the cmd window. This code runs
fine
when I
run this code in windows form.

private void Button1_Click(object sender, System.EventArgs e)
{
try
{

runner.sendKeysTest();
}
catch(Exception ex)
{
string x = ex.Message;
}

}

Thanks,

Any help will be appreciated.
 
G

Guest

because client is located on some other machine and the process that i want
to start using sendkeys is on some other machine.

Nicholas Paldino said:
So wait, you are running the program from the service, and then trying
to make a call to send a key combination from your client, to the service,
to the process that was spawned by the service?

Why not have the client run the program directly?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
I am using remoting. I calling that sendKeyTest method using .net remoting.

Nicholas Paldino said:
Vinki,

If you want to have an application interact with a service, you
should
use remoting or WCF to make calls into your service. You can't access
the
service directly to make calls.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I will uncheck the interacting with desktop option. All I am trying to
do
is
from my windows application. I am trying to call a method that exists
in
the
window service. That method needs to start a process and use sendkey to
start
the process.

:

Vinki,

Why are you trying to interact with the desktop from a service?
What
exactly do you need to do? You shouldn't be interacting with the
desktop
from a service, because you can have multiple desktops running on a
box
at
any one time (due to terminal services) or none at all (if no one is
logged
in interactively).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

public void sendKeysTest()
{

Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("{ENTER}");
else
myProcess.Kill();

}
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

I have this code in my windowservice. Option to interact with
desktop
in
window service is also checked. When I click a button on the front
end.
Inside the button click event, I call this method sendKeysTest, and
sendKeysTest is defined inside the window service. This program runs
fine
and
I can see cmd in my task manager that it is started, but I don't see
the
cmd
window. Is there any way I can see the cmd window. This code runs
fine
when I
run this code in windows form.

private void Button1_Click(object sender, System.EventArgs e)
{
try
{

runner.sendKeysTest();
}
catch(Exception ex)
{
string x = ex.Message;
}

}

Thanks,

Any help will be appreciated.
 
W

Willy Denoyette [MVP]

Vinki said:
because client is located on some other machine and the process that i want
to start using sendkeys is on some other machine.

AFAIK, you can't send keys to the cmd.exe window, don't know why you said it worked from
Winforms, did you really succeeded in sending keystrokes to the cmd window?

Why not simply start the program you want to run instead of cmd.exe? But keep in mind when
doing so, that programs started from a Service are running in the same restricted context as
the service, that is, they should not interact with the desktop, they should not assume a
user is actually logged on, they should not assume any user profile and environment to be
loaded, they should never show any dialogs or error message boxes and they should never ever
require any user response.

Anyway, the best method is let the client run the remote program via "remote desktop" or
"terminal services". Another option is "telnet" or better use the freely downloadable PS
tools from SysInternals (now owned by Microsoft)
http://www.microsoft.com/technet/sysinternals/ProcessesAndThreads/PsTools.mspx

Willy.


Nicholas Paldino said:
So wait, you are running the program from the service, and then trying
to make a call to send a key combination from your client, to the service,
to the process that was spawned by the service?

Why not have the client run the program directly?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vinki said:
I am using remoting. I calling that sendKeyTest method using .net remoting.

:

Vinki,

If you want to have an application interact with a service, you
should
use remoting or WCF to make calls into your service. You can't access
the
service directly to make calls.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I will uncheck the interacting with desktop option. All I am trying to
do
is
from my windows application. I am trying to call a method that exists
in
the
window service. That method needs to start a process and use sendkey to
start
the process.

:

Vinki,

Why are you trying to interact with the desktop from a service?
What
exactly do you need to do? You shouldn't be interacting with the
desktop
from a service, because you can have multiple desktops running on a
box
at
any one time (due to terminal services) or none at all (if no one is
logged
in interactively).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

public void sendKeysTest()
{

Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("{ENTER}");
else
myProcess.Kill();

}
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

I have this code in my windowservice. Option to interact with
desktop
in
window service is also checked. When I click a button on the front
end.
Inside the button click event, I call this method sendKeysTest, and
sendKeysTest is defined inside the window service. This program runs
fine
and
I can see cmd in my task manager that it is started, but I don't see
the
cmd
window. Is there any way I can see the cmd window. This code runs
fine
when I
run this code in windows form.

private void Button1_Click(object sender, System.EventArgs e)
{
try
{

runner.sendKeysTest();
}
catch(Exception ex)
{
string x = ex.Message;
}

}

Thanks,

Any help will be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top