static void InstallService() { SC_HANDLE hSCManager; int status; RemoveService(); hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE); if (hSCManager) { SC_HANDLE hService; char *pgm; char *cmd; static const char* multi_opt="--multi"; static const char* server_opt="--server"; static const char* data_opt=""; char *opts = GetMulti() ? (GetContextSwitching() ? "--multi" : "--server") : ""; SERVICE_DESCRIPTION sd; LPTSTR description=(LPTSTR)malloc(4096); if (GetMulti()) { if (GetContextSwitching()) { opts=multi_opt; wsprintf(description,TEXT("MDSplus data service listening on port %s.\nPermits multiple connections each with own tdi and tree context\n"), GetPortname()); } else { opts=server_opt; wsprintf(description,TEXT("MDSplus data service listening on port %s.\nPermits multiple connections with shared tdi and tree context\n"), GetPortname()); } } else { opts=data_opt; wsprintf(description,TEXT("MDSplus data service listening on port %s.\nEach connections will spawn a private server.\n"), GetPortname()); } sd.lpDescription=description; _get_pgmptr(&pgm); cmd = (char *)malloc(strlen(pgm)+strlen(GetPortname())+strlen(GetHostfile())+100); sprintf(cmd,"%s --port=%s --hostfile=\"%s\" %s",pgm,GetPortname(),GetHostfile(),opts); hService = CreateService(hSCManager, ServiceName(1), ServiceName(0), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, cmd, NULL, NULL, NULL, NULL, NULL); if (hService == NULL) status = GetLastError(); else { ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,&sd); if (GetMulti()) { SERVICE_FAILURE_ACTIONS sfa; SC_ACTION actions[] = {{SC_ACTION_RESTART,5000}}; sfa.dwResetPeriod = INFINITE; sfa.lpRebootMsg = NULL; sfa.lpCommand = NULL; sfa.cActions = 1; sfa.lpsaActions=actions; status = ChangeServiceConfig2(hService,SERVICE_CONFIG_FAILURE_ACTIONS, &sfa); status = GetLastError(); } } free(description); free(cmd); if (hService) CloseServiceHandle(hService); CloseServiceHandle(hSCManager); } }
CRegEntry::operator LPTSTR() { /* If caching is disabled, refresh the entries */ REGENTRY_REFRESH_IF_NOCACHE assert(__bConvertable); // Check for conversion implementation ForceStr(); switch (iType) { case REG_DWORD: _stprintf(lpszStr, _T("%lu"), dwDWORD); break; case REG_MULTI_SZ: GetMulti(lpszStr); break; case REG_BINARY: { _tcsncpy(lpszStr, (const _TCHAR*)&vBytes[0], vBytes.size()); lpszStr[vBytes.size()] = 0; } break; } return lpszStr; }
static char *ServiceName(int generic) { char *name=strcpy((char *)malloc(512),"MDSplus "); if (!generic) strcat(name,GetMulti() ? "Action Server - Port " : "Data Server - Port "); strcat(name,GetPortname()); return name; }
static int ServiceMain(int argc, char **argv) { SOCKET s; int status; static struct sockaddr_in sin; int tablesize = FD_SETSIZE; extern fd_set FdActive(); struct timeval timeout = {1,0}; int error_count=0; fd_set readfds; fd_set fdactive; RedirectOutput(); InitializeService(); SetThisServiceStatus(SERVICE_RUNNING,0); if (GetMulti()) { IoRoutines *io; io=LoadIo(GetProtocol()); if (io && io->listen) io->listen(argc,argv); else { fprintf(stderr,"Protocol %s does not support servers\n",GetProtocol()); return 1; } return 0; } else { s = socket(AF_INET, SOCK_STREAM, 0); if (s == -1) { printf("Error getting Connection Socket\n"); exit(1); } memset(&sin,0,sizeof(sin)); sin.sin_port = GetPort(); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; status = bind(s, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)); if (status < 0) { perror("Error binding to service\n"); exit(1); } status = listen(s,128); if (status < 0) { perror("Error listen on port\n"); exit(1); } FD_ZERO(&fdactive); FD_SET(s,&fdactive); for (readfds=fdactive;!shut;readfds=fdactive) { int sstatus; if ((sstatus = select(tablesize, &readfds, 0, 0, &timeout)) != SOCKET_ERROR) { error_count=0; if (FD_ISSET(s, &readfds)){ int len = sizeof(struct sockaddr_in); SOCKET sock = accept(s, (struct sockaddr *)&sin, &len); SpawnWorker(sock); } } else { error_count++; perror("error in main select"); fprintf(stderr,"Error count=%d\n",error_count); fflush(stderr); if (error_count > 100) { fprintf(stderr,"Error count exceeded, shutting down\n"); shut=1; } } } shutdown(s,2); return 1; } }
CStringA CUnicodeUtils::GetUTF8(const CStringW& string) { return GetMulti(string,CP_UTF8); }