#include "dicsrv.h" BOOL Unload(HWND hDlg) { HWND hConnectionsCtrl; HTREEITEM hItem; TV_ITEM item; pCLIENTDATA pClientData; int iRet; if (ghServer) { if (L_DicomGetClientCount(ghServer) > 0) { iRet = MessageBox(hDlg, "There are clients connected!\nDo you wish to shutdown?", "Warning", MB_YESNO); if (iRet != IDYES) return FALSE; } KillTimer(hDlg, CLIENTS_TIMER_ID); hConnectionsCtrl = GetDlgItem(hDlg, IDC_TREE3); hItem = TreeView_GetRoot(hConnectionsCtrl); while (hItem) { memset(&item, 0, sizeof(TV_ITEM)); item.mask = TVIF_HANDLE | TVIF_PARAM; item.hItem = hItem; TreeView_GetItem(hConnectionsCtrl, &item); pClientData = (pCLIENTDATA) item.lParam; L_DicomCloseForced(pClientData->hClient, TRUE); L_DicomFreeNet(pClientData->hClient); free(pClientData); hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); } LogEvent(hDlg, gszServerAETitle, "Server ShutDown"); L_DicomFreeNet(ghServer); ghServer = NULL; L_DicomShutDown(); } if (gpMoveMatches) { free(gpMoveMatches); gpMoveMatches = NULL; } if (ghMovePDU) { L_DicomFreeAssociate(ghMovePDU); ghMovePDU = NULL; } if (ghClient) { L_DicomFreeNet(ghClient); ghClient = NULL; } if (gBitmap.Flags.Allocated) L_FreeBitmap(&gBitmap); ImageList_Destroy(ghImageList); ghImageList = NULL; if(ghServerDS) { L_DicomFreeDS(ghServerDS); ghServerDS = NULL; } if (!SaveServerSettings(hDlg)) { MessageBox(hDlg, "Failed to save the server settings.", "Error", MB_OK | MB_ICONERROR); } if (!SaveClientsList(hDlg)) { MessageBox(hDlg, "Failed to save the list of clients.", "Error", MB_OK | MB_ICONERROR); } if (!SaveEventsLog(hDlg)) { MessageBox(hDlg, "Failed to save the events log file.", "Error", MB_OK | MB_ICONERROR); } return TRUE; } BOOL SaveSetting(HANDLE hFile, LPCSTR pszSetting) { DWORD dwBytesWritten; UINT uSize; uSize = lstrlen(pszSetting); if (!WriteFile(hFile, &uSize, sizeof(uSize), &dwBytesWritten, NULL)) { CloseHandle(hFile); return FALSE; } if (!WriteFile(hFile, pszSetting, uSize, &dwBytesWritten, NULL)) { CloseHandle(hFile); return FALSE; } return TRUE; } BOOL SaveServerSettings() { HANDLE hFile; TCHAR szFile[MAX_PATH]; TCHAR szBuffer[16]; if (!gbSaveServerSettings) { return TRUE; } lstrcpy(szFile, gszApplicationFolder); lstrcat(szFile, SERVER_SETTINGS_FILENAME); hFile = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { return FALSE; } if (!SaveSetting(hFile, gszConnectionString)) return FALSE; if (!SaveSetting(hFile, gszImagesFolder)) return FALSE; if (!SaveSetting(hFile, gszTempFilesFolder)) return FALSE; if (!SaveSetting(hFile, gszServerAETitle)) return FALSE; itoa(guServerPort, szBuffer, 10); if (!SaveSetting(hFile, szBuffer)) return FALSE; itoa(giTimeOut, szBuffer, 10); if (!SaveSetting(hFile, szBuffer)) return FALSE; itoa(giMaxClients, szBuffer, 10); if (!SaveSetting(hFile, szBuffer)) return FALSE; lstrcpy(szBuffer, gbGenerateEventsLogFile ? "1" : "0"); if (!SaveSetting(hFile, szBuffer)) return FALSE; lstrcpy(szBuffer, gbSaveReceivedCSs ? "1" : "0"); if (!SaveSetting(hFile, szBuffer)) return FALSE; lstrcpy(szBuffer, gbSaveReceivedDSs ? "1" : "0"); if (!SaveSetting(hFile, szBuffer)) return FALSE; lstrcpy(szBuffer, gbSaveSentDSs ? "1" : "0"); if (!SaveSetting(hFile, szBuffer)) return FALSE; if (!SaveSetting(hFile, gszLogFolder)) return FALSE; CloseHandle(hFile); return TRUE; } BOOL SaveClientsList(HWND hDlg) { char szBuffer[128]; char szFile[MAX_PATH]; FILE* pFile; HWND hCtl; int iItemCount, i; LV_ITEM lvi; char szAETitle[20]; char szIPAddress[32]; char szPortNumber[8]; char szTimeout[16]; wsprintf(szFile, TEXT("%s%s"), gszApplicationFolder, CLIENTS_LIST_FILENAME); if ((pFile = fopen(szFile, "w")) == NULL) { return FALSE; } hCtl = GetDlgItem(hDlg, IDC_LIST1); lvi.mask = LVIF_TEXT; lvi.pszText = szBuffer; lvi.cchTextMax = sizeof(szBuffer) / sizeof(szBuffer[0]); iItemCount = ListView_GetItemCount(hCtl); for (i = 0; i < iItemCount; i++) { lvi.iItem = i; lstrcpy(szAETitle, ""); lstrcpy(szIPAddress, ""); lstrcpy(szPortNumber, ""); lstrcpy(szTimeout, ""); lvi.iSubItem = 0; if (ListView_GetItem(hCtl, &lvi)) { lstrcpy(szAETitle, szBuffer); lvi.iSubItem = 1; if (ListView_GetItem(hCtl, &lvi)) { lstrcpy(szIPAddress, szBuffer); } lvi.iSubItem = 2; if (ListView_GetItem(hCtl, &lvi)) { lstrcpy(szPortNumber, szBuffer); } lvi.iSubItem = 3; if (ListView_GetItem(hCtl, &lvi)) { lstrcpy(szTimeout, szBuffer); } } wsprintf(szBuffer, TEXT("%s,%s,%s,%s\n"), szAETitle, szIPAddress, szPortNumber, szTimeout); if (fputs(szBuffer, pFile) == EOF) { fclose(pFile); return FALSE; } } fclose(pFile); return TRUE; } BOOL SaveEventsLog(HWND hDlg) { char szBuffer[512]; char szFile[MAX_PATH]; FILE* pFile; HWND hCtl; int iItemCount, i; LV_ITEM lvi; char szDate[80]; char szClient[20]; char szEventDesc[256]; wsprintf(szFile, TEXT("%s%s"), gszApplicationFolder, EVENTS_LOG_FILENAME); if ((pFile = fopen(szFile, "w")) == NULL) { return FALSE; } hCtl = GetDlgItem(hDlg, IDC_LIST2); lvi.mask = LVIF_TEXT; lvi.pszText = szBuffer; lvi.cchTextMax = sizeof(szBuffer) / sizeof(szBuffer[0]); iItemCount = ListView_GetItemCount(hCtl); for (i = 0; i < iItemCount; i++) { lvi.iItem = i; lstrcpy(szDate, ""); lstrcpy(szClient, ""); lstrcpy(szEventDesc, ""); lvi.iSubItem = 0; if (ListView_GetItem(hCtl, &lvi)) { lstrcpy(szDate, szBuffer); lvi.iSubItem = 1; if (ListView_GetItem(hCtl, &lvi)) lstrcpy(szClient, szBuffer); lvi.iSubItem = 2; if (ListView_GetItem(hCtl, &lvi)) lstrcpy(szEventDesc, szBuffer); } wsprintf(szBuffer, TEXT("%s,%s,%s\n"), szDate, szClient, szEventDesc); if (fputs(szBuffer, pFile) == EOF) { fclose(pFile); return FALSE; } } fclose(pFile); return TRUE; }