#include "DicSrv.h" #include "Shlobj.h" #include "ODBCINST.h" #ifndef INVALID_FILE_ATTRIBUTES #define INVALID_FILE_ATTRIBUTES -1 #endif L_VOID LogEvent(HWND hDlg, L_CHAR L_FAR* pszUser, L_CHAR L_FAR *pszEvent) { SYSTEMTIME st; L_CHAR L_FAR szDate[180]; LV_ITEM lvi; L_INT nPos; GetLocalTime(&st); wsprintf(szDate, "%ld/%ld/%ld %ld:%ld:%ld %s", st.wMonth, st.wDay, st.wYear, (st.wHour<=12)?st.wHour:st.wHour-12, st.wMinute, st.wSecond, (st.wHour>=12)?"PM":"AM"); memset(&lvi, 0, sizeof(LV_ITEM)); lvi.mask = LVIF_TEXT; lvi.iItem = ListView_GetItemCount(GetDlgItem(hDlg, IDC_LIST2)); lvi.pszText = szDate; nPos = ListView_InsertItem(GetDlgItem(hDlg, IDC_LIST2), &lvi); if(nPos!=-1) { lvi.mask = LVIF_TEXT; lvi.iItem = nPos; lvi.iSubItem = 1; lvi.pszText = pszUser; ListView_SetItem(GetDlgItem(hDlg, IDC_LIST2), &lvi); lvi.iSubItem = 2; lvi.pszText = pszEvent; ListView_SetItem(GetDlgItem(hDlg, IDC_LIST2), &lvi); } } L_VOID MatchAspectRatio (LPRECT pRect, L_INT nImageWidth, L_INT nImageHeight) { L_INT cxRect, cyRect; L_INT nRectWidth, nRectHeight; nRectWidth = pRect->right - pRect->left; nRectHeight = pRect->bottom - pRect->top; cyRect = nRectHeight; cxRect = MulDiv (cyRect, nImageWidth, nImageHeight); if (cxRect > nRectWidth) { cxRect = nRectWidth; cyRect = MulDiv (cxRect, nImageHeight, nImageWidth); pRect->top = (pRect->bottom - pRect->top - cyRect) / 2; } else pRect->left = (pRect->right - pRect->left - cxRect) / 2; pRect->bottom = pRect->top + cyRect; pRect->right = pRect->left + cxRect; } BOOL FindClient(HWND hDlg, LPCSTR pszIPAddress, LPCSTR pszAETitle, int* piUserListIndex) { HWND hUserListCtrl = GetDlgItem(hDlg, IDC_LIST1); char szIPAddress[64], szAETitle[20]; int iCount, i; iCount = ListView_GetItemCount(hUserListCtrl); for (i = 0; i < iCount; i++) { ListView_GetItemText(hUserListCtrl, i, 1, szIPAddress, sizeof(szIPAddress) / sizeof(szIPAddress[0])); if (lstrcmp(pszIPAddress, szIPAddress) == 0) { if (pszAETitle) { ListView_GetItemText(hUserListCtrl, i, 0, szAETitle, sizeof(szAETitle) / sizeof(szAETitle[0])); if (lstrcmpi(pszAETitle, szAETitle) == 0) { if (piUserListIndex) *piUserListIndex = i; return TRUE; } } else { if (piUserListIndex) *piUserListIndex = i; return TRUE; } } } return FALSE; } VOID SetClientInfo(HWND hDlg, HDICOMNET hClient, LPCSTR pszIPAddress, LPCSTR pszAETitle) { HWND hConnectionsCtrl, hUserListCtrl; HTREEITEM hItem; TV_ITEM item; pCLIENTDATA pClientData; int iUserListIndex; char szTimeout[64]; char szText[64]; hConnectionsCtrl = GetDlgItem(hDlg, IDC_TREE3); hUserListCtrl = GetDlgItem(hDlg, IDC_LIST1); hItem = TreeView_GetRoot(hConnectionsCtrl); while (hItem) { item.mask = TVIF_HANDLE | TVIF_PARAM; item.hItem = hItem; TreeView_GetItem(hConnectionsCtrl, &item); pClientData = (pCLIENTDATA) item.lParam; if (pClientData->hClient == hClient) { if (FindClient(hDlg, pszIPAddress, pszAETitle, &iUserListIndex)) { ListView_GetItemText(hUserListCtrl, iUserListIndex, 0, pClientData->szAETitle, sizeof(pClientData->szAETitle) / sizeof(pClientData->szAETitle[0])); ListView_GetItemText(hUserListCtrl, iUserListIndex, 3, szTimeout, sizeof(szTimeout) / sizeof(szTimeout[0])); pClientData->uTimeout = atoi(szTimeout); wsprintf(szText, "Remote AE: %s", pClientData->szAETitle); item.mask = TVIF_HANDLE | TVIF_TEXT; item.hItem = TreeView_GetChild(hConnectionsCtrl, hItem); item.pszText = szText; TreeView_SetItem(hConnectionsCtrl, &item); } return; } hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); } } BOOL GetClientInfo(HWND hDlg, HDICOMNET hClient, LPSTR pszAETitle) { HWND hConnectionsCtrl; HTREEITEM hItem; TV_ITEM item; pCLIENTDATA pClientData; hConnectionsCtrl = GetDlgItem(hDlg, IDC_TREE3); hItem = TreeView_GetRoot(hConnectionsCtrl); while (hItem) { item.mask = TVIF_HANDLE | TVIF_PARAM; item.hItem = hItem; TreeView_GetItem(hConnectionsCtrl, &item); pClientData = (pCLIENTDATA) item.lParam; if (pClientData->hClient == hClient) { lstrcpy(pszAETitle, pClientData->szAETitle); return TRUE; } hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); } return FALSE; } VOID EnableClientTimeout(HWND hDlg, HDICOMNET hClient, BOOL bEnable) { HWND hConnectionsCtrl; HTREEITEM hItem; TV_ITEM item; pCLIENTDATA pClientData; hConnectionsCtrl = GetDlgItem(hDlg, IDC_TREE3); hItem = TreeView_GetRoot(hConnectionsCtrl); while (hItem) { item.mask = TVIF_HANDLE | TVIF_PARAM; item.hItem = hItem; TreeView_GetItem(hConnectionsCtrl, &item); pClientData = (pCLIENTDATA) item.lParam; if (pClientData->hClient == hClient) { pClientData->bEnableTimeout = bEnable && pClientData->uTimeout; pClientData->uSecondsElapsed = 0; return; } hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); } } BOOL SaveDataSet(HDICOMNET hClient, HDICOMDS hDataSet, LPSTR pszFilename) { pDICOMELEMENT pElement; L_CHAR* pszSOPClassUID, * pszSOPInstanceUID, * pszImplementationClassUID; L_CHAR* pszImplementationVersionName; HDICOMPDU hAssociate; L_UINT16 uRet; if (!hDataSet || !pszFilename) return FALSE; // File Meta Information Version SetElement(hDataSet, NULL, TAG_FILE_META_INFORMATION_VERSION, VR_OB, "01"); // Media Storage SOP Class UID pszSOPClassUID = NULL; pElement = L_DicomFindFirstElement(hDataSet, NULL, TAG_SOP_CLASS_UID, FALSE); if (pElement) { pszSOPClassUID = L_DicomGetStringValue(hDataSet, pElement, 0, 1); } SetElement(hDataSet, NULL, TAG_MEDIA_STORAGE_SOP_CLASS_UID, VR_UI, pszSOPClassUID); L_DicomFreeValue(hDataSet, pElement); // Media Storage SOP Instance UID pszSOPInstanceUID = NULL; pElement = L_DicomFindFirstElement(hDataSet, NULL, TAG_SOP_INSTANCE_UID, FALSE); if (pElement) { pszSOPInstanceUID = L_DicomGetStringValue(hDataSet, pElement, 0, 1); } SetElement(hDataSet, NULL, TAG_MEDIA_STORAGE_SOP_INSTANCE_UID, VR_UI, pszSOPInstanceUID); L_DicomFreeValue(hDataSet, pElement); hAssociate = L_DicomGetAssociate(hClient); // Implementation Class UID pszImplementationClassUID = NULL; if (hAssociate) { pszImplementationClassUID = L_DicomGetImplementClass(hAssociate); } SetElement(hDataSet, NULL, TAG_IMPLEMENTATION_CLASS_UID, VR_UI, pszImplementationClassUID); // Implementation Version Name pszImplementationVersionName = NULL; if (hAssociate) { pszImplementationVersionName = L_DicomGetImplementVersion(hAssociate); } SetElement(hDataSet, NULL, TAG_IMPLEMENTATION_VERSION_NAME, VR_SH, pszImplementationVersionName); uRet = L_DicomSaveDS(hDataSet, pszFilename, DS_METAHEADER_PRESENT | DS_GROUP_LENGTHS); return (uRet == DICOM_SUCCESS); } VOID SaveSet(HDICOMDS hSet, BOOL bDataSet, BOOL bReceived, LPSTR pszResultFile) { char szFileName[MAX_PATH]; LPSTR pszPrefix, pszChar; if (pszResultFile) lstrcpy(pszResultFile, ""); if (!hSet) return; if (!bDataSet && bReceived) // A CS received by the server? { if (!gbSaveReceivedCSs) return; } else if (bDataSet && bReceived) // A DS received by the server? { if (!gbSaveReceivedDSs) return; } else if (bDataSet && !bReceived) // A DS sent out by the server? { if (!gbSaveSentDSs) return; } else { return; } pszPrefix = bDataSet ? (bReceived ? "dsr" : "dss") : (bReceived ? "csr" : "css"); if (GetTempFileName(gszLogFolder, pszPrefix, 0, szFileName)) { DeleteFile(szFileName); pszChar = strrchr(szFileName, '.'); if (pszChar) { wsprintf(pszChar, "%s", ".dcm"); // Save the Command or Data Set L_DicomSaveDS(hSet, szFileName, 0); if (pszResultFile) { pszChar = strrchr(szFileName, '\\'); if (pszChar) lstrcpy(pszResultFile, pszChar + 1); } } } } VOID LastClientAction(HWND hDlg, HDICOMNET hClient, L_CHAR L_FAR *pszAction, L_BOOL bMessage) { HWND hConnectionsCtrl; HTREEITEM hItem; L_CHAR szTemp[1024]; TV_ITEM item; long lCount; 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); if (((pCLIENTDATA) item.lParam)->hClient == hClient) break; hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); } if (!hItem) return; hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_CHILD); hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); if (bMessage) { memset(&item, 0, sizeof(TV_ITEM)); item.mask = TVIF_HANDLE | TVIF_PARAM; item.hItem = hItem; TreeView_GetItem(hConnectionsCtrl, &item); item.lParam++; lCount = item.lParam; item.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_TEXT; item.pszText = szTemp; wsprintf(szTemp, "Number of Messages: %ld", lCount); TreeView_SetItem(hConnectionsCtrl, &item); } hItem = TreeView_GetNextItem(hConnectionsCtrl, hItem, TVGN_NEXT); item.mask = TVIF_HANDLE | TVIF_TEXT; item.hItem = hItem; item.pszText = szTemp; wsprintf(szTemp, "Last Action: %s", pszAction); TreeView_SetItem(hConnectionsCtrl, &item); } L_INT CopyElement(HWND hDlg, HDICOMDS hTargetDS, pDICOMELEMENT pDestElement, HDICOMDS hSourceDS, L_UINT32 lTag, BOOL bValueRequired) { L_CHAR szTag[200]; pDICOMELEMENT pTempTarget=NULL; pDICOMELEMENT pTempSource=NULL; pDICOMTAG pTag=NULL; L_CHAR L_FAR* pVal=NULL; L_UINT32 lSize; L_CHAR szOut[200]; pTempTarget = L_DicomInsertElement(hTargetDS, pDestElement, FALSE, lTag, 0, FALSE, 0); if(!pTempTarget) //already exists pTempTarget = L_DicomFindFirstElement(hTargetDS, pDestElement, lTag, TRUE); if(!pTempTarget) { pTag = L_DicomFindTag(lTag); if(pTag) lstrcpy(szTag, pTag->pszName); else wsprintf(szTag, "%X", lTag); if(gbImport) { wsprintf(szOut, "Tag - %s error creating/setting element for Target Data Set!", szTag); MessageBox(hDlg, szOut, "Error", MB_OK); } return(FAILURE); //failure } pTempSource = L_DicomFindFirstElement(hSourceDS, NULL, lTag, TRUE); if(pTempSource) { pTag = L_DicomFindTag(lTag); if(pTag) lstrcpy(szTag, pTag->pszName); else wsprintf(szTag, "%X", lTag); if(bValueRequired) { if(gbImport) { wsprintf(szOut, "Tag - %s does not exist in source Data Set!", szTag); MessageBox(hDlg, szOut, "Error", MB_OK); } } return(FAILURE); //failure } lSize = L_DicomGetConvertValue(hSourceDS, pTempSource, NULL); if(lSize) { pVal = (L_CHAR L_FAR*)GlobalAllocPtr(GMEM_MOVEABLE|GMEM_ZEROINIT, lSize*sizeof(L_CHAR)); if(pVal) lSize = L_DicomGetConvertValue(hSourceDS, pTempSource, pVal); } if(!pVal) { pTag = L_DicomFindTag(lTag); if(pTag) lstrcpy(szTag, pTag->pszName); else wsprintf(szTag, "%X", lTag); if(bValueRequired) { if(gbImport) { wsprintf(szOut, "Tag - %s does not contain a value in source Data Set!", szTag); MessageBox(hDlg, szOut, "Error", MB_OK); } } return(FAILURE); //failure } if(!L_DicomSetConvertValue(hTargetDS, pTempTarget, pVal, 1)) { GlobalFreePtr(pVal); pTag = L_DicomFindTag(lTag); if(pTag) lstrcpy(szTag, pTag->pszName); else wsprintf(szTag, "%X", lTag); if(bValueRequired) { if(gbImport) { wsprintf(szOut, "Tag - %s error creating/setting element for Target Data Set!", szTag); MessageBox(hDlg, szOut, "Error", MB_OK); } } return(FAILURE); //failure } GlobalFreePtr(pVal); return(SUCCESS); //successful } VOID SetElement(HDICOMDS hDataSet, pDICOMELEMENT pSiblingElement, L_UINT32 uTag, L_UINT16 uVR, L_CHAR* pszValue) { pDICOMELEMENT pElement; if (!hDataSet) return; pElement = L_DicomFindFirstElement(hDataSet, pSiblingElement, uTag, (pSiblingElement != NULL)); if (pElement == NULL) { pElement = L_DicomInsertElement(hDataSet, pSiblingElement, FALSE, uTag, uVR, FALSE, 0); } if (pElement && pszValue) { L_DicomSetConvertValue(hDataSet, pElement, pszValue, 1); } } VOID SetDateTimeElement(HDICOMDS hDataSet, pDICOMELEMENT pSiblingElement, TIMESTAMP_STRUCT* pTS, L_UINT32 uTag, BOOL bDateValue) { pDICOMELEMENT pElement; VALUEDATE vd; VALUETIME vt; if (!hDataSet) return; pElement = L_DicomFindFirstElement(hDataSet, pSiblingElement, uTag, (pSiblingElement != NULL)); if (pElement == NULL) { pElement = L_DicomInsertElement(hDataSet, pSiblingElement, FALSE, uTag, (L_UINT16) (bDateValue ? VR_DA : VR_TM), FALSE, 0); } if (pElement && pTS) { if (bDateValue) { vd.nYear = pTS->year; vd.nMonth = pTS->month; vd.nDay = pTS->day; L_DicomSetDateValue(hDataSet, pElement, &vd, 1); } else { vt.nHours = pTS->hour; vt.nMinutes = pTS->minute; vt.nSeconds = pTS->second; vt.nFractions = pTS->fraction; L_DicomSetTimeValue(hDataSet, pElement, &vt, 1); } } } // pszSelectedFolder must be at least MAX_PATH characters in size. BOOL GetFolder(HWND hWndOwner, LPTSTR pszSelectedFolder, L_INT nSize,LPTSTR pszTitle) { L_INT nRet; GETDIRECTORYDLGPARAMS DirectoryDlgParams ; ZeroMemory ( &DirectoryDlgParams, sizeof (GETDIRECTORYDLGPARAMS)) ; DirectoryDlgParams.uStructSize = sizeof (GETDIRECTORYDLGPARAMS); DirectoryDlgParams.pszDirectory = pszSelectedFolder; DirectoryDlgParams.pszTitle = pszTitle; DirectoryDlgParams.nBuffSize = nSize; DirectoryDlgParams.pszFilter = NULL ; DirectoryDlgParams.nFilterIndex = 1 ; nRet = L_DlgGetDirectory ( hWndOwner, &DirectoryDlgParams ) ; // Get the Border directory if ( nRet == SUCCESS_DLG_OK ) return TRUE; return FALSE; } VOID Trim(LPSTR pszString) { int iLength, i, j; if (!pszString) return; // Trim from left iLength = lstrlen(pszString); for (i = 0; i < iLength; i++) if (pszString[i] != ' ') break; if (i != 0) { j = 0; while (i < iLength) pszString[j++] = pszString[i++]; pszString[j] = '\0'; } // Trim from right iLength = lstrlen(pszString); for (i = iLength - 1; i >= 0; i--) if (pszString[i] != ' ') { pszString[i + 1] = '\0'; break; } } BOOL PrepareFolder(LPSTR pszFolder, LPCSTR pszAlternativeName) { int iLength; char szAltName[MAX_PATH]; Trim(pszFolder); iLength = lstrlen(pszFolder); if (iLength != 0 && pszFolder[iLength - 1] != '\\' && pszFolder[iLength - 1] != '/') { lstrcat(pszFolder, "\\"); } if (GetFileAttributes(pszFolder) == INVALID_FILE_ATTRIBUTES) // Doesn't exist? { if (!CreateDirectory(pszFolder, NULL)) // Failed to create the folder? { // Use the alternative name if supplied if (pszAlternativeName) { lstrcpyn(szAltName, pszAlternativeName, sizeof(szAltName) / sizeof(szAltName[0])); Trim(szAltName); if (lstrlen(szAltName)) { wsprintf(pszFolder, TEXT("%s%s"), gszApplicationFolder, szAltName); iLength = lstrlen(pszFolder); if (pszFolder[iLength - 1] != '\\' && pszFolder[iLength - 1] != '/') { lstrcat(pszFolder, "\\"); } if (GetFileAttributes(pszFolder) == INVALID_FILE_ATTRIBUTES) // Doesn't exist? { if (!CreateDirectory(pszFolder, NULL)) { return FALSE; } } } else { return FALSE; } } else { return FALSE; } } } return TRUE; } BOOL GetFile(LPCTSTR pszFilter, LPCTSTR pszTitle, LPSTR pszFile, DWORD dwSize) { OPENFILENAME ofn; // Initialize the OPENFILENAME structure ZeroMemory(&ofn, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.lpstrFilter = pszFilter; ofn.nFilterIndex = 1; ofn.lpstrFile = pszFile; ofn.nMaxFile = dwSize; ofn.lpstrTitle = pszTitle; ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; // Display the Open dialog box if (GetOpenFileName(&ofn)) { return TRUE; } return FALSE; } VOID EmptyDatabase() { DBHANDLES db; PATIENTSRECORDSET PatientsRS; STUDIESRECORDSET StudiesRS; SERIESRECORDSET SeriesRS; IMAGESRECORDSET ImagesRS; if (!OpenDatabaseConnection(&db)) { MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } // Delete all the records in the Patients table if (!OpenPatientsRecordset(db.hDbc, &PatientsRS, "SELECT * FROM Patients", FALSE)) { CloseDatabaseConnection(&db); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } while (!PatientsRS.bEOF) { DeleteRecord(&PatientsRS); if (!GetNextRecord(&PatientsRS)) { WrapUpDatabase(&db, &PatientsRS, NULL, NULL, NULL); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } } CloseRecordset(&PatientsRS); // Delete all the records in the Studies table if (!OpenStudiesRecordset(db.hDbc, &StudiesRS, "SELECT * FROM Studies", FALSE)) { CloseDatabaseConnection(&db); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } while (!StudiesRS.bEOF) { DeleteRecord(&StudiesRS); if (!GetNextRecord(&StudiesRS)) { WrapUpDatabase(&db, &StudiesRS, NULL, NULL, NULL); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } } CloseRecordset(&StudiesRS); // Delete all the records in the Series table if (!OpenSeriesRecordset(db.hDbc, &SeriesRS, "SELECT * FROM Series", FALSE)) { CloseDatabaseConnection(&db); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } while (!SeriesRS.bEOF) { DeleteRecord(&SeriesRS); if (!GetNextRecord(&SeriesRS)) { WrapUpDatabase(&db, &SeriesRS, NULL, NULL, NULL); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } } CloseRecordset(&SeriesRS); // Delete all the records in the Images table if (!OpenImagesRecordset(db.hDbc, &ImagesRS, "SELECT * FROM Images", FALSE)) { CloseDatabaseConnection(&db); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } while (!ImagesRS.bEOF) { DeleteRecord(&ImagesRS); if (!GetNextRecord(&ImagesRS)) { WrapUpDatabase(&db, &ImagesRS, NULL, NULL, NULL); MessageBox(NULL, TEXT("Database error."), TEXT("Error"), MB_OK | MB_ICONERROR); return; } } CloseRecordset(&ImagesRS); CloseDatabaseConnection(&db); } BOOL GetNewFilename(LPSTR pszExistingDicomFile, LPSTR pszNewFilename) { HDICOMDS hDS; pDICOMELEMENT pElement; L_CHAR* pszSOPInstanceUID; hDS = L_DicomCreateDS(gszTempFilesFolder); if (L_DicomLoadDS(hDS, pszExistingDicomFile, DS_LOAD_CLOSE) == DICOM_SUCCESS) { pElement = L_DicomFindFirstElement(hDS, NULL, TAG_SOP_INSTANCE_UID, FALSE); if (pElement) { pszSOPInstanceUID = L_DicomGetStringValue(hDS, pElement, 0, 1); if (pszSOPInstanceUID) { wsprintf(pszNewFilename, TEXT("%s%s.dcm"), gszImagesFolder, pszSOPInstanceUID); L_DicomFreeDS(hDS); return TRUE; } } } L_DicomFreeDS(hDS); return FALSE; } BOOL PrepareDatabase(LPSTR pszMdbFolder) { char szNewMdbFile[MAX_PATH]; char szLeadToolsFolder[MAX_PATH]; char szExistingMdbFile[MAX_PATH]; char ExistingDicomFiles[][MAX_PATH] = SAMPLE_DICOM_IMAGES; char NewDicomFiles[sizeof(ExistingDicomFiles) / sizeof(ExistingDicomFiles[0])][MAX_PATH]; char szDicomFile[MAX_PATH]; char szSelectedFolder[MAX_PATH]; int iSampleDicomImagesCount = sizeof(ExistingDicomFiles) / sizeof(ExistingDicomFiles[0]); WIN32_FIND_DATA FileData; HANDLE hFind; char* pszMessage; int iLength, i, j; if (!PrepareFolder(pszMdbFolder, NULL)) { MessageBox(NULL, TEXT("Failed to create the folder which the database file should be copied to."), TEXT("DICOM Server Demo"), MB_OK | MB_ICONSTOP); return FALSE; } wsprintf(szNewMdbFile, TEXT("%s%s"), pszMdbFolder, MDB_FILENAME); // At first, we will assume that the application is running from the "Bin" subfolder of // the folder where LEADTOOLS is installed. lstrcpy(szLeadToolsFolder, gszApplicationFolder); iLength = lstrlen(szLeadToolsFolder); for (i = iLength - 2; i >= 0; i--) if (szLeadToolsFolder[i] == '\\') { szLeadToolsFolder[i + 1] = '\0'; break; } // Locate the database file wsprintf(szExistingMdbFile, TEXT("%sImages\\%s"), szLeadToolsFolder, MDB_FILENAME); hFind = FindFirstFile(szExistingMdbFile, &FileData); if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); } else { // Prompt the user to locate the file pszMessage = TEXT("Failed to locate the database file (MDB) used by this demo. Do you " \ "want to locate it yourself?\n\n" \ "Note: A sample MDB file is located under the \"Images\" directory " \ "created by LEADTOOLS.\n" \ "If the installation path for LEADTOOLS is for example:\n" \ "\"C:\\LEAD Technologies, Inc\\LEADTOOLS\"\n" \ "then the path for the MDB file will be:\n" \ "\"C:\\LEAD Technologies, Inc\\LEADTOOLS\\Images\\" MDB_FILENAME "\"."); if (MessageBox(NULL, pszMessage, TEXT("DICOM Server Demo"), MB_YESNO) != IDYES) { return FALSE; } lstrcpy(szExistingMdbFile, MDB_FILENAME); if (!GetFile(TEXT("Microsoft Access Databases (*.mdb)\0*.mdb\0All Files (*.*)\0*.*\0"), TEXT("Locate MDB File"), szExistingMdbFile, sizeof(szExistingMdbFile) / sizeof(szExistingMdbFile[0]))) { return FALSE; } } // Copy the existing databse file if (!CopyFile(szExistingMdbFile, szNewMdbFile, FALSE)) { MessageBox(NULL, TEXT("Failed to copy the database file."), TEXT("DICOM Server Demo"), MB_OK | MB_ICONSTOP); return FALSE; } // The connection string wsprintf(gszConnectionString, "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=%s", szNewMdbFile); // Now we need to copy the sample DICOM images to the DICOM images folder. Again, we will // assume that the application is running from the "Bin" subfolder of the folder where // LEADTOOLS is installed. wsprintf(szDicomFile, TEXT("%sImages\\%s"), szLeadToolsFolder, ExistingDicomFiles[0]); hFind = FindFirstFile(szDicomFile, &FileData); if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); for (i = 0; i < iSampleDicomImagesCount; i++) { wsprintf(szDicomFile, TEXT("%sImages\\%s"), szLeadToolsFolder, ExistingDicomFiles[i]); lstrcpy(ExistingDicomFiles[i], szDicomFile); } } else { // Prompt the user to specify the location of the sample DICOM images pszMessage = TEXT("Failed to locate the sample DICOM images shipped with this demo. " \ "Do you want to locate them yourself?\nIf you press No, the demo " \ "will start with an empty database.\n\n" \ "Note: The sample DICOM images, which ship with LEADTOOLS, are " \ "located under the \"Images\" directory created by LEADTOOLS.\n" \ "If the installation path for LEADTOOLS is for example:\n" \ "\"C:\\LEAD Technologies, Inc\\LEADTOOLS\"\n" \ "then the path for the sample images will be:\n" \ "\"C:\\LEAD Technologies, Inc\\LEADTOOLS\\Images\"."); if (MessageBox(NULL, pszMessage, TEXT("DICOM Server Demo"), MB_YESNO) != IDYES) { EmptyDatabase(); return TRUE; } else { if (GetFolder(NULL, szSelectedFolder,sizeof(szSelectedFolder), TEXT("Specify the folder of the sample DICOM images:"))) { for (i = 0; i < iSampleDicomImagesCount; i++) { wsprintf(szDicomFile, TEXT("%s\\%s"), szSelectedFolder, ExistingDicomFiles[i]); lstrcpy(ExistingDicomFiles[i], szDicomFile); } } else { EmptyDatabase(); return TRUE; } } } for (i = 0; i < iSampleDicomImagesCount; i++) { if (!GetNewFilename(ExistingDicomFiles[i], NewDicomFiles[i]) || !CopyFile(ExistingDicomFiles[i], NewDicomFiles[i], FALSE)) { for (j = 0; j < i; j++) { DeleteFile(NewDicomFiles[j]); } pszMessage = TEXT("Failed to copy the sample DICOM images. The demo will start " \ "with an empty database."); MessageBox(NULL, pszMessage, TEXT("DICOM Server Demo"), MB_OK); EmptyDatabase(); return TRUE; } } return TRUE; } BOOL GetTreeViewItemData(HWND hTreeCtrl, HTREEITEM hItem, DWORD* pdwItemData) { TV_ITEM item; if (!pdwItemData) return FALSE; *pdwItemData = 0; item.mask = TVIF_PARAM; item.hItem = hItem; if (!TreeView_GetItem(hTreeCtrl, &item)) return FALSE; *pdwItemData = (DWORD) item.lParam; return TRUE; } // To get the certificate names L_VOID GetCertName(char *pszFileName, char pszCACertName[]) { char szpath[_MAX_PATH]; char szdrive[_MAX_PATH]; char szdir[_MAX_PATH]; GetModuleFileName(NULL, szpath, _MAX_PATH); _splitpath(szpath, szdrive, szdir, NULL, NULL ); wsprintf(pszCACertName, "%s%s%s",szdrive,szdir,pszFileName); } // To get the certificate private key password BOOL CALLBACK PrivateKeyPasswordDlgProc(HWND hDlg, UINT uiMessage, WPARAM wParam, LPARAM lParam) { static LPSTR pszPassword = NULL; switch (uiMessage) { case WM_INITDIALOG: pszPassword = (LPSTR)lParam; SetDlgItemText(hDlg, IDC_PASSWORD, "test"); return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDCANCEL: EndDialog(hDlg, IDCANCEL); return TRUE; case IDOK: GetDlgItemText(hDlg, IDC_PASSWORD, pszPassword, 1024); EndDialog(hDlg, IDOK); return TRUE; } } return FALSE; } L_INT L_EXPORT EXT_CALLBACK exGetPrivateKeyPassword(HDICOMNET hNet, L_CHAR *pszPassword, L_INT nSize, L_INT rwFlag, L_VOID *pUserData) { INT_PTR nRet; char szPassword[1024]; int nLenPassword = 0; // This callback can get called multiple times when decoding a private key password // We only prompt the first time if (!gbValidPrivateKeyPassword) { gbValidPrivateKeyPassword = TRUE; nRet = DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_PRIVATE_KEY_PASSWORD_DIALOG), ghWndParent, PrivateKeyPasswordDlgProc, (LPARAM)szPassword); if (nRet == IDOK) { // Copy the password to the global password variable // We will clear this later if the password is incorrect strcpy(gszPrivateKeyPassword, szPassword); } else { DWORD dRet = GetLastError(); } } strcpy(pszPassword, gszPrivateKeyPassword); nLenPassword = strlen(gszPrivateKeyPassword); return nLenPassword; } L_VOID SetCallbacksPrivateKey(HDICOMNET hNet) { DICOMNETCALLBACKEXT cb; memset(&cb, 0, sizeof(cb)); cb.uStructSize = sizeof(cb); cb.pfnPrivateKeyPassword = exGetPrivateKeyPassword; L_DicomSetCallbackExt(hNet, &cb); } L_INT GetServerPrivateKeyPassword(HWND hWndParent) { L_INT nRet = DICOM_SUCCESS; HDICOMNET hDicomNet = NULL; char szServerCertName[_MAX_PATH]; ghWndParent = hWndParent; if (!gbValidPrivateKeyPassword) { hDicomNet = L_DicomCreateNetExt(gszTempFilesFolder, DICOM_SECURE_TLS, NULL); SetCallbacksPrivateKey(hDicomNet); GetCertName(SERVER_CERT_NAME, szServerCertName); nRet = L_DicomSetClientCertificateTLS(hDicomNet, szServerCertName, L_TLS_FILETYPE_PEM, NULL); if (nRet != DICOM_SUCCESS) { strcpy(gszPrivateKeyPassword, ""); } L_DicomFreeNet(hDicomNet); } return nRet; }