Member Avatar for soo5Lo6k

Hi, I am programming some console application that updates itself periodically from Internet. (windows.h + pthreads for win32)
I want to do something like:

MAIN FUNCTION:
create thread
launch updater
THREAD:
infinite loop - starts the main application, exits if application ended normally but when it was killed by updater, start it again.
UPDATER:
checks, if updates are available → kill main app and download new version.

To the application, I need a PROCESS_INFORMATION pi, but:
when i define it globally, the THREAD function freezes when tryiing to CreateProcess.
when i tried to define it like a pointer, compiler told me I am stupid. What should I do?

Recommended Answers

All 3 Replies

Post actual code that illustrates the problem.

Member Avatar for soo5Lo6k
#include "stdio.h"
#include "malloc.h"
#include "unistd.h"
#include "pthread.h"
#include "windows.h"
PROCESS_INFORMATION *zi;
PROCESS_INFORMATION hidden( TCHAR * co )
{
    PROCESS_INFORMATION pi;
    STARTUPINFO si;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    //si.dwFlags = STARTF_USESHOWWINDOW;
    //si.wShowWindow = SW_HIDE;

    ZeroMemory( &pi, sizeof(pi) );


    // Start the child process. 
    CreateProcess( NULL,   // No module name (use command line)
        co,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi );           // Pointer to PROCESS_INFORMATION structure

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return (int *) pi;
}
PROCESS_INFORMATION hiddens( TCHAR * co )
{
    PROCESS_INFORMATION pi;
    STARTUPINFO si;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    //si.dwFlags = STARTF_USESHOWWINDOW;
    //si.wShowWindow = SW_HIDE;

    ZeroMemory( &pi, sizeof(pi) );


    // Start the child process. 
    CreateProcess( NULL,   // No module name (use command line)
        co,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi );           // Pointer to PROCESS_INFORMATION structure
    return (int *) pi;
}
int nekonecny_beh(){
printf("Nekonecny beh..\n");
while (1){
printf("Lemra spustena\n");
zi=hidden("lemra.exe");
}
}
int iterace(){
//PROCESS_INFORMATION pi;
hidden("cmd /C erase verze.new");
printf("Porovnavam\n");
hidden("wget someaddress/verze.new");
if (diff("verze.old","verze.new")){
printf("Nova verze dostupna, stahuji\n");
hidden("cmd /C move verze.new verze.old");
printf("Help\n");
PostThreadMessage(&zi.dwThreadId, WM_CLOSE, 0, 0);
WaitForSingleObject(&zi.hProcess, 1000);
TerminateProcess(&zi.hProcess, 0);
hidden("cmd /C erase lemra.exe");
hidden("wget someaddress/lemra.exe");
hidden("lemra.exe");
} else {
printf("Lemrouch je aktualni");
}
return 0;
}
int diff(const char * oldf,const char * newf){
int oldverze[3]={0,0,0},newverze[3]={0,0,0},x;
FILE *v,*upv;
v=fopen(oldf,"r");
upv=fopen(newf,"r");
if (v==NULL) {return 1;} // prvni spusteni nebo bug
if (upv==NULL) {return 0;} // neni net
fscanf(v,"%d.%d.%d",&oldverze[0],&oldverze[1],&oldverze[2]);
fscanf(upv,"%d.%d.%d",&newverze[0],&newverze[1],&newverze[2]);
fclose(v);
fclose(upv);
for (x=0;x<3;x++) {
if(newverze[x]>oldverze[x]) {
return 1;
}
if(newverze[x]<oldverze[x]){
return 0;
}
}
return 0;
}
int main(void){
pthread_t vlakno;
pthread_create(&vlakno, NULL, nekonecny_beh, NULL);
while(1){
iterace();
sleep(5000);
}
}

(The (int *) was my last desperate try to get it work..)

1. delete lines 19-25 because the function prototype is already available in windows.h

line 37: Just like any ordinary C or C++ program functions can not return a pointer to an object created on the stack. A better way to do it is to pass a reference to the object as a parameter void hidden( TCHAR * co, PROCESS_INFORMATION& pi ) then delete line 9.

I get the impression from just the two above errors that you are not very familiar with C or C++ languages. Writing MS-Windows programs is NOT for beginners, it is assumed that you know what the hell you are doing.

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.