/// <summary> /// Shows the folder browser dialog box with the specified owner window. /// </summary> public DialogResult ShowDialog(IWin32Window owner) { IntPtr pidlRoot = IntPtr.Zero; // Get/find an owner HWND for this dialog. IntPtr hWndOwner; if (owner != null) { hWndOwner = owner.Handle; } else { hWndOwner = Win32API.GetActiveWindow(); } // Get the IDL for the specific startLocation. Win32API.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)startLocation, out pidlRoot); if (pidlRoot == IntPtr.Zero) { return(DialogResult.Cancel); } int mergedOptions = (int)publicOptions | (int)privateOptions; if ((mergedOptions & (int)Win32API.Shell32.BffStyles.NewDialogStyle) != 0) { if (System.Threading.ApartmentState.MTA == Application.OleRequired()) { mergedOptions = mergedOptions & (~(int)Win32API.Shell32.BffStyles.NewDialogStyle); } } IntPtr pidlRet = IntPtr.Zero; try { // Construct a BROWSEINFO. Win32API.Shell32.BROWSEINFO bi = new Win32API.Shell32.BROWSEINFO(); IntPtr buffer = Marshal.AllocHGlobal(Win32API.MAX_PATH); bi.pidlRoot = pidlRoot; bi.hwndOwner = hWndOwner; bi.pszDisplayName = buffer; bi.lpszTitle = descriptionText; bi.ulFlags = mergedOptions; // The rest of the fields are initialized to zero by the constructor. // bi.lpfn = null; bi.lParam = IntPtr.Zero; bi.iImage = 0; // Show the dialog. pidlRet = Win32API.Shell32.SHBrowseForFolder(ref bi); // Free the buffer you've allocated on the global heap. Marshal.FreeHGlobal(buffer); if (pidlRet == IntPtr.Zero) { // User clicked Cancel. return(DialogResult.Cancel); } // Then retrieve the path from the IDList. StringBuilder sb = new StringBuilder(Win32API.MAX_PATH); if (0 == Win32API.Shell32.SHGetPathFromIDList(pidlRet, sb)) { return(DialogResult.Cancel); } // Convert to a string. directoryPath = sb.ToString(); } finally { Win32API.IMalloc malloc = GetSHMalloc(); malloc.Free(pidlRoot); if (pidlRet != IntPtr.Zero) { malloc.Free(pidlRet); } } return(DialogResult.OK); }
/// <summary> /// Awakes filter worker /// </summary> public static void AwakeFilterWorker() { Win32API.SetEvent(varFilterJobsUpdatedEvent); }
/// <summary> /// Compress thread procedure /// </summary> /// <param name="FileName">File to quarantine</param> /// <param name="Listeners">Listeners to be notified about progress</param> private static void QuarantineWorkerProc() { while (true) { int Res = Win32API.WaitForSingleObject(NewItemAddedEvent, 250); lock (MainLock) { if (Terminate) { break; } } if (Res != Win32API.WAIT_OBJECT_0) { continue; } while (true) { QuarantineItem item; lock (MainLock) { if (ItemsQueue.Count > 0) { item = (QuarantineItem)ItemsQueue[0]; ItemsQueue.RemoveAt(0); } else { break; } CurrentID = item.ID; Working = true; } NotifyData notify = new NotifyData(); notify.FileName = item.FileName; notify.Quarantine = item.Quarantine; notify.ID = item.ID; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_START, notify, item.Listeners); if (item.Quarantine) { // Perform compressing and quarantine file string CompressedFileName = GetQuarantineFileName(item.FileName); if (CompressEncrypted(item.FileName, CompressedFileName, QuarantinePassword, item.Listeners, item.ID)) { if (PostCompressProcessing(item.FileName, CompressedFileName)) { lock(MainLock) { CurrentID = -1; } notify.Result = QuarantineResults.Success; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_COMPLETE, notify, item.Listeners); } else { lock(MainLock) { CurrentID = -1; } notify.Result = QuarantineResults.Failed; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_COMPLETE, notify, item.Listeners); } } else { notify.Result = QuarantineResults.Failed; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_COMPLETE, notify, item.Listeners); } } else { // Unquarantine file if (Extract(item.CompressedFileName, item.FileName, item.InitialSize, item.Listeners, item.ID)) { if (!item.Temp) { if (PostExtractProcessing(item.CompressedFileName, item.FileName)) { lock (MainLock) { CurrentID = -1; } notify.Result = QuarantineResults.Success; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_COMPLETE, notify, item.Listeners); } else { lock (MainLock) { CurrentID = -1; } notify.Result = QuarantineResults.Failed; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_COMPLETE, notify, item.Listeners); } } else { lock (MainLock) { CurrentID = -1; } notify.Result = QuarantineResults.Failed; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_COMPLETE, notify, item.Listeners); } } else { notify.Result = QuarantineResults.Failed; SendQuarantineNotify(UM_QUARANTINE_ITEM_PROCESSING_COMPLETE, notify, item.Listeners); } } lock (MainLock) { CurrentID = -1; if (ItemsQueue.Count == 0) { QueueLocked = false; notify = new NotifyData(); SendQuarantineNotify(UM_QUARANTINE_QUEUE_DONE, notify, item.Listeners); } if (Terminate) { Working = false; break; } } } lock (MainLock) { Working = false; } } }
/// <summary> /// /// </summary> /// <param name="FileName"></param> /// <param name="Listeners"></param> /// <returns></returns> public static QuarantineResults UnquarantineFile(string FileName, IntPtr[] Listeners, bool Temp) { lock (MainLock) { if (QueueLocked) { return QuarantineResults.FailedQueueLocked; } } if (!IsFileInQuarantine(FileName)) { return QuarantineResults.FailedFileIsNotInQuarantine; } long InitialSize = 0; string ComressedFileName = ""; long id = 0; lock (MainLock) { foreach (QuarantineItem queued in ItemsQueue) { if (queued.FileName == FileName && queued.Quarantine == false) { return QuarantineResults.FailedAlreadyQueued; } } string command = "SELECT * FROM QuarantineItems WHERE InitialPath='" + FileName + "';"; ArrayList list; if (ClamWinDatabase.ExecReader(command, out list)) { if (list.Count != 0) { InitialSize = long.Parse((string)list[4]); ComressedFileName = (string)list[2]; } else { return QuarantineResults.Failed; } } else { return QuarantineResults.Failed; } id = ItemsCounter++; QuarantineItem item = new QuarantineItem(); item.FileName = Temp ? GetQuarantineTempFolder() + Path.GetFileName(FileName) : FileName; item.Listeners = Listeners; item.Quarantine = false; item.InitialSize = InitialSize; item.CompressedFileName = ComressedFileName; item.Temp = Temp; item.ID = id; ItemsQueue.Add(item); Win32API.SetEvent(NewItemAddedEvent); } NotifyData data = new NotifyData(); data.FileName = FileName; data.Quarantine = false; data.Size = InitialSize; data.ID = id; SendQuarantineNotify(UM_QUARANTINE_QUEUE_NEW_ITEM, data, Listeners); return QuarantineResults.Success; }
/// <summary> /// Quarantine specified file /// </summary> /// <param name="FileName">File to quarantine</param> /// <returns>Result</returns> public static QuarantineResults QuarantineFile(string FileName,IntPtr[] Listeners) { lock (MainLock) { if (QueueLocked) { return QuarantineResults.FailedQueueLocked; } } try { System.IO.FileInfo fi = new System.IO.FileInfo(FileName); if (!fi.Exists) { return QuarantineResults.FailedFileDoesNotExist; } } catch { return QuarantineResults.Failed; } if (IsFileInQuarantine(FileName)) { return QuarantineResults.FailedAlreadyQuarantined; } long id = 0; lock (MainLock) { foreach (QuarantineItem queued in ItemsQueue) { if (queued.FileName == FileName && queued.Quarantine == true) { return QuarantineResults.FailedAlreadyQueued; } } id = ItemsCounter++; QuarantineItem item = new QuarantineItem(); item.FileName = FileName; item.Listeners = Listeners; item.Quarantine = true; item.ID = id; ItemsQueue.Add(item); Win32API.SetEvent(NewItemAddedEvent); } NotifyData data = new NotifyData(); data.FileName = FileName; data.Quarantine = true; data.Size = 0; data.ID = id; try { FileInfo fi = new FileInfo(FileName); data.Size = fi.Length; } catch { } SendQuarantineNotify(UM_QUARANTINE_QUEUE_NEW_ITEM, data, Listeners); return QuarantineResults.Success; }
/// <summary> /// Post user message to main form /// </summary> public static void PostCurrentChanged() { Win32API.PostMessage(MainFormHandle, UM_CURRENT_NOTIFICATION_CHANGED, 0, 0); }
private static void DownloadWorker(IntPtr[] Listeners) { while (true) { WebClient client = new WebClient(); byte[] data = client.DownloadData("http://clamwin.sourceforge.net/clamwin.ver"); string helper = Encoding.ASCII.GetString(data, 0, data.Length); int pos = helper.IndexOf("="); if (pos == -1) { return; } pos++; int pos1 = helper.IndexOf("[", pos); string version = helper.Substring(pos, pos1 - pos); Version Version = new Version(); try { pos = version.IndexOf("."); if (pos != -1) { Version.part1 = int.Parse(version.Substring(0, pos)); } else { throw new SystemException(); } pos++; pos1 = version.IndexOf(".", pos); if (pos1 != -1) { Version.part2 = int.Parse(version.Substring(pos, pos1 - pos)); } else { throw new SystemException(); } pos1++; pos = version.IndexOf(".", pos1); if (pos != -1) { Version.part3 = int.Parse(version.Substring(pos1, pos - pos1)); } else { throw new SystemException(); } pos++; if (pos != -1) { Version.part4 = int.Parse(version.Substring(pos)); } else { throw new SystemException(); } } catch { Version = CurrentVersion; } if (CurrentVersion.Compare(Version) == -1) { NotifyData notify = new NotifyData(); notify.Version = Version; notify.CurrentVersion = CurrentVersion; foreach (IntPtr handle in Listeners) { int size = Marshal.SizeOf(notify); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(notify, ptr, false); Win32API.PostMessage(handle, UM_NEW_VERSION_AVAILABLE, (uint)ptr.ToInt32(), 0); } } System.Threading.Thread.Sleep(60000 * 60 * 12); } }