C++ Installations-Assistent
Posted on: November 18th, 2009
Tags: bmFinanz, C++, Client, Installation, Library, Visual FoxPro
Ein einfacher Installations-Assistent, der bei bedarf Daten vom Internet holt, diese entpackt, gegebenenfalls Systemdateien registriert und wenn alles in Ordnung ist die Software startet.
Der Assistent wurde für die Software bmFinanz entwickelt, welches auf Visual FoxPro basiert und deshalb abhängig von der Microsoft Visual FoxPro Library bez. einem Client ist. Mit diesem C++ Programm kann die Software immer und überall gestartet werden, unabhängig davon, ob ein Client installiert wurde oder nicht.
Hier der komplette C++ Code. Das Programm wurde mit Dev-C++ 4.9.9.0 geschrieben.
#include <sys/stat.h>
#include <iostream>
#include <string>
using namespace std; // translate std::
#define IDC_MAIN_EDIT 101 // ID of textbox
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void CenterWindow (HWND);
bool existsFile (string);
void proto (string);
void downloadFile (char*,char*);
void loader(char*,bool);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
HWND hwnd;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"bmFinanz Installer",/* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
200, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
bool err = false;
if(!existsFile("vfp7r.dll")||!existsFile("vfp7rdeu.dll")||!existsFile("msvcr70.dll")||!existsFile("main000.exe")){
CenterWindow(hwnd);
ShowWindow (hwnd, nFunsterStil);
proto("Downloading bmFinanz....");
remove( "buclient.exe");
downloadFile("http://www.bm-informatik.ch/update_buha/buclient.e00","buclient.e00");
if(rename("buclient.e00", "buclient.exe") == 0){
proto("\r\nbmFinanz wird installiert....");
loader("buclient.exe -y",true);
remove( "buclient.exe");
proto("\r\nDownloading Updates....");
remove( "kom07.exe");
downloadFile("http://www.bm-informatik.ch/update_buha/kom07.e00","kom07.e00");
if(rename("kom07.e00", "kom07.exe") == 0){
proto("\r\nUpdate wird installiert....");
loader("kom07.exe -y",true);
remove( "kom07.exe");
}else{
proto("\r\nFehler beim Umbenennen der Datei....");
err=true;
}
}else{
proto("\r\nFehler beim Umbenennen der Datei....");
err=true;
}
}
if( !err){
loader("REGSVR32.EXE vfp7r.dll /s",true);
loader("REGSVR32.EXE vfp7rdeu.dll /s",true);
loader("REGSVR32.EXE MSCOMCTL.OCX /s",true);
WinExec("main000.exe",SW_SHOW);
}
/* Close the window */
PostQuitMessage (0);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
break;
case WM_SIZE:
{
HWND hEdit;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
void proto(string s){
HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
int nLen = GetWindowTextLength (hEdit);
int nLen2 = s.length();
char* buffer = new char[nLen+nLen2+1];
GetWindowText(hEdit, buffer, nLen);
if(nLen>0){
strcat(buffer,s.c_str());
SetWindowText(hEdit, buffer);
}else{
SetWindowText(hEdit, s.c_str());
}
UpdateWindow(hwnd); //refresh
}
void loader(char szArgs[], bool hiden)
{
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
if(hiden){
si.dwFlags = STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
}
if(CreateProcess(NULL, szArgs, 0, 0, FALSE, 0, 0, 0, LPSTARTUPINFOA(&si), &pi))
{
// optionally wait for process to finish
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
void CenterWindow (HWND hwnd) {
RECT rc;
GetWindowRect(hwnd,&rc);
int nWidth = rc.right - rc.left;
int nHeight = rc.bottom - rc.top;
int X = ((int) GetSystemMetrics(SM_CXFULLSCREEN) - nWidth) /2; // center window horizontally
int Y = ((int) GetSystemMetrics(SM_CYFULLSCREEN) - nHeight) /2; // and vertically
MoveWindow(hwnd,X,Y,nWidth,nHeight,TRUE);
}
void downloadFile(char* url, char* fileName)
{
typedef int * (*URLDownloadToFileA)(void*,char*,char*,DWORD,void*);
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
URLDownloadToFileA URLDownloadToFile = (URLDownloadToFileA) GetProcAddress(LibHnd,"URLDownloadToFileA");
URLDownloadToFile(0, url, fileName, 0, 0);
}
bool existsFile(string strFilename) {
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0) {
// We were able to get the file attributes
// so the file obviously exists.
blnReturn = true;
} else {
// We were not able to get the file attributes.
// This may mean that we don't have permission to
// access the folder which contains this file. If you
// need to do that level of checking, lookup the
// return values of stat which will give you
// more details on why stat failed.
blnReturn = false;
}
return(blnReturn);
}
Posted in C++,Portfolio | Trackback Url









No Responses to “C++ Installations-Assistent”
Trackbacks/Pingbacks
Leave a reply