[win32 api]wsprintf/sprintf/sprintf_s stopping application

Aug 4, 2012 at 10:04pm
Hello,

I'm trying to do something like a countdown timer where you set a time and start it, however I'm having strange problems with setting time, I have static control with "00:00:00" as base string, this is my code for setting time

1
2
3
4
					int dlgres = DialogBox(hInst2, MAKEINTRESOURCE(IDD_SETTIME), hWnd, SetTimeDlgProc);
					char tmp[128];
					wsprintf(tmp, "%s:%s:%s", sHour, sMinute, sSecond);
					SendMessage(GetDlgItem(hWnd, IDC_TIME1), WM_SETTEXT, NULL, (LPARAM)tmp);


from those "red debug dots" you can add at the left that will show you if code is running I've noticed that it stops at wsprintf, so it won't do SendMessage function

i'm not getting any errors at build or in debug output

my SetTimeDlgProc

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
BOOL CALLBACK SetTimeDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_INITDIALOG:
		return TRUE;
	case WM_DESTROY:
		{
			EndDialog(hWnd, IDCANCEL);
			return 0;
		}
		break;
	case WM_COMMAND:
		{
			switch(LOWORD(wParam))
			{
				case IDOK:
				{
					GetDlgItemText(hWnd, IDC_HOUR, (LPSTR)sHour.c_str(), 5);
					GetDlgItemText(hWnd, IDC_MINUTE, (LPSTR)sMinute.c_str(), 5);
					GetDlgItemText(hWnd, IDC_SECOND, (LPSTR)sSecond.c_str(), 5);

					EndDialog(hWnd, IDOK);
					break;
				}
				case IDCANCEL:
				{
					EndDialog(hWnd, IDCANCEL);
					break;
				}
			}
			break;
		}
	default:
		return FALSE;
	}
	return TRUE;
}


if I add MessageBox after GetDlgItemText (in SetTimeDlgProc) it is returning string as it should, but if I add wsprintf there everything is fcked up and it stops working there too

I've tried to add sprintf and sprintf_s but same result..

(I'm using multibyte character set)
Last edited on Aug 4, 2012 at 10:19pm
Aug 4, 2012 at 10:41pm
I don't get it. Where are you putting a MessageBox and which string are you showing in it? This part is not clear to me.
Aug 4, 2012 at 11:17pm
1
2
3
GetDlgItemText(hWnd, IDC_HOUR, (LPSTR)sHour.c_str(), 5);
					GetDlgItemText(hWnd, IDC_MINUTE, (LPSTR)sMinute.c_str(), 5);
					GetDlgItemText(hWnd, IDC_SECOND, (LPSTR)sSecond.c_str(), 5);


after one of those and one of those strings (sHour, sMinute or sSecond), but that's not the problem, everything AFTER ANY sprintf function will stop working, so any sprintf function (wsprintf, sprintf, sprintf_s) is stopping my application...
Aug 4, 2012 at 11:23pm
Ah, I just noted the issue: You think you can pass a std::string object (a C++ object) to a C function like wsprintf(). You need to pass the return value of c_str(). It is called that for a reason: It returns a C string. C functions use C strings.

wsprintf(tmp, "%s:%s:%s", sHour.c_str(), sMinute.c_str(), sSecond.c_str());

Well, that's weird now that I think about it. The C++ compiler should have complained about this. Are you sure that compiled for you? Soooo weird. Did you force something by typecasting?
Last edited on Aug 4, 2012 at 11:24pm
Aug 4, 2012 at 11:25pm
I thought about this too, but I told myself that compiler should tell me it too since I HAVE to use .c_str() in, for example, GetDlgItemText, but it didn't tell me anything so I left it like it is, and yes, it's compiling without any errors or warnings
Aug 5, 2012 at 12:01am
Right. Now that I remember, wsprintf() and related functions take those in the variable parameters, so no type checking there. So yes, use .c_str() and your problem will be solved.
Topic archived. No new replies allowed.