I am wanting to get and change the display date in the datetimepicker control located in a 3rd party application that runs in the background and thus not in focus. I found that DTM_GetSystemTime and DTM_SetSystemTime can do this but I have been unable to get it to work. I get an error message stating:
"A call to PInvoke function 'DateTimePicker!DateTimePicker.Form1::SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."
On a side note, I can get and set the SystemTray timeclock via GetSystemTime and SetSystemTime. This assures me that the SystemTime structure should be ok (works with short/int16). Any suggestions? Thanks.
"A call to PInvoke function 'DateTimePicker!DateTimePicker.Form1::SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."
On a side note, I can get and set the SystemTray timeclock via GetSystemTime and SetSystemTime. This assures me that the SystemTime structure should be ok (works with short/int16). Any suggestions? Thanks.
Code:
Imports System.Runtime.InteropServices
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As SYSTEMTIME) As Int32
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32
Const DTM_FIRST As Int32 = &H1000
Const DTM_GETSYSTEMTIME As Int32 = (DTM_FIRST + 1)
Const DTM_SETSYSTEMTIME As Int32 = (DTM_FIRST + 2)
<StructLayout(LayoutKind.Sequential)> Structure SYSTEMTIME
Public wYear As Int16
Public wMonth As Int16
Public wDayOfWeek As Int16
Public wDay As Int16
Public wHour As Int16
Public wMinute As Int16
Public wSecond As Int16
Public wMilliseconds As Int16
End Structure
Dim hwnd, datehwnd, txtboxhwnd As Integer
Dim gettime As New SYSTEMTIME
Dim changetime As New SYSTEMTIME
changetime.wDay = 22
changetime.wMonth = 10
changetime.wYear = 2005
hwnd = FindWindow(vbNullString, "Application_Name")
datehwnd = FindWindowEx(hwnd, 0, "DateTimePickerClassInfo", vbNullString)
SendMessage(datehwnd, DTM_GETSYSTEMTIME, 0, gettime)
SendMessage(datehwnd, DTM_SETSYSTEMTIME, 0, changetime)
End Sub