marshal return types, add CharSet.Unicode to functions that lack it, marshal BOOL parameters

This commit is contained in:
David Bremner 2016-04-16 18:28:44 -07:00
commit d780d337e4
7 changed files with 18 additions and 10 deletions

View file

@ -645,6 +645,7 @@ namespace Greenshot.Helpers
#region GET #region GET
#region PRODUCT INFO #region PRODUCT INFO
[DllImport("Kernel32.dll")] [DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetProductInfo( internal static extern bool GetProductInfo(
int osMajorVersion, int osMajorVersion,
int osMinorVersion, int osMinorVersion,
@ -654,7 +655,8 @@ namespace Greenshot.Helpers
#endregion PRODUCT INFO #endregion PRODUCT INFO
#region VERSION #region VERSION
[DllImport("kernel32.dll")] [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo); private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
#endregion VERSION #endregion VERSION
#endregion GET #endregion GET

View file

@ -59,6 +59,7 @@ namespace GreenshotPlugin.Core {
/// <summary>Encapsulates dialog functionality from the Credential Management API.</summary> /// <summary>Encapsulates dialog functionality from the Credential Management API.</summary>
public sealed class CredentialsDialog { public sealed class CredentialsDialog {
[DllImport("gdi32.dll", SetLastError=true)] [DllImport("gdi32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteObject(IntPtr hObject); private static extern bool DeleteObject(IntPtr hObject);
/// <summary>The only valid bitmap height (in pixels) of a user-defined banner.</summary> /// <summary>The only valid bitmap height (in pixels) of a user-defined banner.</summary>
@ -602,6 +603,6 @@ namespace GreenshotPlugin.Core {
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/creduiconfirmcredentials.asp /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/creduiconfirmcredentials.asp
/// </summary> /// </summary>
[DllImport("credui.dll", CharSet=CharSet.Unicode)] [DllImport("credui.dll", CharSet=CharSet.Unicode)]
public static extern ReturnCodes CredUIConfirmCredentials(string targetName, bool confirm); public static extern ReturnCodes CredUIConfirmCredentials(string targetName, [MarshalAs(UnmanagedType.Bool)] bool confirm);
} }
} }

View file

@ -423,6 +423,7 @@ namespace GreenshotPlugin.Core {
/// <param name="hObject"></param> /// <param name="hObject"></param>
/// <returns></returns> /// <returns></returns>
[DllImport("gdi32", SetLastError = true)] [DllImport("gdi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteObject(IntPtr hObject); private static extern bool DeleteObject(IntPtr hObject);
private WindowCapture() { private WindowCapture() {

View file

@ -62,6 +62,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// </summary> /// </summary>
public abstract class SafeObjectHandle : SafeHandleZeroOrMinusOneIsInvalid { public abstract class SafeObjectHandle : SafeHandleZeroOrMinusOneIsInvalid {
[DllImport("gdi32", SetLastError = true)] [DllImport("gdi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteObject(IntPtr hObject); private static extern bool DeleteObject(IntPtr hObject);
protected SafeObjectHandle(bool ownsHandle) : base(ownsHandle) { protected SafeObjectHandle(bool ownsHandle) : base(ownsHandle) {
@ -149,7 +150,8 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// </summary> /// </summary>
public class SafeCompatibleDCHandle : SafeDCHandle { public class SafeCompatibleDCHandle : SafeDCHandle {
[DllImport("gdi32", SetLastError = true)] [DllImport("gdi32", SetLastError = true)]
private static extern bool DeleteDC(IntPtr hDC); [return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteDC(IntPtr hDC);
[SecurityCritical] [SecurityCritical]
private SafeCompatibleDCHandle() : base(true) { private SafeCompatibleDCHandle() : base(true) {

View file

@ -58,12 +58,14 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[DllImport("kernel32", SetLastError = true)] [DllImport("kernel32", SetLastError = true)]
public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, bool bInheritHandle, int dwProcessId); public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags, StringBuilder lpExeName, ref uint lpdwSize); public static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags, StringBuilder lpExeName, ref uint lpdwSize);
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, uint uuchMax); public static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, uint uuchMax);
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr GetModuleHandle(string lpModuleName); public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32", SetLastError = true)] [DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject); public static extern bool CloseHandle(IntPtr hObject);
/// <summary> /// <summary>

View file

@ -49,12 +49,12 @@ namespace GreenshotPlugin.UnmanagedHelpers {
public SHITEMID mkid; public SHITEMID mkid;
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
private struct BROWSEINFO { private struct BROWSEINFO {
public IntPtr hwndOwner; public IntPtr hwndOwner;
public IntPtr pidlRoot; public IntPtr pidlRoot;
public IntPtr pszDisplayName; public IntPtr pszDisplayName;
[MarshalAs(UnmanagedType.LPTStr)] [MarshalAs(UnmanagedType.LPWStr)]
public string lpszTitle; public string lpszTitle;
public uint ulFlags; public uint ulFlags;
public IntPtr lpfn; public IntPtr lpfn;

View file

@ -104,14 +104,14 @@ namespace GreenshotPlugin.UnmanagedHelpers {
public extern static bool IsZoomed(IntPtr hwnd); public extern static bool IsZoomed(IntPtr hwnd);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)] [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static int GetClassName (IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); public extern static int GetClassName (IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32", SetLastError = true)] [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetClassLong(IntPtr hWnd, int nIndex); public static extern uint GetClassLong(IntPtr hWnd, int nIndex);
[DllImport("user32", SetLastError = true, EntryPoint = "GetClassLongPtr")] [DllImport("user32", SetLastError = true, EntryPoint = "GetClassLongPtr")]
public static extern IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex); public static extern IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32", SetLastError=true)] [DllImport("user32", CharSet=CharSet.Unicode, SetLastError=true)]
public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam); public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
@ -119,7 +119,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
public extern static int GetWindowLong(IntPtr hwnd, int index); public extern static int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32", SetLastError = true, EntryPoint = "GetWindowLongPtr")] [DllImport("user32", SetLastError = true, EntryPoint = "GetWindowLongPtr")]
public extern static IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex); public extern static IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex);
[DllImport("user32", SetLastError = true)] [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int index, int styleFlags); public static extern int SetWindowLong(IntPtr hWnd, int index, int styleFlags);
[DllImport("user32", SetLastError = true, EntryPoint = "SetWindowLongPtr")] [DllImport("user32", SetLastError = true, EntryPoint = "SetWindowLongPtr")]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int index, IntPtr styleFlags); public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int index, IntPtr styleFlags);
@ -139,9 +139,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
public static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi); public static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowScrollBar(IntPtr hwnd, ScrollBarDirection scrollBar, bool show); public static extern bool ShowScrollBar(IntPtr hwnd, ScrollBarDirection scrollBar, [MarshalAs(UnmanagedType.Bool)] bool show);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
public static extern int SetScrollPos(IntPtr hWnd, Orientation nBar, int nPos, bool bRedraw); public static extern int SetScrollPos(IntPtr hWnd, Orientation nBar, int nPos, [MarshalAs(UnmanagedType.Bool)] bool bRedraw);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]
public static extern RegionResult GetWindowRgn(IntPtr hWnd, SafeHandle hRgn); public static extern RegionResult GetWindowRgn(IntPtr hWnd, SafeHandle hRgn);
[DllImport("user32", SetLastError = true)] [DllImport("user32", SetLastError = true)]