Pass value to windows command

Hi everyone,

I am new here. I took C++ as one of my school requirements and I found it interesting. I am currently working on a program that will allow me to remote connect to users computers.

I want usrname to take my input and then I want the program to pass the value of usrname to system("vncviewer.exe -AutoSelect=0 -lowcolourlevel=2 usrname");

Please see the code below:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <windows.h>
using namespace std;
int main()

{

string usrname;
string carlos = "carlospc";
string james = "jamespc";

    cout << "Please enter Name: "; cin >> usrname;
    cout <<endl;
    cout << "Connecting to "; cout << usrname; cout << "'PC, Please wait..." <<endl;
    cout <<endl;
    system("vncviewer.exe -AutoSelect=0 -lowcolourlevel=2 usrname");
    system ("pause");


}


thank you
If I understood you well, in line 16 you need to say for example:
system("vncviewer.exe -AutoSelect=0 -lowcolourlevel=2 carlos");
(username is variable in this line)

don't forget that system takes a char* parameter (const char* to be precise)
so you can do:
1
2
3
4
string s="vncviewer.exe -AutoSelect=0 -lowcolourlevel=2 ";
s+=usrname;
const char *s2=s.c_str();
system(s2);

I think that might work
Last edited on
Hi JewelCpp,

Thanks for your response. This works; however, I would like the program that instead of calling 'carlos'

 
system("vncviewer.exe -AutoSelect=0 -lowcolourlevel=2 carlos");


I want it to call 'carlospc'

 
system("vncviewer.exe -AutoSelect=0 -lowcolourlevel=2 carlospc");


The reason being is that computer name is not based on the user but it is based on the department. (carlospc is just an example) This is why I did the following:
1
2
string carlos = "carlospc";
string james = "jamespc";


So when I enter carlos, the program will know to call:

 
system("vncviewer.exe -AutoSelect=0 -lowcolourlevel=2 carlospc");


Please let me know if this is possible.

Thank you!!!
To my knowledge this is not possible like that.
However I think that If you put the user name and the computer name in a bi-dimensional string array you would be able to do it.
So you'll find the computer name based on the user name and do like what I said in my previous post.
Hope this help!
OK, I am genuinely confused now. You are including the 'windows.h' header file and not 'cstdlib', but you are using the "system()" function instead of "CreateProcess()" to launch VNC... Why would you do this?

Side Note: You can also send the password on the command line when you launch vncviewer with the "/password" switch. I wasn't sure if you knew that and just left it out or what but I thought I would mention it.

Something to add to your project later:
You're already including the 'windows.h' header so you should be able to link to the 'AdvApi32.lib' and also include 'WinSvc.h' without having do download anything additional. Before you make what ever call you decide on to launch the vncviewer app you should probably make sure that it has something to connect to on the other side, right? To do this you'll want to call the "OpenSCManager()" and feed it the target PC's name, which you already have since you need it to launch vncviewer. If "OpenSCManager()" succeeds it returns a 'SC_HANDLE' object to the target PC's Service Control Manager which you should then use as an argument to the "OpenService()" function along with the name of the VNC Server service (I'll save you the trip to Google, it's 'winvnc' without the quotes). The "OpenService()" function (again if it succeeds) returns a different 'SC_HANDLE' object that matches up with that service on the target machine, you will use this with the "QueryService()" function to fill out the 'SERVICE_STATUS' struct and tell you if the service is up and running. If it is then great, on you go, but if it is not then you will want to start it up again won't you? This is done by passing the same 'SC_HANDLE' object that was returned from the "OpenService()" function to the function called "StartService()", the return code from that function will tell you if it worked or not.

I wrote this up to illustrate how easy it is to do something like interact with a remote computer using the WinAPI. This is to encourage you try doing things the right way and stop using this "system()" call crap. Also, feel free to check out the Windows section of the forum if you have questions about stuff like this.

- OpenSCManager(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms684323(v=vs.85).aspx

- OpenService(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms684330(v=vs.85).aspx

- QueryServiceStatus(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms684939(v=vs.85).aspx

- SERVICE_STATUS: http://msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx

- StartService(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms686321(v=vs.85).aspx

- CreateProcess(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

- You'll also want these for cleanup time CloseSC_HANDLE(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx

AND

- CloseServiceSC_HANDLE(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms682028(v=vs.85).aspx
Last edited on
Jewellcpp: Thanks for your response. I will look into it.

Computergeek01:
OK, I am genuinely confused now. You are including the 'windows.h' header file and not 'cstdlib', but you are using the "system()" function instead of "CreateProcess()" to launch VNC... Why would you do this?


Because I am using however I learned in my C++ class. As you can see on my first post, I am stating that I took 1 c++ class. I am not a program nor I want to be. I am a sys admin who would like to make my life easier.

Side Note: You can also send the password on the command line when you launch vncviewer with the "/password" switch. I wasn't sure if you knew that and just left it out or what but I thought I would mention it.


I knew that... for security reasons, I don't. In case someone takes the program and runs it. And I know what you might say "vnc is not secure" well, I use it for my internal network and we are behind a firewall. Also, I have a complex password setup on each machine.

I wrote this up to illustrate how easy it is to do something like interact with a remote computer using the WinAPI. This is to encourage you try doing things the right way and stop using this "system()" call crap. Also, feel free to check out the Windows section of the forum if you have questions about stuff like this.


As stated before, I am using whatever my teacher taught me. I am not a programmer. I will look into your suggestion.

Thank you
Topic archived. No new replies allowed.