无法搞清楚????在wchar_t的末尾

时间:2013-08-27 23:57:08

标签: c++

我正在尝试将wchar_t发送到另一个窗口上的文本框中,我无法弄清楚为什么当我打印到控制台时它显示正确,但是当我发送到文本框时它附加了“?????? ??”最后。

//来自MyTest.h的函数

    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????????

0 个答案:

没有答案