C++ 搞不懂这个????在wchar\t的末尾
我试图向另一个窗口上的文本框发送一个wchar\u t,我不明白为什么当我打印到控制台时,它会正确显示,但当我发送到文本框时,它会附加一个????????最后 //来自MyTest.h的函数C++ 搞不懂这个????在wchar\t的末尾,c++,C++,我试图向另一个窗口上的文本框发送一个wchar\u t,我不明白为什么当我打印到控制台时,它会正确显示,但当我发送到文本框时,它会附加一个????????最后 //来自MyTest.h的函数 static HWND GetEditWindow() { HWND winmx = WinMX::GetWinMXWindow(); if(winmx == NULL) return NULL; // Check for inner win
static HWND GetEditWindow()
{
HWND winmx = WinMX::GetWinMXWindow();
if(winmx == NULL) return NULL;
// Check for inner window
HWND inner = FindWindowEx(winmx, NULL, NULL, NULL);
while(inner != NULL)
{
TCHAR title[100];
GetWindowText(inner, title, 100);
if(wcsstr(title, L"WinMX Peer Network") != NULL && wcsstr(title, L"on WinMX Peer Network") == NULL)
return inner;
inner = FindWindowEx(winmx, inner, NULL, NULL);
}
// Check for floating window
HWND channels = FindWindowEx(NULL, NULL, NULL, NULL);
while(channels != NULL)
{
TCHAR title[100];
GetWindowText(channels, title, 100);
if(wmemcmp(title, L"WinMX Peer Network", 18) == 0)
return channels;
channels = FindWindowEx(NULL, channels, NULL, NULL);
}
// Failed
return NULL;
}
static map<wchar_t*, HWND> GetOpenWindows()
{
map<wchar_t*, HWND> results;
HWND winmx = WinMX::GetWinMXWindow();
if (winmx == NULL) return results;
// Check for inner chat windows.
HWND inner = FindWindowEx(winmx, NULL, NULL, NULL);
while(inner != NULL)
{
TCHAR title[100];
GetWindowText(inner, title, 100);
if(wcsstr(title, L" on WinMX Peer Network") != NULL)
{
wchar_t* newtitle = new wchar_t[wcslen(title) - 22];
wmemcpy(newtitle, title, wcslen(title) - 22);
results.insert(pair<wchar_t*,HWND>(newtitle, inner));
}
inner = FindWindowEx(winmx, inner, NULL, NULL);
}
// Check for floating chat windows.
HWND top = FindWindowEx(NULL, NULL, NULL, NULL);
while(top != NULL)
{
TCHAR title[100];
GetWindowText(top, title, 100);
if(wcsstr(title, L"on WinMX Peer Network") != NULL)
{
wchar_t* newtitle = new wchar_t[wcslen(title) - 22];
wmemcpy(newtitle, title, wcslen(title) - 22);
results.insert(pair<wchar_t*,HWND>(newtitle, top));
}
top = FindWindowEx(NULL, top, NULL, NULL);
}
return results;
}
//Main.cpp
#include <stdio.h>
#include "MyTest.h"
using namespace std;
void main()
{
map<wchar_t*, HWND> Wins = Api::GetOpenWindows();
HWND editwindow = Api::GetEditWindow();
HWND filterbar = FindWindowEx(editwindow, NULL, L"edit", NULL);
for(map<wchar_t*, HWND>::iterator it = Wins.begin(); it != Wins.end(); ++it)
{
wcout << it->first << "\n";
SendMessage(filterbar, WM_SETTEXT, NULL, (LPARAM)it->first);
}
getchar();
}
// Output:
// WindowNameHere_334334
// TextBox SetText OutPut:
// WindowNameHere_334334????????
Api::GetOpenWindows和Api::GetEditWindow从何而来?这不是我认识的API。很抱歉,我在我的测试中有这样的API。以下是:wmemcpynewtitle,title,wcslentitle-22;您从未在分配的字符串中设置空终止符。在这种情况下,这是一种喜忧参半的祝福,因为这样做会写入你无论如何都不拥有的记忆。幸运的频道拥有少于22个字符。这会造成伤害。将wchar\u t*替换为std::wstring,这将永远不会发生。请确保您在unicode中无处不在:GetWindowTextW SendMessageW等。调试时宏应转换为这些调用。有人把中间的东西弄丢了。