| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
|||
|
|||
|
Could you not only use the create thread call... or are you starting a completly different program?
|
|
#3
|
|||
|
|||
|
Its a completely new program
btw, this program is going to be a startup script for my computer on windows |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > General - CreateProcess() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|