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:
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
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)
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
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!