A friend gave me this file to compile.
My goal is to open 4 browser windows and then close them via "end task" with a
delay between them.
When I try to compile it (dev-C++) I'm getting this error message, How can I
solve it?
-------
68: Invalid conversion from 'const char*' to 'CHAR*'
-------
Source file:
---------------------------
Code:
#include <windows.h>
#include <winbase.h>
/* times in miliseconds */
#define INTERMEDIATE_DELAY 2000
#define FINAL_DELAY 15000
/* erm, duh */
#define NUMBER_OF_PROCESSES 4
/* yes yes, a dreaded global variable - speed vs size tradeoff,
who needs to initialize 68 bytes with code inside main when
we've got .data for it? ;) Besides, a smart compiler will
optimize it into .bss anyway */
STARTUPINFO startupInfo = {
sizeof(STARTUPINFO),
NULL,
NULL,
NULL,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
NULL,
NULL,
NULL,
NULL
};
/* const to let the system know it can page out without
hassle if needed, amounts to discardable memory */
const char* cmdLines[] = {
"C:\Program Files\Internet Explorer\iexplore.exe http://www.site1.com";,
"C:\Program Files\Internet Explorer\iexplore.exe http://www.site2.com";,
"C:\Program Files\Internet Explorer\iexplore.exe http://www.site3.com";,
"C:\Program Files\Internet Explorer\iexplore.exe http://www.site4.com";
};
int main()
{
unsigned int i;
HANDLE hProcesses[NUMBER_OF_PROCESSES];
PROCESS_INFORMATION procInfo;
/* add some time verification, etc here (suggest using
GetLocalTime() and checking against the desired stop time,
or GetTickCount() and compare a subtraction if it's just a
relative time instead of absolute hh:mm) */
for(;;)
{ /* infinite cycle doing the whole thing */
for (i=0;i<NUMBER_OF_PROCESSES;i++)
{ /* cycle creating the NUMBER_OF_PROCESSES processes */
if (!CreateProcess(NULL,
cmdLines[i],
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&procInfo))
{ /* couldn't run */
/* add code to print an error out based on GetLastError()
here, I'm lazy ;) - suggest using FormatMessage() */
return 2;
}
CloseHandle(procInfo.hThread); /* don't need this */
hProcesses[i]=procInfo.hProcess;
Sleep(INTERMEDIATE_DELAY);
}
/* final delay */
Sleep(FINAL_DELAY - INTERMEDIATE_DELAY);
/* _kill_ all processes */
for (i=0;i<NUMBER_OF_PROCESSES;i++)
{
TerminateProcess(hProcesses[i],0); /* die, you #$%@! */
CloseHandle(hProcesses[i]);
}
}
/* won't ever reach this as it is, it's here for when we actually
implement a stopping condition for the big for */
return 0;
}
------------------
I'd also like to run it for a period of time...
I wonder if someone can complete and test my program.
Thanks,
Jenny
(I don't understand anything about programming)
Fri Sep 03, 04 5:48 am
dinangkur Super Moderator
Joined: 24 Mar 2004 Posts: 491
Location: Dhaka, Bangladesh
Post subject:
Hi I just looked in your code and I've found 13 errors in it. So, according to you information, that "const" is just another code among those 13. By the way, I fix it and it's going to work now. If you want implement the second part - why don't you exercise a bit about it? Bring some code, then I will look into it. Enjoy.
I tested this code in VC++ 6 in console mode application.
Have fun.
-DK.
Code:
#include <windows.h>
#include <winbase.h>
/* times in miliseconds */
#define INTERMEDIATE_DELAY 2000
#define FINAL_DELAY 15000
/* erm, duh */
#define NUMBER_OF_PROCESSES 4
/* yes yes, a dreaded global variable - speed vs size tradeoff,
who needs to initialize 68 bytes with code inside main when
we've got .data for it? ;) Besides, a smart compiler will
optimize it into .bss anyway */
STARTUPINFO startupInfo = {
sizeof(STARTUPINFO),
NULL,
NULL,
NULL,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
NULL,
NULL,
NULL,
NULL
};
/* const to let the system know it can page out without
hassle if needed, amounts to discardable memory */
char* cmdLines[] = {
"C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.site1.com",
"C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.site2.com",
"C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.site3.com",
"C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.site4.com",
};
int main()
{
unsigned int i;
HANDLE hProcesses[NUMBER_OF_PROCESSES];
PROCESS_INFORMATION procInfo;
/* add some time verification, etc here (suggest using
GetLocalTime() and checking against the desired stop time,
or GetTickCount() and compare a subtraction if it's just a
relative time instead of absolute hh:mm) */
for(;;)
{ /* infinite cycle doing the whole thing */
for (i=0;i<NUMBER_OF_PROCESSES;i++)
{ /* cycle creating the NUMBER_OF_PROCESSES processes */
if (!CreateProcess(NULL,
cmdLines[i],
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&procInfo))
{ /* couldn't run */
/* add code to print an error out based on GetLastError()
here, I'm lazy ;) - suggest using FormatMessage() */
return 2;
}
CloseHandle(procInfo.hThread); /* don't need this */
hProcesses[i]=procInfo.hProcess;
Sleep(INTERMEDIATE_DELAY);
}
/* final delay */
Sleep(FINAL_DELAY - INTERMEDIATE_DELAY);
/* _kill_ all processes */
for (i=0;i<NUMBER_OF_PROCESSES;i++)
{
TerminateProcess(hProcesses[i], 0); /* die, you #$%@! */
CloseHandle(hProcesses[i]);
}
}
/* won't ever reach this as it is, it's here for when we actually
implement a stopping condition for the big for */
return 0;
}