sendmessagetimeout
时间: 2023-04-26 20:00:37 浏览: 74
sendmessagetimeout是一个函数,用于在指定的时间内发送消息并等待响应。如果在指定的时间内没有收到响应,则函数会超时并返回错误代码。该函数通常用于网络通信或与外部设备进行通信时,以确保及时收到响应并避免程序出现死锁或长时间等待的情况。
相关问题
多线程中使用::PostMessage会失败
在多线程中使用 `PostMessage` 函数可能会失败的原因是,目标窗口的消息队列可能已满,导致无法将消息放入队列中。这种情况下,`PostMessage` 函数会返回0,表示发送失败。
当目标窗口的消息队列已满时,可以考虑使用 `SendMessageTimeout` 函数代替 `PostMessage` 函数,以避免消息丢失。`SendMessageTimeout` 函数会等待目标窗口处理完消息后才返回。
以下是一个使用 `SendMessageTimeout` 函数的示例:
```cpp
DWORD dwResult;
BOOL bResult = SendMessageTimeout(
hWnd, // 目标窗口句柄
Msg, // 消息类型
wParam, // wParam 参数
lParam, // lParam 参数
SMTO_NORMAL, // 标志位
5000, // 超时时间(单位:毫秒)
&dwResult // 接收返回值的变量
);
if (bResult)
{
// 消息发送成功,可以根据返回值 dwResult 进行处理
}
else
{
// 消息发送失败
}
```
使用 `SendMessageTimeout` 函数可以确保在消息发送失败时能够得到通知,并可以根据需要进行处理。但需要注意,在多线程中发送消息时,仍然需要保证线程安全,避免出现竞争条件和死锁等问题。
希望这个回答对你有帮助!如果你还有其他问题,请继续提问。
帮我封装WeChat_control 以便实现上述代码
好的,以下是一个可能的`WeChat_control`类的封装实现。该类封装了微信PC客户端API的常用功能,可以方便地实现打开微信、打开聊天窗口、发送消息等操作。请将以下代码保存为一个名为`WeChat_control`的类模块。
```
Option Explicit
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Any, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const GW_HWNDNEXT = 2
Private Const WM_SETTEXT = &HC
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_RETURN = &HD
Private m_WeChatPath As String
Private m_WeChatHWnd As Long
Private m_WeChatEditHWnd As Long
Private m_ChatHWnd As Long
Private m_ChatTitle As String
Public Property Let WeChatPath(ByVal value As String)
m_WeChatPath = value
End Property
Public Sub StartWeChat()
'启动微信PC客户端
Dim pid As Long
pid = Shell(m_WeChatPath, vbNormalFocus)
'等待微信窗口加载完成
Do While m_WeChatHWnd = 0
m_WeChatHWnd = FindWindowEx(0&, m_WeChatHWnd, "WeChatMainWndForPC", vbNullString)
Sleep 100
Loop
'等待微信进程空闲
WaitForInputIdle pid, 5000
'获取微信编辑框句柄
m_WeChatEditHWnd = FindWindowEx(m_WeChatHWnd, 0&, "Edit", vbNullString)
End Sub
Public Sub OpenChatWindow(ByVal title As String)
'打开聊天窗口
Dim hwnd As Long
hwnd = FindWindowEx(m_WeChatHWnd, 0&, "WeChatChatWndForPC", vbNullString)
Do While hwnd > 0
If WindowText(hwnd) = title Then
m_ChatHWnd = hwnd
m_ChatTitle = title
Exit Sub
End If
hwnd = FindWindowEx(m_WeChatHWnd, hwnd, "WeChatChatWndForPC", vbNullString)
Loop
SendMessageTimeout m_WeChatHWnd, &H10, 0&, 0&, 2, 5000, ByVal 0&
SendMessageTimeout m_WeChatHWnd, &H10, 0&, 0&, 2, 5000, ByVal 0&
SendMessageTimeout m_WeChatHWnd, &H10, 0&, 0&, 2, 5000, ByVal 0&
hwnd = FindWindowEx(m_WeChatHWnd, 0&, "WeChatChatWndForPC", vbNullString)
Do While hwnd > 0
If WindowText(hwnd) = title Then
m_ChatHWnd = hwnd
m_ChatTitle = title
Exit Sub
End If
hwnd = FindWindowEx(m_WeChatHWnd, hwnd, "WeChatChatWndForPC", vbNullString)
Loop
End Sub
Public Sub SendMessage(ByVal text As String)
'发送消息
If m_ChatHWnd = 0 Then Exit Sub
SendMessageTimeout m_ChatHWnd, WM_SETTEXT, 0&, ByVal text, 2, 5000, ByVal 0&
SendMessageTimeout m_WeChatEditHWnd, WM_KEYDOWN, VK_RETURN, 0&, 2, 5000, ByVal 0&
SendMessageTimeout m_WeChatEditHWnd, WM_KEYUP, VK_RETURN, 0&, 2, 5000, ByVal 0&
End Sub
Public Function WindowText(ByVal hwnd As Long) As String
'获取窗口标题
Dim len As Long
len = SendMessageTimeout(hwnd, WM_GETTEXTLENGTH, 0&, ByVal 0&, 2, 5000, ByVal 0&)
If len > 0 Then
Dim buf As String
buf = Space(len + 1)
SendMessageTimeout hwnd, WM_GETTEXT, len + 1, ByVal buf, 2, 5000, ByVal 0&
WindowText = Left$(buf, len)
End If
End Function
```
使用示例:
```
Sub main()
'创建WeChat_control对象
Dim WeChat As New WeChat_control
'设置微信PC客户端路径
WeChat.WeChatPath = "E:\WeChat\WeChat.exe"
'启动微信PC客户端
WeChat.StartWeChat
'等待微信窗口加载完成(可根据需要设置等待时间)
Sleep 5000
'打开目标窗口
WeChat.OpenChatWindow "文件传输助手"
'向目标窗口发送消息
WeChat.SendMessage "负胜已分"
End Sub
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)