Hello guys. I made a simple program that works like command prompt. You insert a command, it inserts it into system();
why i do this? because im also adding my own commands. so before it goes into system. it checks first if its one of my newly made commands.
Whats the problem?
Well i was trying out a simple thing:

start notepad.exe
but when i got to:
taskkill /IM notepad.exe
it crashed?!!

So i tryed other things, if i do taskkill for anything EXEPT notepad.exe it works and doesent crash.

WTF is the problem?!!? please help. Tyvm guys.

Recommended Answers

All 7 Replies

Don't know what the problem is. Use your compiler's debugger and single-step through the code. Maybe it has a problem with the "/IM" parameter. Maybe that parameter should follow notepad.exe.

Don't know what the problem is. Use your compiler's debugger and single-step through the code. Maybe it has a problem with the "/IM" parameter. Maybe that parameter should follow notepad.exe.

Well the command works fine in CMD.exe. and im not completly sure how to use DevC++'s(the compiler im using) single-step debugger.
Maybe i'll bring it to visual studio to debug.

Ok let me explain this more...

Im making a sort-of remote computer control. Im not adding GUI yet, but i wanted at least to have control to the CMD. because you can do allot from the CMD. so im doing this with a predefined winsock class i found.

#pragma once
#include "Winsock2.h"

const int STRLEN = 256;

class Socket
{
    public:
        Socket();
        ~Socket();
        bool SendData( char* );
        bool RecvData( char*, int );
        void CloseConnection();
        char* GetMessage();
    protected:
        WSADATA wsaData;
        SOCKET mySocket;
        SOCKET myBackup;
        SOCKET acceptSocket;
        sockaddr_in myAddress;
};

class ServerSocket : public Socket
{
    public:
        void Listen();
        void Bind( int port );
        void StartHosting( int port );
};

class ClientSocket : public Socket
{
    public:
        void ConnectToServer( const char *ipAddress, int port );
};

Socket::Socket()
{
    if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
    {
        cerr << "Socket Initialization: Error with WSAStartup\n";
        system("pause");
        WSACleanup();
        exit(10);
    }

    //Create a socket
    mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( mySocket == INVALID_SOCKET )
    {
        cerr << "Socket Initialization: Error creating socket" << endl;
        system("pause");
        WSACleanup();
        exit(11);
    }

    myBackup = mySocket;
}

Socket::~Socket()
{
    WSACleanup();
}

bool Socket::SendData( char *buffer )
{
    send( mySocket, buffer, strlen( buffer ), 0 );
    return true;
}

bool Socket::RecvData( char *buffer, int size )
{
    int i = recv( mySocket, buffer, size, 0 );
    buffer[i] = '\0';
    return true;
}

void Socket::CloseConnection()
{
    closesocket( mySocket );
    mySocket = myBackup;
}

void ServerSocket::StartHosting( int port )
{
     Bind( port );
     Listen();
}

void ServerSocket::Listen()
{
    if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
    {
        cerr << "ServerSocket: Error listening on socket\n";
        system("pause");
        WSACleanup();
        exit(15);
    }    
    acceptSocket = accept( myBackup, NULL, NULL );
    while ( acceptSocket == SOCKET_ERROR )
    {
        acceptSocket = accept( myBackup, NULL, NULL );
    }
    mySocket = acceptSocket;
}

void ServerSocket::Bind( int port )
{
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
    myAddress.sin_port = htons( port );

    if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
    {
        cerr << "ServerSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(14);
    }
}

void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr( ipAddress );
    myAddress.sin_port = htons( port );

    if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
    {
        cerr << "ClientSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(13);
    } 
}

I made this same program before and it worked. But i wanted it to be easier to add commands. So i made a class for it. In this class it would connect to the client. The server sends info to the client, the client takes that and puts it into system(); then it sends back "Done".
This all works for EVERY COMMAND POSSIBLE exept for "taskkill /IM notoepad.exe"
could it possible be because i use a config.ini file to get the ip? may it not have closed correctly? Heres how i get the ip:

char ip[256];
    ifstream fin("config.ini");
    if(!fin)
        sprintf(ip,"127.0.0.1"); // default ip (Loopback)
    else
    {
        fin.getline(ip,256);
        fin.close();
    }

then i plug that ip so i can connect to it.

I dont know why every command works fine exept one. Its really weird. Thanks for your help again.

I never figured out how to use Dev-C++ debugger either -- I always use VC++ because it has such a great debugger. In your programs you may have to use two debugging sessions -- one for client and another for server. With VC++ I've done that successfully quite a few times.

As for your problem: Sorry, but I have no idea what's wrong.

well ty anyway. Ill try using VC++

Just wondering...Notepad and other editor like applications tend to respond to a normal 'terminate' message with a user query if their file has been modified, but don't terminate until they get an answer.

Might this be a part of your problem?

Strangly, when i do taskkill /F /IM notepad.exe it works.
I just gota remember to use /F i guess.. But i still don't know why it would crash. At least get an error message. but it shouldent crash... Ty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.