unicode sprintf problem

Hello

I've written the following function as an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Buf is a 4k wchar_t null-terminated string
//edit is a textedit window handle
int _log(wchar_t* str, ...){
	va_list va;
	va_start(va, str);
	LPWSTR tmp =(LPWSTR) malloc(sizeof(wchar_t)*2048);
	_snwprintf(tmp, 2048 str, va);
	_snwprintf(client->buf, 4*1024, L"%s %s\r\n", buf, tmp);
	free(tmp);
    SendMessage(edit, WM_SETTEXT, 0, (LPARAM)buf); 
	va_end(va);	
	return 0;
}
//in the owner thread...
_log(L"The following numbers will be random - %d, %u", 1,1337);


This will print
The following numbers will be random - 1242908,1244460


The problem is obvious, cant seem to find the solution and I'm thinking of writing my own wsprintf...


SOLUTION Use _vswprintf!

I was calling this function (updated, with _vs version) with va_list so I actualy made the same mistake... All thanks go to Duoas

Thanks,

Greg
Last edited on
You can't use a va_list like that. You want the _vsnwprintf() function.
http://www.google.com/search?btnI=1&q=msdn+_vsnwprintf

Some additional notes --

Line 7 appears to be missing a comma. I hope that's just a typo when you posted.
Line 8 is using a variable buf which is neither declared local to your function nor is it locally initialized with any value. I hope that is intentional... I'd give a global variable a better name that 'buf'.

Hope this helps.
Thanks for the reply. I wrote the snippet from scratch, here is the original code, now with _vsnw

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//getTime() returns a LPWSTR allocated with malloc
int _log(CLIENT* client, wchar_t* str, ...){//since it's a logging function I wont hold back on safety checks
	if(!(str&&client))
		return -1;
	if(!(client->buf&&client->main&&client->edit))
		return -1;
	va_list va;
	va_start(va, str);
	LPWSTR tmp =(LPWSTR) malloc(sizeof(wchar_t)*2048);
	LPWSTR time=getTime();
	_vsnwprintf(tmp, 2048, str, va);
	_snwprintf(client->buf, 65536,  L"%s[%s] %s\r\n", client->buf, time, tmp);
	free(time);
	free(tmp);
    SendMessage(client->edit, WM_SETTEXT, 0, (LPARAM)client->buf); 
	va_end(va);	
	return 0;
}


Same problem
I am not able to understand your problem.. can you elaborate?
arey you trying to write a unicode strings?
Yes. sizeof(wchar_t) per character
Look@post 2
Last edited on
Topic archived. No new replies allowed.