Click here to Skip to main content
15,444,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello.  I am attempting to add a command button to the title bar of an external application.  From my research it appears that I should be able to use the CreateWindowEx function provided by user32.dll, but so far I am having absolutely no luck.  I have done extensive research online, and I can't find anything helpful.  Nothings seems amiss in my code, but when I try to add the command button to the external app, I receive an OverflowException (Arithmetic operation resulted in an overflow) from the CreateWindowEx function.  Any help would be greatly appreciated!

See the below code.

Thank you.

What I have tried:

I try use this code:
Public Class Form1
    Private Const WS_CHILD = &H40000000
    Private Const WS_VISIBLE = &H10000000
    Private Structure CREATESTRUCT
        Dim lpCreateParams As Long
        Dim hInstance As Long
        Dim hMenu As Long
        Dim hWndParent As Long
        Dim cy As Long
        Dim cx As Long
        Dim y As Long
        Dim x As Long
        Dim style As Long
        Dim lpszName As String
        Dim lpszClass As String
        Dim ExStyle As Long
    End Structure
    Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Integer,                                                                      
ByVal lpClassName As String,
ByVal lpWindowName As String,                                                               ByVal dwStyle As Integer,                                                                         ByVal x As Integer,
ByVal y As Integer,
ByVal nWidth As Integer,
ByVal nHeight As Integer,
ByVal hWndParent As Integer,
ByVal hMenu As Integer,
ByVal hInstance As Integer,
ByRef lpParam As Object) As Long

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) As Long

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim lhWnd As Long, fw As Long, CS As CREATESTRUCT

        '.NET version of App.hInstance
        Dim rhWnd As Long = System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32()

        'Retrieve hWnd for the application for which I am trying to add a command button 
        fw = FindWindow(vbNullString, "External App Name")

        'Create the button (this is where the error occurs)
        lhWnd = CreateWindowEx(0, "BUTTON", "ButtonText", WS_VISIBLE Or WS_CHILD,
                               10, 10, 70, 25, fw, 0, rhWnd, CS)
    End Sub
End Class
Updated 23-Jun-21 21:53pm

1 solution

ByVal hWndParent As Integer,
ByVal hMenu As Integer,
ByVal hInstance As Integer,
ByRef lpParam As Object) As Long
The parameters starting with h need to be IntPtr, not Integer.

The return value also needs to be IntPtr, not Long. CreateWindowEx (user32)[^]
CreateWindowExW function (winuser.h) - Win32 apps | Microsoft Docs[^]

The FindWindow return value also needs to be an IntPtr.

Your lhWnd, fw, and rhWnd variables also need to be IntPtr, and you need to remove the .ToInt32() call on the line that assigns rhWnd.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900