C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 20th, 2009, 02:20 AM
kotarou3 kotarou3 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 4 kotarou3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 sec
Reputation Power: 0
General - CreateProcess()

When i try to create a process, GetLastError() reports error code 998: Access to invalid memory location occured

My Code is

path/test/main.cpp:
Code:
#include "../tools.h"
#include "../startup/header.h"

int main()
{
	start("\"path\\to\\program\"",5,"description");
	system("pause");
	return 0;
}


path/tools.h
Code:
#ifndef _TOOLS_H_
#define _TOOLS_H_

#include <iostream>
#include <string.h>
#include <sstream>
#include <cstdlib>
#include <windows.h>
using namespace std;

//#include "tools.cpp"

string add2cstrings(char* cstring1,char* cstring2,bool hmm);
char* add2cstrings(char* cstring1,char* cstring2);
void gotoxy(int col,int row);
void wait(int seconds,string message,bool visible);
bool StartProcess(LPTSTR command,string description);
string IntToString(int integer);
void startService(string service,string description);
void errorCodeHandle(DWORD code,string description);

#endif


path/tools.cpp
Code:
#ifndef _TOOLS_CPP_
#define _TOOLS_CPP_

#include "tools.h"

#define INTERVAL 250

string add2cstrings(char* cstring1,char* cstring2,bool hmm)
{
	string out = cstring1;
	out += cstring2;
	return out;
}

char* add2cstrings(char* cstring1,char* cstring2)
{
	char nearly[2048];
	strcpy(nearly,const_cast<char*>(add2cstrings(cstring1,cstring2,true).c_str()));
	char* out = nearly;
	return out;
}

void gotoxy(int col,int row)
{
	HANDLE hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

	if (INVALID_HANDLE_VALUE != hConsole)
	{
		COORD pos = {col,row};
		SetConsoleCursorPosition(hConsole,pos);
	}
}

void wait(int seconds,string message,bool visible)
{
	int times = seconds*(1000/INTERVAL);

	if (visible)
		cout << message << ":";

	for (int i=0;times>=i;i++)
	{
		if (visible)
			cout << ".";
		Sleep(INTERVAL);
	}
	cout << endl;
}

bool StartProcess(LPTSTR command,string description)
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	bool var = false;
	if(CreateProcess(NULL,command,NULL,NULL,FALSE,0,NU  LL,NULL,&si,&pi) == NULL)
	{
		errorCodeHandle(GetLastError(),description);
		var = true;
	}
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return var;
}

string IntToString(int integer)
{
	stringstream tmp;
	tmp << integer;
	return tmp.str();
}

void startService(string service,string description)
{
	SERVICE_STATUS g_ssStatus;

    SC_HANDLE hSCManager, hService;
    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if(hSCManager == NULL)
	{
		errorCodeHandle(GetLastError(),"NULL");
		goto end;
	}
    hService = OpenService(hSCManager, service.c_str(), SERVICE_ALL_ACCESS);
	if(hService == NULL)
	{
		if(ERROR_ACCESS_DENIED == GetLastError())
			cout << "The handle does not have access to the service";
		else if(ERROR_INVALID_NAME == GetLastError())
			cout << "The service " << service << " is invalid";
		else
			errorCodeHandle(GetLastError(),service);
		goto endman;
	}
	if(StartService(hService,0,NULL))
	{
		cout << "Starting " << description << ":";
		Sleep(INTERVAL);
		while(QueryServiceStatus(hService,&g_ssStatus))
		{
			if(SERVICE_START_PENDING == g_ssStatus.dwCurrentState)
			{
				cout << ".";
				Sleep(INTERVAL);
			}
			else
				break;
		}
		if(SERVICE_RUNNING == g_ssStatus.dwCurrentState)
			cout << endl << description << " has started";
		else if(ERROR_ACCESS_DENIED == GetLastError())
			cout << "The handle does not have the SERVICE_QUERY_STATUS access right";
		else
			errorCodeHandle(GetLastError(),description);
	}
	else if(ERROR_ACCESS_DENIED == GetLastError())
		cout << "The handle does not have the SERVICE_START access right";
	else
		errorCodeHandle(GetLastError(),description);
	CloseServiceHandle(hService);
endman:
	CloseServiceHandle(hSCManager);
end:
	cout << endl;
}

void errorCodeHandle(DWORD code,string description)
{
	switch(code)
	{
	case ERROR_ACCESS_DENIED:
		cout << "The requested access was denied";
		break;
	case ERROR_INVALID_HANDLE:
		cout << "The handle to " << description << " is invalid";
		break;
	case ERROR_INVALID_NAME:
		cout << "The name " << description << " is invalid";
		break;
	case ERROR_SERVICE_DOES_NOT_EXIST:
		cout << "The service " << description << " does not exist";
		break;
	case ERROR_PATH_NOT_FOUND:
		cout << description << " does not exist";
		break;
	case ERROR_SERVICE_ALREADY_RUNNING:
		cout << description << " is already running";
		break;
	case ERROR_SERVICE_DATABASE_LOCKED:
		cout << description << "\'s database is locked";
		break;
	case ERROR_SERVICE_DEPENDENCY_FAIL:
		cout << "One or more of " << description << "\'s dependencies could not be started or isn\'t started";
		break;
	case ERROR_SERVICE_DISABLED:
		cout << description << " has been disabled";
		break;
	case ERROR_SERVICE_LOGON_FAILED:
		cout << description << " could not start due to a logon failure. This occurs if the service is" << endl << "configured to run under an account that does not have the \"Log on as a service\" right";
		break;
	case ERROR_SERVICE_MARKED_FOR_DELETE:
		cout << description << " has been marked for deletion";
		break;
	case ERROR_SERVICE_NO_THREAD:
		cout << "A thread could not be created for " << description;
		break;
	case ERROR_SERVICE_REQUEST_TIMEOUT:
		cout << "The process for " << description << " was started, but it did not call" << endl << "StartServiceCtrlDispatcher(), or the thread that called StartServiceCtrlDispatcher() may be blocked" << endl << "in a control handler function";
		break;
	case ERROR_DATABASE_DOES_NOT_EXIST:
		cout << "The \"" << description << "\" database does not exist";
		break;
	case ERROR_INVALID_PARAMETER:
		cout << "A specified parameter is invalid";
		break;
	case ERROR_NOACCESS:
		cout << "A invalid access to a memory location occured";
		break;
	default:
		cout << description << " failed with error code: " << GetLastError();
	}
}

#endif


path/startup/header.h
Code:
#ifndef _HEADER_H_
#define _HEADER_H_

#include "../tools.h"
//#include "function.cpp"

void start(LPTSTR command,int seconds,string description);

#endif


path/startup/function.cpp
Code:
#ifndef _FUNCTION_CPP_
#define _FUNCTION_CPP_

#include "../tools.h"
#include "header.h"

void start(LPTSTR command,int seconds,string description)
{
	if(StartProcess(command,description) == 0)
	{
		string message = "Waiting " + IntToString(seconds) + " seconds for " + description + " to start";
		wait(seconds,message,true);
	}
	cout << endl;
}

#endif

Reply With Quote
  #2  
Old July 1st, 2009, 03:26 PM
larssss larssss is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 30 larssss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 54 m 18 sec
Reputation Power: 1
Cool

Could you not only use the create thread call... or are you starting a completly different program?

Reply With Quote
  #3  
Old July 4th, 2009, 08:19 AM
kotarou3 kotarou3 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 4 kotarou3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 sec
Reputation Power: 0
Its a completely new program

btw, this program is going to be a startup script for my computer on windows

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - CreateProcess()


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
Stay green...Green IT