Well, that is because I haven't shown my whole source at all. I've shown just 2 functions. You guys leave my no choice, but I hope showing everything will lead to a complete solution.
Updater.cpp
Code:
#include "updater.h"
//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInst;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
HWND hwndpb;
HANDLE thread_download;
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&InitCtrlEx);
// Now create thread for progess bar
thread_download = CreateThread( NULL, 0, CheckForUpdate, 0, CREATE_SUSPENDED, NULL);
SuspendThread(thread_download);
DialogBox(hInst, MAKEINTRESOURCE(IDD_CONTROLS_DLG),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
hwndpb = CreateWindowEx(0, PROGRESS_CLASS, NULL,
WS_CHILD | WS_VISIBLE | PBS_SMOOTH ,
20, 20, 260, 17,
hWndDlg, (HMENU)IDC_PROGBAR, hInst, NULL);
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDUPDATE:
{
ResumeThread(thread_download);
return TRUE;
}
break;
case IDCANCEL:
{
CloseHandle(thread_download);
DestroyWindow(hwndpb);
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
}
return FALSE;
}
Updater.h
Code:
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#define IDD_CONTROLS_DLG 100
#define IDC_PROGBAR 101
#define IDUPDATE 102
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
void DownloadFile(char *url, char *outFile);
void prog_update(void *p, double dltotal, double dlnow, double ult, double uln);
DWORD WINAPI CheckForUpdate(void *file);
extern HWND hWnd;
extern HWND hwndpb;
extern int num_files;
struct Version
{
int major;
int minor;
int build;
int numFiles;
};
struct Download
{
char name[56];
char outName[16];
};
Resource.rc
Code:
#include "updater.h"
#include <afxres.h>
1 RT_MANIFEST "style.xml"
IDD_CONTROLS_DLG DIALOG 260, 200, 200, 120
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Project DOD Updater"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON "Update", IDUPDATE, 30, 100, 50, 14
PUSHBUTTON "Close", IDCANCEL, 120, 100, 50, 14
END
Download.cpp
Code:
#include "updater.h"
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
void DownloadFile(char *url, char *outFile)
{
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if (curl)
{
fp = fopen(outFile,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, prog_update);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
Thread.cpp
Code:
#include "updater.h"
struct Version curVer;
struct Version version;
FILE *fp;
int num_files;
void GetProgVersion(void)
{
fp = fopen("version.dat", "rb");
fread(&curVer, sizeof(curVer), 1, fp);
fclose(fp);
}
int GetUpdate(void) // num_files = GetUpdate();
{
DownloadFile("http://website.net/projects/dod/update.dat","update.dat");
struct Download file[version.numFiles];
fp = fopen("update.dat", "rb");
fread(&version, sizeof(version), 1, fp);
fread(&file, sizeof(file), 1, fp);
fclose(fp);
return(version.numFiles);
}
void DownloadUpdates(void *file)
{
int j;
for(j = 0; j < num_files; j++)
{
SendMessage(hwndpb, PBM_SETPOS, 0, 0);
DownloadFile((struct*)file[j].name, (struct*)file[j].outName); //for reg struct
//DownloadFile((*file)[j].name, (*file)[j].outName);
}
}
void prog_update(void *p, double dltotal, double dlnow, double ult, double uln)
{
SendMessage(hwndpb, PBM_SETPOS, (int)dlnow/(int)dltotal*200, 0);
}
// This creates the second thread to our application which will
// control the downloading as the progress bar works
DWORD WINAPI CheckForUpdate(void *file)
{
GetProgVersion();
num_files = GetUpdate();
SendMessage(hwndpb, PBM_SETRANGE, 0, MAKELPARAM(0, 200 ));
if(version.major > curVer.major || version.minor > curVer.minor || version.build > curVer.build)
{
int ret = MessageBox(hWnd, "An update is available to download. Would you like to download it?", "Update Available", MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON1);
if(ret == IDNO)
return 2;
DownloadUpdates((void*)file);
return 0;
}
else
MessageBox(hWnd, "No updates are available to download.", "No Updates Available", MB_OK | MB_ICONINFORMATION);
return 1;
}
There is my entire project contents. I'm downloading files vai libcurl. Firstly, I do call CreateThread() upon initialization. Secondly, I download the update.dat file, which contains the update version, and all the file url's and the outfilenames of all the necessary files for the update (which is dynamic). Thirdly, this is the updater, and I have no plans of updating it. I just need a good updater, so it does not require it to be updated.