1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Convert HWND to LPCTSTR WIN32

Discussion in 'Win32' started by micheal jordan, May 21, 2008.

  1. micheal jordan

    micheal jordan New Member

    Joined:
    May 21, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    hi every one
    I'm new here and new to windows programing
    how to convert from HWND to LPCTSTR
    I want to disply a message after I use FindWindow
    the handle mast be HWND and the caption must be LPCTSTR
    so any one know how to do this

    HWND Handle;
    Handle=FindWindow("win32padClass","[Untitled]* - win32pad");
    MessageBox(hWnd,Handle,"Handle",MB_OK);

    thank you;
    .
     
  2. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Impossible to answer from the information given, except to say that converting a HWND to a LPCTSTR is impossible; they're completely different objects.

    Could you give an example, you mentioned "[Untitled]* - win32pad", what message exactly do you want to display if, say, the window handle of the first window you find with this title is 0x01234567?
     
  3. micheal jordan

    micheal jordan New Member

    Joined:
    May 21, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    hi there
    I'm sorry if the question is not clear
    let's say what you say
    I find a Window Handle with FindWindow/EX
    we know that the type of that return is HWND

    now, how about that
    I have a Window with child windows

    static= Caption
    static=class
    static = Handle
    and a

    edit=NULL // for the window CAPTION
    edit=NULL // for the window CLASS
    edit=NULL //for the window HANDLE // that is disabled
    and
    button=Find Window

    now the user write the caption and the class of the window
    he wants to get its handle

    and when he clicks the button, the found handle should be written in the edit
    window's handle
     
  4. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    OK, you can convert a number into a string with sprintf. I'd probably use the format string 0x%08lx, then the resulting string would contain "0x01234567" in my example.
     
  5. micheal jordan

    micheal jordan New Member

    Joined:
    May 21, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi there
    would you xpi0t0s
    give an example or just change the attached sample PLEASE
     
  6. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    Hey, for security reasons we don't allow attachments for code and I would suggest you share the code in posts.
     
  7. micheal jordan

    micheal jordan New Member

    Joined:
    May 21, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    all right what ever
    here is the C++ code
    Code:
    // Test.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include <commctrl.h>
    #include "resource.h"
    #include <stdio.h>
    #include <string>
    using namespace std;
    int CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg) {
    	case WM_DESTROY:
    		case WM_CLOSE:
    			PostQuitMessage(0);
    		break;
        case WM_INITDIALOG:
    
            RECT    rc;
    
            GetWindowRect(hDlg, &rc);
    
            int     nWidth, nHeight;
            
            nWidth = rc.right - rc.left;
            nHeight = rc.bottom - rc.top;
            
            int     cxScreen, cyScreen;
            
            cxScreen = GetSystemMetrics(SM_CXSCREEN);
            cyScreen = GetSystemMetrics(SM_CYSCREEN);
    
            MoveWindow(hDlg, (cxScreen - nWidth) / 2, (cyScreen - nHeight) / 2, nWidth, nHeight, TRUE);
    
            break;
    
        case WM_COMMAND:
    
            switch (LOWORD(wParam)) {
    
    		case ID_GETHWND:
    HWND hwnd,hEdit1,hEdit2;
    HWND hEdit3;
    
    
    hEdit1=GetDlgItem(hDlg,ID_CAPTION);
    hEdit2=GetDlgItem(hDlg,ID_CLASS);
    hEdit3=GetDlgItem(hDlg,ID_HWND);
    
    int lEdit1,lEdit2;
    lEdit1=SendMessage(hEdit1,WM_GETTEXTLENGTH,NULL,NULL);
    lEdit2=SendMessage(hEdit2,WM_GETTEXTLENGTH,NULL,NULL);
    
    char Caption[255];
    char Class[255];
    
    GetWindowText(hEdit1,Caption,GetWindowTextLength(hEdit1) + 1);
    GetWindowText(hEdit2,Class,GetWindowTextLength(hEdit2) + 1);
    
    hwnd=FindWindow(Class,Caption); //we find the window
    
    /*what is wrong here NOTING WORK
    
    SendMessage(hEdit3,WM_SETTEXT,(WPARAM)hwnd,NULL);
    SendMessage(hEdit3,WM_SETTEXT,hwnd,NULL);
    SetWindowText(hEdit3,(LPCTSTR)hwnd);
    SetWindowText(hEdit3,h);
    */
            }
    
            break;
    
        }
        return 0;
    }
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        InitCommonControls();
    	DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG), 0, DlgProc, 0);
    
    	return 0;
    }
    
    
    here is the RC code
    Code:
    //Microsoft Developer Studio generated resource script.
    //
    #include "resource.h"
    
    #define APSTUDIO_READONLY_SYMBOLS
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 2 resource.
    //
    #include "afxres.h"
    
    /////////////////////////////////////////////////////////////////////////////
    #undef APSTUDIO_READONLY_SYMBOLS
    
    /////////////////////////////////////////////////////////////////////////////
    // Chinese (P.R.C.) resources
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
    #ifdef _WIN32
    LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
    #pragma code_page(936)
    #endif //_WIN32
    
    #ifdef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // TEXTINCLUDE
    //
    
    1 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "resource.h\0"
    END
    
    2 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "#include ""afxres.h""\r\n"
        "\0"
    END
    
    3 TEXTINCLUDE DISCARDABLE 
    BEGIN
        "\r\n"
        "\0"
    END
    
    #endif    // APSTUDIO_INVOKED
    
    #endif    // Chinese (P.R.C.) resources
    /////////////////////////////////////////////////////////////////////////////
    
    
    /////////////////////////////////////////////////////////////////////////////
    // English (U.S.) resources
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
    #ifdef _WIN32
    LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
    #pragma code_page(1252)
    #endif //_WIN32
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Dialog
    //
    
    IDD_DIALOG DIALOG DISCARDABLE  0, 0, 252, 42
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Find Window Handle"
    FONT 8, "MS Sans Serif"
    BEGIN
        LTEXT           "Caption",IDC_STATIC,4,7,25,8
        LTEXT           "Class",IDC_STATIC,3,26,21,8
        EDITTEXT        ID_CAPTION,31,7,110,12,ES_AUTOHSCROLL
        EDITTEXT        ID_CLASS,31,25,110,12,ES_AUTOHSCROLL
        PUSHBUTTON      "Get Handle",ID_GETHWND,147,17,50,14
        EDITTEXT        ID_HWND,200,17,51,14,ES_AUTOHSCROLL
    END
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Icon
    //
    
    // Icon with lowest ID value placed first to ensure application icon
    // remains consistent on all systems.
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // DESIGNINFO
    //
    
    #ifdef APSTUDIO_INVOKED
    GUIDELINES DESIGNINFO DISCARDABLE 
    BEGIN
        IDD_DIALOG, DIALOG
        BEGIN
            RIGHTMARGIN, 251
            BOTTOMMARGIN, 37
        END
    END
    #endif    // APSTUDIO_INVOKED
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // 24
    //
    
    1                       24      DISCARDABLE     "xptheme.bin"
    #endif    // English (U.S.) resources
    /////////////////////////////////////////////////////////////////////////////
    
    
    
    #ifndef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 3 resource.
    //
    
    
    /////////////////////////////////////////////////////////////////////////////
    #endif    // not APSTUDIO_INVOKED
    
    
    I HOPE THAT IS A LITTLE CLEAR
     
  8. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    SendMessage(hEdit3,WM_SETTEXT,(WPARAM)hwnd,NULL);
    SendMessage(hEdit3,WM_SETTEXT,hwnd,NULL);
    SetWindowText(hEdit3,(LPCTSTR)hwnd);
    SetWindowText(hEdit3,h);
    
    It's been a while since I've done any WinAPI dialog programming so I'm not sure whether you should use SendMessage...WM_SETTEXT or SetWindowText, in fact they could be equivalent.

    However it doesn't work because hwnd is a window handle, not a text string. Any functions that relate to text MUST take a char*.

    So if you want to display the window handle itself you must convert the handle to text, which you can do with sprintf, which works exactly the same way as printf except that there is an additional parameter at the start that tells sprintf where to print the text. For example
    Code:
    char str[128];
    sprintf(str,"Window handle is 0x%08x",hwnd);
    
     
  9. micheal jordan

    micheal jordan New Member

    Joined:
    May 21, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    thank you
    I use these tree lines
    Code:
    	char szBuffer[256];
    	sprintf(szBuffer, "0x%08x", hwnd);
    	SetWindowText(hEdit3,szBuffer);
    
     
  10. johny10151981

    johny10151981 New Member

    Joined:
    Jul 7, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    char szBuffer[256];
    sprintf(szBuffer, "0x%08x", hwnd);
    SetWindowText(hEdit3,szBuffer);

    i dont think it will work properly, you may get japanese kanji as out put try with TCHAR szBufferp[256]; (wide charecter version),
    next thing is find/create a function to convert from large number to TCHAR
     
  11. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    I'm sure I used to use %x for this sort of stuff. Maybe compilers were less strict in those days. Anyway, the following definitely works (I tested this in a WM_CREATE block within the basic Windows C++ program generated by VS2005):
    Code:
    TCHAR szBuffer[256];
    wsprintf(szBuffer, L"Window handle 0x%08p", hWnd);
    SetWindowText(hWnd,szBuffer);
    
     

Share This Page