Need help with converting string to char for SendMessage()

I'm somewhat new to C++ and need some help. I am trying to fill a combobox with character names that are stored in a string vector inside of a Class "Acc". I need help converting that string to a char to be used in the SendMessage fuction.

I've tried several different solutions from this site and others and have had no success. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void MainWindow::FillAUCharacterCombo()
{
	TCHAR CName;
	string strtest;
	int ListSize;
	ListSize = Acc.GetCharacterListSize(); //Acc represent the class that has the characternames.

	for (int x=1; x<ListSize; x++)
	{
		strtest = Acc.GetCharacterName(x);
		
		SendMessage(AUCharacterCombo, CB_ADDSTRING, 0, (LPARAM)CName);

	}
}
as specified in the standard:
a classes declaration doesn't create any physical instance of that class.
the process of creating a physical instance of that class is called Instantiation.

unless GetCharacterListSize() is a static function, this expression is false:
ListSize = Acc.GetCharacterListSize(); //false

maybe you mean this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void MainWindow::FillAUCharacterCombo(Acc ob)
{
	TCHAR CName;
	string strtest;
	int ListSize;
	ListSize = ob.GetCharacterListSize();

	for (int x=1; x<ListSize; x++)
	{
		strtest = ob.GetCharacterName(x);
		
		SendMessage(AUCharacterCombo, CB_ADDSTRING, 0, (LPARAM)CName);

	}
}
Rechard3.

Maybe I didn't explain myself well enough. the Acc is an object of a class called Account. This class/object holds the character names and other info that I will use for the main program.

All the code i posted works except for the SendMessage function. I have not been able to get strtest to convert over to Cname. That is the issue i need help with.
@Dragonstar:
the Acc is an object of a class called Account

so it's an object, you can't call it class.
that cleared your intention, maybe i missed the real problem in this code, i think i see it now:
inside the loop, you assign the new letter to the strtest, this makes the strtest contain only one letter, then you are sending CName as an argument to SendMessage().
you can do either of these:
replace line 10 of your code with this:
CName = Acc.Acc.GetCharacterName(x);
this will send one character in each call to SendMessage().

another way is, you can follow this:
1
2
3
4
5
for (int x=1; x<ListSize; x++)
{
	strtest += ob.GetCharacterName(x);				
}
SendMessage(AUCharacterCombo, CB_ADDSTRING, 0, (LPARAM)strtest.c_str());


if this isn't what you need, maybe we could use the declaration of account and its functions.
@Rechard3

hmmm... we definetaly not on same page. i'll walk through the code the best i can. I may be using terms like class incorrectly so bear with me please.

1
2
3
4
5
6
void MainWindow::FillAUCharacterCombo()
{
	TCHAR CName;
	string strtest;
	int ListSize;
	

These are obvious, declaring function variable.

ListSize = Acc.GetCharacterListSize(); //Acc represent the class that has the characternames.
This line is calling the function "GetCharacterListSize() from the Class i call Account. This class will hold all the account information for my program (this is read in from a text file when my program starts up. Character Names is one of variables of Account which is declared as a Vector <string>. so basicaly this line gets me the size of the Vector <string> CharacterNames.
1
2
3
4
5
6
for (int x=1; x<ListSize; x++)
	{
		
	SendMessage(AUCharacterCombo, CB_ADDSTRING, 0, (LPARAM)CName);

	}
The for loops will cycles through until i reach the Listsize,

strtest = Acc.GetCharacterName(x);
this line retrieve the charactername which is a string and puts into strtest.
1
2
3
SendMessage(AUCharacterCombo, CB_ADDSTRING, 0, (LPARAM)CName);

	}

This is where my problem is. I need to convert the strtest (string) into Cname (tchar) so that sendmessage will work.

I hope this explains my situation better.
closed account (Dy7SLyTq)
what dies cname take for a constructor?
@DTSCode
what dies cname take for a constructor?


not sure what you mean hear? I basically want to convert strTest (a string) into CName(a Tchar) so that i can get SendMessage to work as it won't take strTest at all.
closed account (Dy7SLyTq)
sorry i thought cname was a class. have you tried (tchar) strTest
are you saying to do this
CName = (TCHAR) strtest;

if so I get the following error:
Error: no suitable conversion function from "std::string" to "TCHAR" exists
closed account (Dy7SLyTq)
i was but then try
cname = dynamic_cast<tchar> (&strtest)
No luck

1
2
typedef WCHAR TCHAR
Error: the type in a dynamic_cast must be a pointer or reference to a complete class type, or void *
closed account (Dy7SLyTq)
sorry i dont know then
1
2
3
4
5
void MainWindow::FillAUCharacterCombo()
{
	TCHAR CName;
	string strtest;
	int ListSize;



These are obvious, declaring function variable.


Not quite so obvious. I don't know of many names that consist of a single character (as CName does.)

I don't know why you're messing around with all of this stuff. Don't use TCHAR. You're using strings that don't consist of wide characters judging from the code, so use the narrow version of the WINAPI functions.

1
2
3
4
5
void MainWindow::FillAUCharacterCombo(Acc ob)
{
    for ( int i=0; i<ob.GetCharacterListSize(); ++i )
        SendMessageA(AUCharacterCombo, CB_ADD_STRING, 0, (LPARAM)ob.GetCharacterName(i).c_str()) ;
}

Last edited on
@Chervil
CName = strtest.c_str();
I get the following when trying the above
Error: a value of type "const char*" cannot be assigned to an entity of type "TCHAR"

@Cire

That worked but i have no clue why, since other attempts of me using strtest.c_str() did not? Would you mind explaining why it does work. And Thank you very much.
My guess would be that you have your project set up to use Unicode. If it is then SendMessage will map to SendMessageW. Since your string doesn't consist of wide characters, this obviously causes the data to be misinterpreted. Using the ANSI version of the function, SendMessageA, sidesteps the problem.
Topic archived. No new replies allowed.