p/invoke usage

StewartBW 1,765 Reputation points
2025-06-15T09:20:08.06+00:00

Hi

Anyone can confirm if my p/invoke definition and usage is correct?

<DllImport("dwmapi.dll", CharSet:=CharSet.Unicode)>

Private Function DwmSetWindowAttribute(ByVal hwnd As IntPtr, ByVal attribute As UInteger, ByRef pvAttribute As Integer, ByVal cbAttribute As UInteger) As Integer

End Function

DwmSetWindowAttribute(Form1.Handle, 20, CInt(IsDarkMode), CUInt(Marshal.SizeOf(GetType(UInteger)))) 'DWMWA_USE_IMMERSIVE_DARK_MODE

DwmSetWindowAttribute(Form1.Handle, 1029, 1, CUInt(Marshal.SizeOf(GetType(UInteger)))) 'DWMWA_MICA_EFFECT

DwmSetWindowAttribute(Form1.Handle, 33, 2, CUInt(Marshal.SizeOf(GetType(UInteger)))) 'DWMWA_WINDOW_CORNER_PREFERENCE DWMWCP_ROUND

parameters: IntPtr UInt Int UInt

no PreserveSigAttribute CharSet.Unicode

Thanks all :)

Developer technologies VB
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2025-06-15T13:34:46.9+00:00

    If you get correct results, it is ok...

    There are several ways to declare it

    For example, for DWMWA_USE_IMMERSIVE_DARK_MODE, I used :

      <DllImport("Dwmapi.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function DwmSetWindowAttribute(hwnd As IntPtr, dwAttribute As Integer, <[In]> pvAttribute() As Integer, cbAttribute As Integer) As Integer
        End Function
    
    

    in this test (on Windows 10 22H2) :

    https://github.com/castorix/VB_WinUI3_DesktopWindowXamlSource/blob/master/Form1.vb#L65


  2. RLWA32 49,536 Reputation points
    2025-06-15T15:10:48.6666667+00:00

    I think it always makes code more understandable if you take the time to declare enums and structures. For example, you cold define an enum for the DWMATTRIBUTES and other variables specified in the documentation for the DwmSetAttributes function. You can also take advantage of function overloading and default parameters to simplify/clarify the code.

    For example,

        Public Enum DWMWINDOWATTRIBUTE As Integer
            DWMWA_NCRENDERING_ENABLED = 1               ' [get] Is non-client rendering enabled/disabled
            DWMWA_NCRENDERING_POLICY                    ' [set] DWMNCRENDERINGPOLICY - Non-client rendering policy
            DWMWA_TRANSITIONS_FORCEDISABLED             ' [set] Potentially enable/forcibly disable transitions
            DWMWA_ALLOW_NCPAINT                         ' [set] Allow contents rendered In the non-client area To be visible On the DWM-drawn frame.
            DWMWA_CAPTION_BUTTON_BOUNDS                 ' [get] Bounds Of the caption button area In window-relative space.
            DWMWA_NONCLIENT_RTL_LAYOUT                  ' [set] Is non-client content RTL mirrored
            DWMWA_FORCE_ICONIC_REPRESENTATION           ' [set] Force this window To display iconic thumbnails.
            DWMWA_FLIP3D_POLICY                         ' [set] Designates how Flip3D will treat the window.
            DWMWA_EXTENDED_FRAME_BOUNDS                 ' [get] Gets the extended frame bounds rectangle In screen space
            DWMWA_HAS_ICONIC_BITMAP                     ' [set] Indicates an available bitmap When there Is no better thumbnail representation.
            DWMWA_DISALLOW_PEEK                         ' [set] Don't invoke Peek on the window.
            DWMWA_EXCLUDED_FROM_PEEK                    ' [set] LivePreview exclusion information
            DWMWA_CLOAK                                 ' [set] Cloak Or uncloak the window
            DWMWA_CLOAKED                               ' [get] Gets the cloaked state Of the window
            DWMWA_FREEZE_REPRESENTATION                 ' [set] BOOL, Force this window To freeze the thumbnail without live update
            DWMWA_PASSIVE_UPDATE_MODE                   ' [set] BOOL, Updates the window only When desktop composition runs For other reasons
            DWMWA_USE_HOSTBACKDROPBRUSH                 ' [set] BOOL, Allows the use Of host backdrop brushes For the window.
            DWMWA_USE_IMMERSIVE_DARK_MODE = 20          ' [set] BOOL, Allows a window To either use the accent color, Or dark  according To the user Color Mode preferences.
            DWMWA_WINDOW_CORNER_PREFERENCE = 33         ' [set] WINDOW_CORNER_PREFERENCE, Controls the policy that rounds top-level window corners
            DWMWA_BORDER_COLOR                          ' [set] COLORREF, The color Of the thin border around a top-level window
            DWMWA_CAPTION_COLOR                         ' [set] COLORREF, The color Of the caption
            DWMWA_TEXT_COLOR                            ' [set] COLORREF, The color Of the caption text
            DWMWA_VISIBLE_FRAME_BORDER_THICKNESS        ' [get] UINT, width Of the visible border around a thick frame window
            DWMWA_SYSTEMBACKDROP_TYPE                   ' [get, Set] SYSTEMBACKDROP_TYPE, Controls the system-drawn backdrop material Of a window, including behind the non-client area.
            DWMWA_REDIRECTIONBITMAP_ALPHA               ' [set] BOOL, GDI redirection bitmap containspremultiplied alpha
            DWMWA_LAST
        End Enum
    
        Public Enum WINDOW_CORNER_PREFERENCE As Integer
            DWMWCP_DEFAULT = 0
            DWMWCP_DONOTROUND = 1
            DWMWCP_ROUND = 2
            DWMWCP_ROUNDSMALL = 3
        End Enum
    

    and also

        <DllImport("dwmapi.dll", ExactSpelling:=True, SetLastError:=False)>
        Public Shared Function DwmSetWindowAttribute(hwnd As IntPtr,
                                               dwAttribute As DWMWINDOWATTRIBUTE,
                                               <MarshalAs(UnmanagedType.Bool)>
                                               ByRef attribute As Boolean,
                                               Optional cbattribute As Integer = 4) As Integer
        End Function
    
        <DllImport("dwmapi.dll", ExactSpelling:=True, SetLastError:=False)>
        Public Shared Function DwmSetWindowAttribute(hwnd As IntPtr,
                                                dwAttribute As DWMWINDOWATTRIBUTE,
                                                ByRef attribute As Integer,
                                                Optional cbattribute As Integer = 4) As Integer
        End Function
    

    Many of the attributes are set by passing the address of a Win32 BOOL. So the first overload allows you to specify a VB True/False. Since its marshaled to unmanaged code as a 4 byte integer the size uses a default value of 4 bytes. No need to call Marshal.SizeOF for every function call. Same for any other call that sets a 4 byte integer value. If circumstances require you can specify the required size of the passed data in the call's cbattribute parameter instead of accepting the default.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.