private void LoadSources() { var sources = twain.GetSourceList(); try { string[] sourceStrings = new string[sources.Count]; for (int i = 0; i < sourceStrings.Length; i++) { if (i == twain.DefaultSourceIndex) { sourceStrings[i] = "Default:" + sources[i] + "\0"; } else { sourceStrings[i] = sources[i] + "\0"; } } int bufferSize = GetBufferLength(sourceStrings); IntPtr buffer = Marshal.AllocHGlobal(bufferSize); try { int offset = 0; Marshal.WriteInt32(buffer, sourceStrings.Length); offset += 4; Encoding unicode = Encoding.Unicode; for (int i = 0; i < sourceStrings.Length; i++) { byte[] bytes = unicode.GetBytes(sourceStrings[i]); Marshal.WriteInt32(buffer, offset, bytes.Length); offset += 4; Marshal.Copy(bytes, 0, new IntPtr(buffer.ToInt64() + (long)offset), bytes.Length); offset += bytes.Length; } NativeStructs.COPYDATASTRUCT cds = new NativeStructs.COPYDATASTRUCT(); cds.cbData = bufferSize; cds.lpData = buffer; SafeNativeMethods.SendCopyData(ClientHWnd, NativeConstants.WM_COPYDATA, this.Handle, ref cds); } finally { Marshal.FreeHGlobal(buffer); buffer = IntPtr.Zero; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
internal static extern IntPtr SendCopyData([In()] IntPtr hWnd, [In()] uint Msg, [In()] IntPtr wParam, [In()] ref NativeStructs.COPYDATASTRUCT cds);