string to TCHAR

I'm writing a WinAPI program, and I need a TCHAR variable for my CreateWindow() function. I want this TCHAR variable called QuestionLine to hold a string. But I cannot find a way to convert. I even tried stringstream and using a for loop to trade character by character but with that one I get a bunch of junk characters after the output.

This is my entire code but the problem is shown at the bottom of my post.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <windows.h>
#include <string>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CenterWindow(HWND);

ifstream in( "Resources.txt" );

	struct data
	{
		string chapter_data;
		string question_data;
		string answer_data;
	};

	data problem;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
				LPSTR lpCmdLine, int nCmdShow )
{
	MSG  msg ;    
	WNDCLASS wc = {0};
	wc.lpszClassName = TEXT( "Center" );
	wc.hInstance     = hInstance ;
	wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
	wc.lpfnWndProc   = WndProc ;
	wc.hCursor       = LoadCursor(0,IDC_ARROW);
  
	RegisterClass(&wc);
	CreateWindow( wc.lpszClassName, TEXT("Study Japanese"),
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		100, 100, 500, 300, 0, 0, hInstance, 0);  

	while( GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	//Main-------------------------------------------------------------

	//-----------------------------------------------------------------


	return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	static HWND QuestionBox;
	static HWND AnswerBox;

  switch(msg)  
  {
      case WM_CREATE:
      {
		  CenterWindow(hwnd);

		  
		  string line;

		  while( getline( in, line ) )
		  {
			  problem.chapter_data = line;
			  getline( in, line );
			  problem.question_data = line;
			  getline( in, line );
			  problem.answer_data = line;
		  }
		  TCHAR QuestionLine = problem.question_data;

		  /*for( unsigned i = 0; i < 100; ++i )
		  {
			  if( i < problem.question_data.length() )
				  QuestionLine[i] = problem.question_data[i];
			  else
				  QuestionLine[i] = ' ';
		  }*/

		  QuestionBox = CreateWindow( TEXT("STATIC"), QuestionLine,
			  WS_VISIBLE | WS_CHILD | WS_BORDER,
			  10, 10, 200, 100,
			  hwnd, (HMENU) 1, NULL, NULL);

          return 0;
      }

      case WM_DESTROY:
      {
          PostQuitMessage(0);
          return 0;
      }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

void CenterWindow( HWND hwnd )
{
    RECT rc ;
    
    GetWindowRect ( hwnd, &rc ) ;
    
    SetWindowPos( hwnd, 0, 
        (GetSystemMetrics(SM_CXSCREEN) - rc.right)/2,
        (GetSystemMetrics(SM_CYSCREEN) - rc.bottom)/2,
         0, 0, SWP_NOZORDER|SWP_NOSIZE );
}


This is where my problem really is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
		  string line;

		  while( getline( in, line ) )
		  {
			  problem.chapter_data = line;
			  getline( in, line );
			  problem.question_data = line;
			  getline( in, line );
			  problem.answer_data = line;
		  }
		  TCHAR QuestionLine = problem.question_data;

		  /*for( unsigned i = 0; i < 100; ++i )
		  {
			  if( i < problem.question_data.length() )
				  QuestionLine[i] = problem.question_data[i];
			  else
				  QuestionLine[i] = ' ';
		  }*/

		  QuestionBox = CreateWindow( TEXT("STATIC"), QuestionLine,
			  WS_VISIBLE | WS_CHILD | WS_BORDER,
			  10, 10, 200, 100,
			  hwnd, (HMENU) 1, NULL, NULL);
A TCHAR is not a string. It's a macro that's defined as a char or wchar_t depending on whether UNICODE is defined.
The solution is kinda ugly:
1
2
3
4
5
6
std::string str="something";
TCHAR *param=new TCHAR[str.size()+1];
param[str.size()]=0;
//As much as we'd love to, we can't use memcpy() because
//sizeof(TCHAR)==sizeof(char) may not be true:
std::copy(str.begin(),str.end(),param);
Topic archived. No new replies allowed.