如何设置其他程序中DatePicker的日期
我想为其他程序中的 DatePicker 控件设置日期,用如下的代码运行不成功,
这一句总是返回 0 ,
a = SendMessage(hControl, DTM_SETSYSTEMTIME, 0, pStrBufferMemory),
我是单开了一个新的Excel进程,然后在里面画了一个Datepicker,这样来测试,
请大家帮我看看,哪里有问题
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Const DTM_SETSYSTEMTIME = &H1002
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const MEM_COMMIT = &H1000
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Const MEM_RELEASE = &H8000
Public Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Sub Test()
Dim hms As SYSTEMTIME
Dim hControl As Long
Dim pid As Long
Dim hProcess As Long
Dim pStrBufferMemory As Long
Dim a As Long
Dim b As Long
hms.wYear = 1982
hms.wMonth = 10
hms.wDay = 2
hControl = 1379188 '获得DatePicker句柄
GetWindowThreadProcessId hControl, pid
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
pStrBufferMemory = VirtualAllocEx(hProcess, ByVal 0&, LenB(hms), MEM_COMMIT, PAGE_EXECUTE_READWRITE)
If (WriteProcessMemory(hProcess, pStrBufferMemory, hms, LenB(hms), 0)) Then
a = SendMessage(hControl, DTM_SETSYSTEMTIME, 0, pStrBufferMemory)
End If
VirtualFreeEx hProcess, pStrBufferMemory, 0, MEM_RELEASE
CloseHandle hProcess
End sub