I didn't know about the SetLastError=true option on DllImports. Now that I have read up on it, though, according to MS documentation for it, you'd have to call GetLastWin32Error() instead of the Win32GetLastError P/Invoke that is being called.

Also, your patch has whitespace changes.

Cheers,
 Peter
----- Original Message ----- From: "Atsushi Eno" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: 16 September, 2005 03:36
Subject: [Mono-winforms-list] minor patch for win32 clipboard stuff


Hi,

I have a tiny patch for XplatUIWin32. Changes are

- Add SetLastError=true on some DllImports.
- Use Marshal.Copy() instead of ReadByte().
- When GetClipboardData() resulted in an error,
  throw ExternalException for Win32Error.

I think there would be several fixes could be done in this source ;-)

Atsushi Eno



--------------------------------------------------------------------------------


Index: XplatUIWin32.cs
===================================================================
--- XplatUIWin32.cs (revision 50131)
+++ XplatUIWin32.cs (working copy)
@@ -674,11 +674,8 @@
 header[13] = (byte)(offset >> 24);

 Array.Copy(header, 0, buffer, 0, 14);
+ Marshal.Copy (dib_data, buffer, 14, size - 14);

- for (int i = 14; i < size; i++) {
- buffer[i] = Marshal.ReadByte(dib_data, i - 14);
- }
-
 ms = new MemoryStream(buffer, 0, size, false);
 bmp = new Bitmap(ms);
 ms.Close();
@@ -1682,12 +1679,12 @@
 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
 }

-
- internal override void SetBorderStyle(IntPtr handle, BorderStyle border_style) {
- Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0,
- SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
- }

+ internal override void SetBorderStyle(IntPtr handle, BorderStyle border_style) {
+ Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0,
+ SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
+ }
+
 internal override void SetMenu(IntPtr handle, IntPtr menu_handle) {
 // Trigger WM_NCCALC
Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
@@ -1824,6 +1821,9 @@

 hmem = Win32GetClipboardData((uint)type);
 if (hmem == IntPtr.Zero) {
+ uint error = Win32GetLastError ();
+ if (error != 0)
+ throw new ExternalException ("Win32 internal error on GetClipboardData", (int) error);
 return null;
 }

@@ -1930,9 +1930,9 @@
 }
 }

- internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
- return Win32DnD.StartDrag(hwnd, data, allowedEffects);
- }
+ internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
+ return Win32DnD.StartDrag(hwnd, data, allowedEffects);
+ }

 internal override int KeyboardSpeed {
 get {
@@ -2218,7 +2218,7 @@
[DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
 private extern static uint Win32EnumClipboardFormats(uint format);

- [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)] + [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
 private extern static IntPtr Win32GetClipboardData(uint format);

[DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
@@ -2236,10 +2236,10 @@
[DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
 internal extern static uint Win32GlobalSize(IntPtr hMem);

- [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)] + [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);

- [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)] + [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
 internal extern static bool Win32GlobalUnlock(IntPtr hMem);
 #endregion
 }



--------------------------------------------------------------------------------


_______________________________________________
Mono-winforms-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-winforms-list


_______________________________________________
Mono-winforms-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-winforms-list

Reply via email to