/// <summary> /// Renders the file names in a DROPFILE structure that can be used along with the CF_DROP. /// </summary> /// <param name="fileNames">The file names.</param> /// <returns>A pointer to a global memory handle containing the DROPFILES structure with the file names appended behind.</returns> public static IntPtr RenderFiles(IEnumerable <string> fileNames) { // build array with zero terminated file names and double zero at the end var str = new List <byte>(); var enc = System.Text.Encoding.Unicode; foreach (var fileName in fileNames) { str.AddRange(enc.GetBytes(fileName)); str.AddRange(enc.GetBytes("\0")); // Terminate each fileName with a zero } str.AddRange(enc.GetBytes("\0")); // Terminate with an additional zero var byteData = str.ToArray(); // Allocate and get pointer to global memory var dropFiles = new DROPFILES(); int totalLength = Marshal.SizeOf(dropFiles) + byteData.Length; var ipGlobal = Marshal.AllocHGlobal(totalLength); if (ipGlobal != IntPtr.Zero) { // Build DROPFILES structure in global memory. dropFiles.pFiles = Marshal.SizeOf(dropFiles); dropFiles.fWide = 1; // for unicode-encoding Marshal.StructureToPtr(dropFiles, ipGlobal, true); var ipNew = new IntPtr(ipGlobal.ToInt64() + Marshal.SizeOf(dropFiles)); Marshal.Copy(byteData, 0, ipNew, byteData.Length); } return(ipGlobal); }
public bool StartProcess() { if (!IsOpen || !System.IO.Directory.Exists(FilesPath)) { return(false); } //IntPtr hwnd = this.Handle; DROPFILES s = new DROPFILES(); s.size = 20; //<-- 20 is the size of this struct in memory s.pt = new POINT(10, 10); //<-- drop file 20 pixels from left, total height minus 40 from top s.fND = false; //<-- the point 0;0 will be in the window s.WIDE = false; //<-- ANSI //Get all files from the folder string[] files = System.IO.Directory.GetFiles(FilesPath); //string[] files = { "C:\\Universal.0000\0", "C:\\Universal.0001\0" }; //<-- add null terminator at end Int32 lengthFiles = 0; foreach (String file in files) { lengthFiles += file.Length + 2; //<-- add null terminator at end } Int32 filelen = Convert.ToInt32(lengthFiles); byte[] bytes = RawSerialize(s); int structlen = (int)bytes.Length; int size = structlen + filelen + 1; IntPtr p = Marshal.AllocHGlobal(size); //<-- allocate memory and save pointer to p GlobalLock(p); //<-- lock p int i = 0; for (i = 0; i < structlen; i++) { Marshal.WriteByte(p, i, bytes[i]); } foreach (String file in files) { byte[] b = ASCIIEncoding.ASCII.GetBytes(file + "\0"); //<-- convert filepath to bytearray //<-- add null terminator at end for (int k = 0; k < b.Length; k++) { Marshal.WriteByte(p, i, b[k]); i++; } } //Write the end of the parameters to Send to the Window Marshal.WriteByte(p, i, 0); GlobalUnlock(p); PostMessage(Handle, WM_DROPFILES, p, IntPtr.Zero); //Process the files dragged int wParam = (BN_CLICKED << 16) | (ButtonId & 0xffff); SendMessage(Handle, WM_COMMAND, wParam, hWndButton); //Release all objects' //Marshal.FreeHGlobal(p); return(true); }
static void Main(string[] args) { //IntPtr handle = IntPtr.Zero; //Process[] localAll = Process.GetProcesses(); //Process mainProc = null; //foreach (Process proc in localAll) //{ // if (proc.MainWindowHandle != IntPtr.Zero) // { // ProcessModule pm = GetModule(proc); // if (pm != null && proc.MainModule.FileName.ToUpper() == fn.ToUpper()) { // handle = proc.MainWindowHandle; // mainProc = proc; // } // } //} ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = fn; Process.Start(startInfo); IntPtr handle = IntPtr.Zero; Process mainProc = null; while (handle == IntPtr.Zero) { Process[] localAll = Process.GetProcesses(); foreach (Process proc in localAll) { if (proc.MainWindowHandle != IntPtr.Zero) { ProcessModule pm = GetModule(proc); if (pm != null && proc.MainModule.FileName.ToUpper() == fn.ToUpper()) { handle = proc.MainWindowHandle; mainProc = proc; } } } } if (handle == IntPtr.Zero) { Console.WriteLine("Not found"); return; } Console.WriteLine("{0:X}", handle); //Process the file computation' IntPtr hWndList = GetDlgItem(handle, ListId); //IntPtr hwnd = this.Handle; DROPFILES s = new DROPFILES(); s.size = 20; //<-- 20 is the size of this struct in memory s.pt = new POINT(10, 10); //<-- drop file 20 pixels from left, total height minus 40 from top s.fND = false; //<-- the point 0;0 will be in the window s.WIDE = false; //<-- ANSI string[] files = { "C:\\Universal.0000\0", "C:\\Universal.0001\0" }; //<-- add null terminator at end Int32 lengthFiles = 0; foreach (String file in files) { lengthFiles += file.Length; } Int32 filelen = Convert.ToInt32(lengthFiles); byte[] bytes = RawSerialize(s); int structlen = (int)bytes.Length; int size = structlen + filelen + 1; IntPtr p = Marshal.AllocHGlobal(size); //<-- allocate memory and save pointer to p GlobalLock(p); //<-- lock p int i = 0; for (i = 0; i < structlen; i++) { Marshal.WriteByte(p, i, bytes[i]); Console.WriteLine("Wrote header byte " + i.ToString() + " of " + size.ToString()); } foreach (String file in files) { byte[] b = ASCIIEncoding.ASCII.GetBytes(file); //<-- convert filepath to bytearray for (int k = 0; k < file.Length; k++) { Marshal.WriteByte(p, i, b[k]); Console.WriteLine("Wrote filename byte " + i.ToString() + " of " + size.ToString()); i++; } } Marshal.WriteByte(p, i, 0); GlobalUnlock(p); PostMessage(handle, WM_DROPFILES, p, IntPtr.Zero); //Process the file computation' IntPtr hWndButton = GetDlgItem(handle, ButtonId); //Get default text String DefaultButtonText = String.Empty; const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); if (GetWindowText(hWndButton, Buff, nChars) > 0) { DefaultButtonText = Buff.ToString(); } int wParam = (BN_CLICKED << 16) | (ButtonId & 0xffff); SendMessage(handle, WM_COMMAND, wParam, hWndButton); Console.WriteLine("Message sent"); Buff.Length = 0; string CurrentButtonText = String.Empty; while (CurrentButtonText != DefaultButtonText) { if (GetWindowText(hWndButton, Buff, nChars) > 0) { CurrentButtonText = Buff.ToString(); Buff.Length = 0; } } //kILL PROCESS mainProc.Kill(); //mainProc.WaitForExit(); //Release all objects' //Marshal.FreeHGlobal(p); }
private static bool CopyFilesToClipboard(string[] strFiles, POINT pt, out int error) { var dropfiles = new DROPFILES(); int intFile; // Calculate total data length int intDataLen = 0; for (intFile = 0; intFile <= strFiles.GetUpperBound(0); intFile++) { intDataLen += strFiles[intFile].Length + 1; } intDataLen++; // Terminating double zero var bData = new byte[intDataLen]; int intPos = 0; // Build null terminated list of files for (intFile = 0; intFile <= strFiles.GetUpperBound(0); intFile++) { int intChar; for (intChar = 0; intChar < strFiles[intFile].Length; intChar++) { bData[intPos++] = (byte)strFiles[intFile][intChar]; } bData[intPos++] = 0; } bData[intPos] = 0; // Terminating double zero // Allocate and get pointer to global memory int intTotalLen = Marshal.SizeOf(dropfiles) + intDataLen; IntPtr ipGlobal = Marshal.AllocHGlobal(intTotalLen); if (ipGlobal == IntPtr.Zero) { error = Marshal.GetLastWin32Error(); return(false); } // Build DROPFILES structure in global memory. dropfiles.pFiles = Marshal.SizeOf(dropfiles); dropfiles.pt = pt; dropfiles.fNC = false; dropfiles.fWide = 0; Marshal.StructureToPtr(dropfiles, ipGlobal, true); var ipNew = new IntPtr(ipGlobal.ToInt32() + Marshal.SizeOf(dropfiles)); Marshal.Copy(bData, 0, ipNew, intDataLen); // Open and empty clipboard if (!OpenClipboard(IntPtr.Zero)) { error = Marshal.GetLastWin32Error(); return(false); } EmptyClipboard(); // Copy data to clipboard var result = SetClipboardData((int)CLIPFORMAT.CF_HDROP, ipGlobal); bool success = (result != IntPtr.Zero); error = Marshal.GetLastWin32Error(); if (!success) { Marshal.FreeHGlobal(ipGlobal); } // Clean up CloseClipboard(); return(success); }
private static bool CopyFilesToClipboard(string[] strFiles, POINT pt, out int error) { var dropfiles = new DROPFILES(); int intFile; // Calculate total data length int intDataLen = 0; for (intFile = 0; intFile <= strFiles.GetUpperBound(0); intFile++) intDataLen += strFiles[intFile].Length + 1; intDataLen++; // Terminating double zero var bData = new byte[intDataLen]; int intPos = 0; // Build null terminated list of files for (intFile = 0; intFile <= strFiles.GetUpperBound(0); intFile++) { int intChar; for (intChar = 0; intChar < strFiles[intFile].Length; intChar++) bData[intPos++] = (byte) strFiles[intFile][intChar]; bData[intPos++] = 0; } bData[intPos] = 0; // Terminating double zero // Allocate and get pointer to global memory int intTotalLen = Marshal.SizeOf(dropfiles) + intDataLen; IntPtr ipGlobal = Marshal.AllocHGlobal(intTotalLen); if (ipGlobal == IntPtr.Zero) { error = Marshal.GetLastWin32Error(); return false; } // Build DROPFILES structure in global memory. dropfiles.pFiles = Marshal.SizeOf(dropfiles); dropfiles.pt = pt; dropfiles.fNC = false; dropfiles.fWide = 0; Marshal.StructureToPtr(dropfiles, ipGlobal, true); var ipNew = new IntPtr(ipGlobal.ToInt32() + Marshal.SizeOf(dropfiles)); Marshal.Copy(bData, 0, ipNew, intDataLen); // Open and empty clipboard if (!OpenClipboard(IntPtr.Zero)) { error = Marshal.GetLastWin32Error(); return false; } EmptyClipboard(); // Copy data to clipboard var result = SetClipboardData((int)CLIPFORMAT.CF_HDROP, ipGlobal); bool success = (result != IntPtr.Zero); error = Marshal.GetLastWin32Error(); if (!success) Marshal.FreeHGlobal(ipGlobal); // Clean up CloseClipboard(); return success; }