how convert from LPCTSTR to string?

how can i convert from LPCTSTR to string?
i'm trying enum window properties, but i need print the properties names with cout, but the LPCTSTR type don't works with cout :(
In a unicode project you need to use wcout and wstring with LPCTSTR.
In a Multi-byte char set you can use string cout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <windows.h>
#include <tchar.h>

int main ()
{
   const TCHAR szBuffer[] = _T ("HEllo world.");

  #ifdef UNICODE
    std::wstring ws(szBuffer);
    std::wcout << ws;
  #else
    std::string s (szBuffer);
    std::cout << s;
  #endif

  system ("pause");
  return 0;
}


continue with Memory Error :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
BOOL CALLBACK PropEnumProc(
  _In_ HWND    hwnd,
  _In_ LPCTSTR lpszString,
  _In_ HANDLE  hData
)
{
    #ifdef UNICODE
    std::wstring ws(lpszString);
    std::wcout << ws;
  #else
    std::string s (lpszString);
    std::cout << s;
  #endif
    return TRUE;
}


//............
getwmpwindow=WaitUntilWindowsMediaPlayerIsVisible(1);//i get the window handle correctly
    SetWindowPos(getwmpwindow, HWND_TOPMOST, 830,735,0,0,
                             SWP_NOCOPYBITS| SWP_NOSIZE);
    EnumProps(getwmpwindow,PropEnumProc);

i can't print the property name :(
reading from: http://www.cplusplus.com/forum/articles/16820/
#define LPCTSTR const TCHAR*
DeleteFileA <- Takes a char string (LPCSTR)

so:
MessageBox(NULL,lpszString,"hey", MB_OK);
MessageBox() accept LPCTSTR.
but why the message box isn't showed? why i get a memory leak?
What memory leak? Did you allocate memory for your string?
it's an error from Operation System.
the only way that i printed the text was using the WriteConsole() console function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BOOL CALLBACK PropEnumProc(
  _In_ HWND    hwnd,
  _In_ LPCTSTR lpszString,
  _In_ HANDLE  hData
)
{

    HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD rd;
    WriteConsole( hout, lpszString, rd, &rd, 0 );
    cout << "\n";

    return TRUE;
}

why the message box don't show the string, if it's the same type?
ok. finally i get something:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
BOOL CALLBACK PropEnumProc(
  _In_ HWND    hwnd,
  _In_ LPCTSTR lpszString,
  _In_ HANDLE  hData
)
{
    #if defined(_UNICODE)
    {
        wchar_t szBuf[MAX_PATH] = { 0 };
        GlobalGetAtomNameW(LOWORD(lpszString), szBuf, _countof(szBuf));
        wcout <<szBuf << "\n";
    }
    #else
    {
        char szBuf[MAX_PATH] = { 0 };
        GlobalGetAtomNameA(LOWORD(lpszString), szBuf, _countof(szBuf));
        cout <<szBuf << "hey\n";
    }
            
    #endif
    return TRUE;
}

now i don't have any errors. but i see 2 problems:
1 - the cout prints, only, 'hey'. why if is UNICODE?
2 - i get a wrong output:
1
2
3
4
5
#43288

#43288

#43290 

why i don't get a normal string, instead numbers?
What is the value of lpszString ?
using a WriteConsole(), i get what i need, the property name. but i don't know what the string is by head. but seems that the windows ATOM's can get that results :(
finally i can convert them to a string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
BOOL CALLBACK PropEnumProc(
  _In_ HWND    hwnd,
  _In_ LPCTSTR lpszString,
  _In_ HANDLE  hData
)
{
    ATOM atmText;
    atmText=GlobalFindAtom(lpszString);
    string strText;

    #if defined(_UNICODE)
    {
        wchar_t szBuf[MAX_PATH] = { 0 };
        GlobalGetAtomNameW(atmText, szBuf, _countof(szBuf));
        GetAtomName(atmText,szBuf,_countof(szBuf));
        wstring ws(txt);
        strText(ws.begin(), ws.end());
    }
    #else
    {
        char szBuf[MAX_PATH] = { 0 };
        GlobalGetAtomNameA(atmText, szBuf, _countof(szBuf));
        strText=szBuf;
    }
    #endif
    cout << strText << "\n";

    return TRUE;
}

//use:
EnumProps(getwmpwindow,PropEnumProc);

getwmpwindow: it's a HWND object
thanks to all
Last edited on
Topic archived. No new replies allowed.