CFanatic

Go Back   CFanatic > Programming > Win32 Programming

Join CFanatic Forum Now

Convert HWND to LPCTSTR WIN32 topic posted under Win32 Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 05-20-2008, 10:18 PM
Junior Member
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
micheal jordan is on a distinguished road
| More
Convert HWND to LPCTSTR WIN32

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;
.
Reply With Quote
  #2  
Old 07-18-2008, 05:47 AM
Senior Member
 
Join Date: Jul 2008
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
xpi0t0s is on a distinguished road
| More
Re: Convert HWND to LPCTSTR WIN32

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?
Reply With Quote
  #3  
Old 07-18-2008, 11:20 PM
Junior Member
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
micheal jordan is on a distinguished road
| More
Re: Convert HWND to LPCTSTR WIN32

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
Reply With Quote
  #4  
Old 07-20-2008, 05:24 AM
Senior Member
 
Join Date: Jul 2008
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
xpi0t0s is on a distinguished road
| More
Re: Convert HWND to LPCTSTR WIN32

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.
Reply With Quote
  #5  
Old 07-20-2008, 11:14 PM
Junior Member
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
micheal jordan is on a distinguished road
| More
Re: Convert HWND to LPCTSTR WIN32

Hi there
would you xpi0t0s
give an example or just change the attached sample PLEASE
Reply With Quote
  #6  
Old 07-20-2008, 11:17 PM
shabbir's Avatar
Administrator
 
Join Date: Sep 2006
Posts: 1,158
Thanks: 48
Thanked 62 Times in 43 Posts
shabbir has a spectacular aura aboutshabbir has a spectacular aura about
| More
Re: Convert HWND to LPCTSTR WIN32

Hey, for security reasons we don't allow attachments for code and I would suggest you share the code in posts.
Reply With Quote
  #7  
Old 07-22-2008, 08:53 PM
Junior Member
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
micheal jordan is on a distinguished road
| More
Re: Convert HWND to LPCTSTR WIN32

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
Reply With Quote
  #8  
Old 07-23-2008, 12:48 AM
Senior Member
 
Join Date: Jul 2008
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
xpi0t0s is on a distinguished road
| More
Re: Convert HWND to LPCTSTR WIN32

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);
Reply With Quote
  #9  
Old 07-24-2008, 10:31 PM
Junior Member
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
micheal jordan is on a distinguished road
| More
Re: Convert HWND to LPCTSTR WIN32

thank you
I use these tree lines
Code:
	char szBuffer[256];
	sprintf(szBuffer, "0x%08x", hwnd);
	SetWindowText(hEdit3,szBuffer);
Reply With Quote
  #10  
Old 10-05-2008, 04:12 AM
Junior Member
 
Join Date: Oct 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
johny10151981 is on a distinguished road
| More
Post Re: Convert HWND to LPCTSTR WIN32

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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Win32 programming book sen_kumar76 Win32 Programming 11 09-11-2011 01:26 PM
how to convert NFA to DFA in c ashokkumarrsp C Programming 3 07-31-2008 04:51 PM
convert dialog application rachel196 MFC Programming 1 06-26-2008 10:31 AM
new win32 programmer rajeshreddy Introduce yourself 1 02-20-2008 01:30 AM
Win32 resources shabbir Win32 Programming 0 09-09-2006 05:16 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:33 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board