Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

c++ hidden?

Status
Not open for further replies.

danielsmusic

New Member
i need to write a program that will shutdown a computer on the network.
i know how to do it and what to do but i need to run the program secretly.
just like how viruses do it you dont know there running but they are. how do i do this.
 
its not a virus if thats what you think :lol:

i need to shutdown my own computer from my sisters, but i don't want to see the program running.


i know you can use the shutdown command in com prompt but it don't work.
 
while remote shutdown is the topic i need to know why this code don't work properly
Code:
char *com;
cin >> com;
cout << "you entered " << com;
it prints only ONE word eg: i type "hello my name is daniel" it will print "hello"
 
First of all, you create a pointer to a char type. You cannot store actual data in a pointer, it can only be used to ... well ... point to somewhere in memory... It's VERY strange your above code even returned a word...


So you need to make an array of char's to store your string...

Secondly, it's dangerous to use cin that way to read a string!
there is no length checking that way - if you have a 20byte array, and the user enters more then 20 characters then you're getting into a situation wich ANSI like to call 'undefined behaviour' (mostly a crash)...
Use getline() member of istream, it gets rid of your original problem too, it keeps reading till it gets and EOL character (enter)...

For example:

Code:
char com[30];         //a buffer for 30 characters

cin.getline(com, 30); //read a line from cin - only store 30 characters , the rest is truncated
cout << "You entered " << com;
 
i have always been using char * and never had a problem
the code you posted didnt work i did a screenshot.

btw: it is not a DOS program just a console
 

Attachments

  • console.jpg
    console.jpg
    26 KB · Views: 380
the code isnt as important as the fact i need to hide it. do i do it in the regestry?
 
It's not because you didn't have problems with it yet that it's the right way to do it...

When you create a pointer, it points to some random memory position. When you write to it the data will be stored on the position the pointer points to, wich is NOT your memory...

If it works then you're just lucky that spot is unused at that time, but another application, or part of your own may be residing at that position...

You allways need to allocate a real variable first, then you can point at it as much as you like...

Code:
char *pCh;         // a pointer to a char - it points to a random position - writing to it is NOT allowed
char MyString[10];   //an array of 10 bytes

pCh = &MyString;   //assing the ADRESS of MyString to the pointer pCh - Now the pointer points to a valid memory position

As for the getline() solution - it should work - post your complete code rather then a screenshot of the output.
 
danielsmusic said:
the code isnt as important as the fact i need to hide it. do i do it in the regestry?

I don't know how to 'hide' a program, i never had the need to, sorry.
 
i don't like posting code it look so messy and i never comment bad programmer i am :oops: but it is important.

Code:
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
	string who_to_send;
	string addon = "\\C\\TMP.TMP";
	string addbe = "\\\\";
	cout << "Type the computer name to send to:";
	cin >> who_to_send;
	addbe.append(who_to_send);
	string final = addbe.append(addon);	
	cout << "\nHas this computer got the reciver program installed(1 = yes, 0 = no):";
	int yesno = 0;
	cin >> yesno;
	ofstream file;
	cout << "Loading please wait ...";
	file.open(final.data(), fstream::out);
	if(file.fail())
	{
		cout << "\nLoading failed!";
		cout << "\nThis program will ternamate in:3";
		Sleep(1000);
		cout << "\b \b";
		cout << "2";
		Sleep(1000);
		cout << "\b \b";
		cout << "1";
		Sleep(1000);
		return 0;
	}
	if(!yesno)
	{
		fstream file2;
		cout << "\nCopying...";
		file2.open(argv[0], fstream::in);
		string copy1;
		copy1.append("copy ");
		copy1.append(argv[0]);
		copy1.append(" ");
		copy1.append(who_to_send);
		copy1.append("\\\\C\\remote_shutdown.exe");
		cout << copy1;
		system(copy1.data());
		
	}
	cout << "\nPlease enter the command you want to send:";
	char * com;         
	cin >> com; 
	cout << "You entered " << com; 
	file << com;
	file.close();
	cout << "\n\n\n";
	return 0;
}
 
That's the old code :lol: ....
I mean post the new one with getline(), it should work, if it doesnt you made an error.
 
you see what i mean about poorly written :wink:

Code:
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
	string who_to_send;
	string addon = "\\C\\TMP.TMP";
	string addbe = "\\\\";
	cout << "Type the computer name to send to:";
	cin >> who_to_send;
	addbe.append(who_to_send);
	string final = addbe.append(addon);	
	cout << "\nHas this computer got the reciver program installed(1 = yes, 0 = no):";
	int yesno = 0;
	cin >> yesno;
	ofstream file;
	cout << "Loading please wait ...";
	file.open(final.data(), fstream::out);
	if(file.fail())
	{
		cout << "\nLoading failed!";
		cout << "\nThis program will ternamate in:3";
		Sleep(1000);
		cout << "\b \b";
		cout << "2";
		Sleep(1000);
		cout << "\b \b";
		cout << "1";
		Sleep(1000);
		return 0;
	}
	if(!yesno)
	{
		fstream file2;
		cout << "\nCopying...";
		file2.open(argv[0], fstream::in);
		string copy1;
		copy1.append("copy ");
		copy1.append(argv[0]);
		copy1.append(" ");
		copy1.append(who_to_send);
		copy1.append("\\\\C\\remote_shutdown.exe");
		cout << copy1;
		system(copy1.data());
		
	}
	cout << "\nPlease enter the command you want to send:";
	char com[30];         
	cin.getline(com, 30);  
	cout << "You entered " << com; 
	file << com;
	file.close();
	cout << "\n\n\n";
	return 0;
}
 
Right, it seems there is still a EOL character in the stream that wasn't extracted from a previous input action..

try adding a "cin.get()" in front of the "getline()" command.
 
It's because of this:

Code:
cout << "\nHas this computer got the reciver program installed(1 = yes, 0 = no):"; 
int yesno = 0; 
cin >> yesno;

You type a number (1 or 0) and then press the enter key, this puts the number + a EOL (enter) in the stream
The overloaded operator 'istream::eek:perator >>()' only reads your number from the stream and stores it in your variable yesno.
The EOL however stays in the stream - When you call the 'getline()' it will return immediately because the first character it reads is a EOL.

the extra istream::get() gets the EOL out of the stream.
 
As i know, hiding the program couldnt be done in the standart WIN32 console application of C++..U should use a WIN32 application or MFC because in <windows.h> you have functions that hide the window and couldnt be seen by the user.
Furthermore, u can run the program at the startup, all u have to do is to insert this program in the windows Registry in 'Run'
I advise to go for www.planet-source-code.com , you can find lot of source codes there :)
 
yes i had a feeling that i could'nt do it with a console app.

has anyone got any pointers(i dont mean the ones that you get in a program :lol: )
 
Porting your application to MFC isn't at all hard. It's a small application, you could do it by just letting the appwizard create a MFC project for you and copying-pasting your code...(assuming MSVC++ compiler)

But still i wouldnt know how to hide it then, presumably you'll have to make your program act as some system service ?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top