void CMainFrame::OnSize(UINT nType, int cx, int cy) { static int first_sizing = 0; if (nType == SIZE_MINIMIZED) { if (TRAY_ICON) ShowWindow (FALSE);// hide it if (HIDE_ICON) ShowWindow (FALSE); } CMDIFrameWnd::OnSize(nType, cx, cy); // On Windows XP, we need to tile on the second message, while on 64-bit // windows tiling on the first size message works. Go figure. if (nType != SIZE_MINIMIZED && first_sizing < 2) { int id; char rgch[80]; rgch[0] = 0; IniGetString (INI_FILE, "W0", rgch, sizeof(rgch), NULL); first_sizing++; id = (0 == rgch[0]) ? ID_WINDOW_TILE_HORZ : ID_WINDOW_POSITION; PostMessage (WM_COMMAND, id, 0); } }
/******************************************************************************* * desc: get a double value by key *------------------------------------------------------------------------------ * param: const char * psz_section -- section name * const char * psz_key -- key name * double i_default -- default value *------------------------------------------------------------------------------ * return: key value or default value *******************************************************************************/ double IniGetDouble(const char *psz_section, const char *psz_key, double i_default) { char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE]; //memset( sz_buffer, 0, sizeof(sz_buffer) ); if (IniGetString(psz_section, psz_key, sz_buffer)) { return atof(sz_buffer); } return i_default; }
BOOL getSubWindowPlacement( CWnd *pwnd, int vnum) { BOOL handled = FALSE; char name[16]; char rgch[80]; wsprintf(name, "W%d", vnum); rgch[0] = 0; IniGetString (INI_FILE, name, rgch, sizeof(rgch), NULL); if (0 != rgch[0]) { int state = 0; WINDOWPLACEMENT wp = {0}; RECT *prc = &wp.rcNormalPosition; POINT *pptMin= &wp.ptMinPosition; POINT *pptMax= &wp.ptMaxPosition; int count = sscanf(rgch, "%d %ld %ld %ld %ld %ld %ld %ld %ld", &state, &prc->top, &prc->right, &prc->bottom, &prc->left, &pptMin->x, &pptMin->y, &pptMax->x, &pptMax->y ); if (9 == count) { BOOL fMinimized = (0 != (state & P95_WP_MINIMIZED)); BOOL fMaximized = (0 != (state & P95_WP_MAXIMIZED)); wp.length = sizeof(wp); if (fMinimized) { wp.showCmd = SW_SHOWMINNOACTIVE; } else { wp.showCmd = (fMaximized) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL; } wp.flags = (fMaximized) ? WPF_RESTORETOMAXIMIZED : WPF_SETMINPOSITION; if (0 != pwnd->SetWindowPlacement(&wp)) { handled = TRUE; } } } return handled; }
/******************************************************************************* * desc: get a long value by key *------------------------------------------------------------------------------ * param: const char * psz_section -- section name * const char * psz_key -- key name * long i_default -- default value *------------------------------------------------------------------------------ * return: key value or default value *******************************************************************************/ long IniGetLong(const char *psz_section, const char *psz_key, long i_default) { char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE]; //memset( sz_buffer, 0, sizeof(sz_buffer) ); if (IniGetString(psz_section, psz_key, sz_buffer)) { if (strlen(sz_buffer) > 2) { // maybe a hex value if (sz_buffer[0] == '0' && (sz_buffer[1] == 'x' || sz_buffer[1] == 'X')) { return (strtol(sz_buffer, (char **)NULL, 16)); } } return atol(sz_buffer); } return i_default; }
/******************************************************************************* * desc: get a bool value by key *------------------------------------------------------------------------------ * param: const char * psz_section -- section name * const char * psz_key -- key name * bool b_default -- default value *------------------------------------------------------------------------------ * return: key value or default value *******************************************************************************/ int IniGetBool(const char *psz_section, const char *psz_key, int b_default) { char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE]; //memset( sz_buffer, 0, sizeof(sz_buffer) ); if (IniGetString(psz_section, psz_key, sz_buffer)) { DBGPRINT(("GetBool -- key value is: %s\n", sz_buffer)); if (strncasecmp(sz_buffer, "y", strlen("y")) == 0 || strncasecmp(sz_buffer, "yes", strlen("yes")) == 0 || strncasecmp(sz_buffer, "true", strlen("true")) == 0) return TRUE; if (strncasecmp(sz_buffer, "n", strlen("n")) == 0 || strncasecmp(sz_buffer, "no", strlen("no")) == 0 || strncasecmp(sz_buffer, "false", strlen("false")) == 0) return FALSE; } return b_default; }
/******************************************************************************* * desc: get a interger value by key *------------------------------------------------------------------------------ * param: const char * psz_section -- section name * const char * psz_key -- key name * int i_default -- default value *------------------------------------------------------------------------------ * return: key value or default value *******************************************************************************/ int IniGetInteger(const char *psz_section, const char *psz_key, int i_default) { char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE]; //memset( sz_buffer, 0, sizeof(sz_buffer) ); if (IniGetString(psz_section, psz_key, sz_buffer)) { DBGPRINT(("GetInteger -- key value is: %s\n", sz_buffer)); if (strlen(sz_buffer) > 2) { // maybe a hex value if (sz_buffer[0] == '0' && (sz_buffer[1] == 'x' || sz_buffer[1] == 'X')) { return (int)(strtol(sz_buffer, (char **)NULL, 16)); } } return atoi(sz_buffer); } return i_default; }
/******************************************************************************* * desc: set a string value by key *------------------------------------------------------------------------------ * param: const char * psz_section -- section name * const char * psz_key -- key name * const char * psz_value -- key value *------------------------------------------------------------------------------ * return: TRUE -- key value writen to buffer *******************************************************************************/ int IniSetString(const char *psz_section, const char *psz_key, const char *psz_value) { char sz_value[M_MAX_VALUE_BUFFER_SIZE]; int i_oldvaluelen = 0; // lenght of old value int i_newvaluelen = 0; // lenght of new value long i = 0; long i_temp = 0; //DBGPRINT(( "\nSetString -- to search section: %s\n", psz_section )); if (IniSearchSection(psz_section) == FALSE) { //========================================================================== // section not found, we append the section and key value // at the end of buffer //========================================================================== memset(sz_value, 0, sizeof(sz_value)); sprintf(sz_value, "\n\n\n[%s]\n%s = %s\n\n\n", psz_section, psz_key, psz_value); i_temp = strlen(sz_value); strncpy(f_inifile.sz_filebuffer + f_inifile.i_filesize, sz_value, strlen(sz_value)); f_inifile.i_filesize += i_temp; f_inifile.b_bufferchanged = TRUE; return TRUE; } if (IniGetString(psz_section, psz_key, sz_value)) { //========================================================================== // section and key found, replace value //========================================================================== i_oldvaluelen = f_inifile.i_value_endpos - f_inifile.i_value_startpos + 1; i_newvaluelen = strlen(psz_value); //DBGPRINT(( "SetString -- before update, file size = %d\n", i_filesize )); if (i_newvaluelen > i_oldvaluelen) { // new value is longer than old value ************************************ // 1. get more space by moving content backward i_temp = i_newvaluelen - i_oldvaluelen; for (i = f_inifile.i_filesize - 1; i >= f_inifile.i_value_endpos; i--) { f_inifile.sz_filebuffer[i + i_temp] = f_inifile.sz_filebuffer[i]; } f_inifile.i_filesize += i_temp; f_inifile.sz_filebuffer[f_inifile.i_filesize + 1] = '\0'; // 2. write new value to buffer strncpy(f_inifile.sz_filebuffer + f_inifile.i_value_startpos, psz_value, strlen(psz_value)); } else if (i_newvaluelen < i_oldvaluelen) { // new value is shorter than old value *********************************** // 1. write new value to buffer strncpy(f_inifile.sz_filebuffer + f_inifile.i_value_startpos, psz_value, strlen(psz_value)); // 2. replace the following chars with spaces i_temp = i_oldvaluelen - i_newvaluelen; for (i = 0; i < i_temp; i++) { f_inifile.sz_filebuffer[f_inifile.i_value_endpos - i] = ' '; } } else { // same length *********************************************************** // just replace old value strncpy(f_inifile.sz_filebuffer + f_inifile.i_value_startpos, psz_value, strlen(psz_value)); } //DBGPRINT(( "SetString -- after update, file size = %d\n", f_inifile.i_filesize )); f_inifile.b_bufferchanged = TRUE; } else { //========================================================================== // key not found, we add key value by inserting a new line //========================================================================== // 1. make new line memset(sz_value, 0, sizeof(sz_value)); sprintf(sz_value, "\n%s = %s\n", psz_key, psz_value); i_temp = strlen(sz_value); // 2. move buffer for new line for (i = f_inifile.i_filesize; i >= f_inifile.i_sc_endpos; i--) { f_inifile.sz_filebuffer[i + i_temp] = f_inifile.sz_filebuffer[i]; } // 3. copy new line to buffer strncpy(f_inifile.sz_filebuffer + f_inifile.i_sc_endpos, sz_value, strlen(sz_value)); f_inifile.i_filesize += i_temp; f_inifile.b_bufferchanged = TRUE; } // search end position of content again IniSearchContentEnd(f_inifile.i_sc_startpos); return TRUE; }
void Service95 () { char pathname[256]; char regkey[20]; /* Win9x registry name */ SC_HANDLE schSCManager = 0; SC_HANDLE schService = 0; HKEY hkey = 0; DWORD rc, disposition; /* In Windows 95/98/Me, call RegisterServiceProcess in the Kernel */ /* This will prevent prime95 from terminating on logoff. */ if (isWindows95 ()) { HMODULE hlib; DWORD (__stdcall *proc)(DWORD, DWORD); hlib = LoadLibrary ("KERNEL32.DLL"); if (!hlib) { OutputStr (MAIN_THREAD_NUM, "Unable to load KERNEL32.DLL\n"); goto done; } proc = (DWORD (__stdcall *)(DWORD, DWORD)) GetProcAddress (hlib, "RegisterServiceProcess"); if (proc == NULL) OutputStr (MAIN_THREAD_NUM, "Unable to find RegisterServiceProcess\n"); else { if (WINDOWS95_SERVICE) rc = (*proc) (NULL, RSP_SIMPLE_SERVICE); else rc = (*proc) (NULL, RSP_UNREGISTER_SERVICE); if (!rc) OutputStr (MAIN_THREAD_NUM, "RegisterServiceProcess failed\n"); } FreeLibrary (hlib); } /* Now we deal with making the registry entries correct for proper starting */ /* or not starting of the service. */ /* Get pathname of executable */ GetModuleFileName (NULL, pathname, sizeof (pathname)); /* In Win95/98/Me, we create a registry entry for each -A command line value */ /* We used to do this in WinNT/2000/XP, but now we just delete these old */ /* registry entries. */ if (WINDOWS95_A_SWITCH < 0) strcpy (regkey, "Prime95"); else sprintf (regkey, "Prime95-%d", WINDOWS95_A_SWITCH); // For Windows 95/98/Me create a RunServices entry if (isWindows95 ()) { if (RegCreateKeyEx ( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition) != ERROR_SUCCESS) { OutputStr (MAIN_THREAD_NUM, "Can't create registry key.\n"); goto done; } /* Now create or delete an entry for prime95 */ if (WINDOWS95_SERVICE) { if (WINDOWS95_A_SWITCH >= 0) { char append[20]; sprintf (append, " -A%d", WINDOWS95_A_SWITCH); strcat (pathname, append); } rc = RegSetValueEx (hkey, regkey, 0, REG_SZ, (BYTE *) pathname, (DWORD) strlen (pathname) + 1); if (rc != ERROR_SUCCESS) { OutputStr (MAIN_THREAD_NUM, "Can't write registry value.\n"); goto done; } } else { rc = RegDeleteValue (hkey, regkey); if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND){ OutputStr (MAIN_THREAD_NUM, "Can't delete registry entry.\n"); goto done; } } RegCloseKey (hkey); hkey = 0; } // For Windows NT/2000/XP we call the service control manager to maintain the // services database. If we don't have administrator privileges (can't open // the service control manager) then simply create a registry entry to start // the program at logon. So, attempt to open the service control manager on // NULL = local machine, NULL = default database, all access required. // Also Vista won't let services interact with the desktop, so we can't run // as a service. else if (isWindowsVista () || ! (schSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS))) { if (RegCreateKeyEx ( HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition) != ERROR_SUCCESS) { OutputStr (MAIN_THREAD_NUM, "Can't create registry key.\n"); goto done; } /* Now create or delete an entry for prime95 */ if (WINDOWS95_SERVICE) { if (WINDOWS95_A_SWITCH >= 0) { char append[20]; sprintf (append, " -A%d", WINDOWS95_A_SWITCH); strcat (pathname, append); } rc = RegSetValueEx (hkey, regkey, 0, REG_SZ, (BYTE *) pathname, (DWORD) strlen (pathname) + 1); if (rc != ERROR_SUCCESS) { OutputStr (MAIN_THREAD_NUM, "Can't write registry value.\n"); goto done; } } else { rc = RegDeleteValue (hkey, regkey); if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND){ OutputStr (MAIN_THREAD_NUM, "Can't delete registry entry.\n"); goto done; } } RegCloseKey (hkey); hkey = 0; } // Make the necessary NT/2000/XP service control changes else { char servicename[80]; char displayname[80]; // Create the Windows NT service name and display name IniGetString (LOCALINI_FILE, "ServiceName", servicename, sizeof (servicename), NULL); if (servicename[0] == 0) { if (WINDOWS95_A_SWITCH < 0) strcpy (servicename, "Prime95 Service"); else sprintf (servicename, "Prime95 Service-%d", WINDOWS95_A_SWITCH); } IniGetString (LOCALINI_FILE, "DisplayName", displayname, sizeof (displayname), servicename); // Create the service entry if (WINDOWS95_SERVICE) { schService = CreateService ( schSCManager, // SCManager database servicename, // name of service displayname, // display name SERVICE_ALL_ACCESS, // desired access SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_AUTO_START, // start type SERVICE_ERROR_NORMAL, // error control type pathname, // service's binary NULL, // no load ordering NULL, // no tag identifier NULL, // no dependencies NULL, // LocalSystem account NULL); // no password if (!schService) { if (GetLastError () != ERROR_SERVICE_EXISTS) OutputStr (MAIN_THREAD_NUM, "Error creating service.\n"); goto done; } // Set description for Win2K and later if (isWindows2000 ()) { SERVICE_DESCRIPTION svc_desc; svc_desc.lpDescription = "GIMPS client to find large prime numbers"; ChangeServiceConfig2 ( schService, SERVICE_CONFIG_DESCRIPTION, &svc_desc); } } // Remove the service entry else { schService = OpenService ( schSCManager, servicename, SERVICE_ALL_ACCESS); if (!schService) { if (GetLastError () != ERROR_SERVICE_DOES_NOT_EXIST) OutputStr (MAIN_THREAD_NUM, "Error opening service.\n"); goto done; } if (! DeleteService (schService)) { OutputStr (MAIN_THREAD_NUM, "Error deleting service.\n"); goto done; } } /* Delete the old-style (version 21) Run entry for the current user */ if (RegCreateKeyEx ( HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition) == ERROR_SUCCESS) { RegDeleteValue (hkey, regkey); RegCloseKey (hkey); hkey = 0; } } /* For Windows NT/2000/XP delete the old-style (version 21) Run entry for */ /* the local machine */ if (! isWindows95 ()) { if (RegCreateKeyEx ( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition) == ERROR_SUCCESS) { RegDeleteValue (hkey, regkey); RegCloseKey (hkey); hkey = 0; } } /* Now delete any shortcuts to the program. We do this so that as users */ /* upgrade from version 20 and try this menu choice they do not end up with */ /* both a registry entry and a StartUp menu shortcut. */ if (WINDOWS95_SERVICE) { char buf[256]; DWORD type; DWORD bufsize = sizeof (buf); if (RegCreateKeyEx ( HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition) != ERROR_SUCCESS) goto done; if (RegQueryValueEx (hkey, "Startup", NULL, &type, (BYTE *) buf, &bufsize) == ERROR_SUCCESS && type == REG_SZ) { strcat (buf, "\\prime95.lnk"); _unlink (buf); } } // Cleanup and return done: if (schService) CloseServiceHandle (schService); if (schSCManager) CloseServiceHandle (schSCManager); if (hkey) RegCloseKey (hkey); }