Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to figure out how to send/set a date to SysMonthCal32 of a external application. I have the correct windowhandle and can set present textboxes, checkboxes and buttons, but somehow I can't get it to work for the MonthCalendar.

My code:
VB
Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1
   
    'MonthCalendar
    Public Const MCM_SETCURSEL = &H1002

    Dim hWnd As IntPtr
    Dim sClassName As StringBuilder
    Dim length As Integer
    Dim sb As New System.Text.StringBuilder

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ChildHandle As IntPtr
        Dim ChildHandle2 As IntPtr
        Dim ChildHandle3 As IntPtr
        Dim ButtonHandle As IntPtr

        Dim MainWindowHandle As IntPtr
        MainWindowHandle = FindWindow(Nothing, TextBox3.Text)
        hWnd = Nothing
        hWnd = FindWindowEx(MainWindowHandle, 0&, vbNullString, vbNullString)
        hWnd = FindWindowEx(MainWindowHandle, hWnd, vbNullString, vbNullString)
        hWnd = FindWindowEx(MainWindowHandle, hWnd, vbNullString, vbNullString)

        ChildHandle = FindWindowEx(hWnd, 0&, vbNullString, vbNullString)
        ChildHandle = FindWindowEx(hWnd, ChildHandle, vbNullString, vbNullString)

        ChildHandle2 = FindWindowEx(ChildHandle, IntPtr.Zero, "WindowsForms10.Window.8.app.0.33c0d9d", "Date frame to search")

        ChildHandle3 = FindWindowEx(ChildHandle2, 0&, vbNullString, vbNullString)
        ButtonHandle = FindWindowEx(ChildHandle2, ChildHandle3, vbNullString, vbNullString)

        '### TO CHECK FOR CORRECT WINDOW/HANDLE ###
        sClassName = New StringBuilder("", 256)
        Call GetClassName(ButtonHandle, sClassName, 256)
        TextBox1.Text = sClassName.ToString
                
        length = GetWindowTextLength(ButtonHandle)
        sb = New System.Text.StringBuilder("", length)
        GetWindowText(ButtonHandle, sb, sb.Capacity + 1)
        TextBox2.Text = sb.ToString
        '###

        Dim seldate As Date = Format(CDate("24-08-2013"), "dd-MM-yyyy")
        Dim setdate As SYSTEMTIME
        With setdate
            .wYear = Year(seldate)
            .wMonth = Month(seldate)
            .wDayOfWeek = Weekday(seldate, vbSunday) - 1
            .wDay = seldate.Day
            .wHour = Hour(seldate)
            .wMinute = Minute(seldate)
            .wSecond = Second(seldate)
            .wMilliseconds = 0
        End With

        If ButtonHandle <> 0 Then
            SendMessageDate(ButtonHandle, MCM_SETCURSEL, 0&, setdate)
        End If

End Sub

    'SENDMESSAGEDATE
   Private Declare Function SendMessageDate Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As SYSTEMTIME) As Long
    
   Public Structure SYSTEMTIME
        Dim wYear As Integer
        Dim wMonth As Integer
        Dim wDayOfWeek As Integer
        Dim wDay As Integer
        Dim wHour As Integer
        Dim wMinute As Integer
        Dim wSecond As Integer
        Dim wMilliseconds As Integer
   End Structure

End Class

The error I'm getting is on the SendMessageDate:
PInvokeStackImbalance was detected!

Does anyone have any ideas? Thanks in advance!
Posted
Updated 1-Aug-13 1:29am
v3

1 solution

Well, I doubt anyone is going to be able to help with the SysMonthCal32 thing, whatever that is.

But, the stack imbalance is coming from your Declare. You're using a Decalre statement for VB6, not VB.NET. A Long in VB6 was a 32-bit signed integer, where in VB.NET it's a 64-bit signed integer.

The Declare statement in VB.NET is:
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
 
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