RE: [PATCH] reg.exe: Modernise print functions
On Monday, 10 November 2014, Jonathan Vollebregt wrote:
WCHAR msg_buffer[1024];
No easy way to get the expected size and dynamically allocate the output I take it?
I looked into it before but besides _vscprintf (In msvcrt, which we're not allowed to use) there doesn't seem to be a way to get the expected output size besides perhaps growing the buffer over and over again until it fits (Which seems inefficient for a simple printing function)
One possible way would be to avoid using vsnprintfW() and pass everything through to output_vprintf(). static void __cdecl reg_printfW(const WCHAR* msg, ...) { __ms_va_list va_args; __ms_va_start(va_args, msg); output_vprintf(msg, va_args); __ms_va_end(va_args); } This would have the advantage of using FormatMessage() to allocate memory. But we would have to replace all format specifiers (four or five?) with their FormatMessage() equivalents. static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','x',' ',' ','d','a','t','a',' ','%','s','\n',0}; becomes static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','1','!','x','!',' ',' ','d','a','t','a',' ','%','2','\n',0};
That was my thought, but it doesn't appear to be parsing printf parameters properly: Sticking this in main:
static const WCHAR testW[] = {'w','0','o','t','\n','%','s','\n',0}; reg_printfW(testW, deleteW);
Results in the output:
w0ot s
On 11/10/2014 01:10 PM, Hugh McMaster wrote:
On Monday, 10 November 2014, Jonathan Vollebregt wrote:
WCHAR msg_buffer[1024];
No easy way to get the expected size and dynamically allocate the output I take it?
I looked into it before but besides _vscprintf (In msvcrt, which we're not allowed to use) there doesn't seem to be a way to get the expected output size besides perhaps growing the buffer over and over again until it fits (Which seems inefficient for a simple printing function)
One possible way would be to avoid using vsnprintfW() and pass everything through to output_vprintf().
static void __cdecl reg_printfW(const WCHAR* msg, ...) { __ms_va_list va_args;
__ms_va_start(va_args, msg); output_vprintf(msg, va_args); __ms_va_end(va_args); }
This would have the advantage of using FormatMessage() to allocate memory. But we would have to replace all format specifiers (four or five?) with their FormatMessage() equivalents.
static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','x',' ',' ','d','a','t','a',' ','%','s','\n',0};
becomes
static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','1','!','x','!',' ',' ','d','a','t','a',' ','%','2','\n',0};
On Monday, 10 Nov 2014 13:17:56 +0100, Jonathan Vollebregt wrote:
That was my thought, but it doesn't appear to be parsing printf parameters properly:
Sticking this in main:
static const WCHAR testW[] = {'w','0','o','t','\n','%','s','\n',0}; reg_printfW(testW, deleteW);
Results in the output:
w0ot s
At a quick glance, %s should be %1. FormatMessage() accepts numerals.
Unfortunately if I pass in anything besides a pointer it will segfault. It actually wants a format string "%1!d!" for ints which is hardly compact (Especially since we have to type it out in a WCHAR array) There must be a better way to get the size of the string... On 11/10/2014 01:44 PM, Hugh McMaster wrote:
On Monday, 10 Nov 2014 13:17:56 +0100, Jonathan Vollebregt wrote:
That was my thought, but it doesn't appear to be parsing printf parameters properly:
Sticking this in main:
static const WCHAR testW[] = {'w','0','o','t','\n','%','s','\n',0}; reg_printfW(testW, deleteW);
Results in the output:
w0ot s
At a quick glance, %s should be %1. FormatMessage() accepts numerals.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Am 2014-11-10 14:05, schrieb Jonathan Vollebregt:
Unfortunately if I pass in anything besides a pointer it will segfault. It actually wants a format string "%1!d!" for ints which is hardly compact (Especially since we have to type it out in a WCHAR array)
There must be a better way to get the size of the string... Fwiw, I cannot really comment on this patch because I have no experience with output handling. I'm afraid the two of you have to come to an agreement on your own.
Just two general observations: *) The entire console writing code is terribly verbose, but I guess that's the Windows API's fault. What may help is a helper file similar to include/wine/list.h that contains this code, so it doesn't have to be re-coded in every program. I haven't looked at the individual implementations though. *) I see the static buffer passed to LoadString as a relatively minor issue because the length of those strings is fixed. Translations may cause problems I guess. Does SizeofResource() help here? I'm a bit confused re HMODULE and HINSTANCE. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQIcBAEBAgAGBQJUZLzJAAoJEN0/YqbEcdMwDrMP/Anod8C7HBg5lm+6N8dsitLK x+43aRa4lFEpZjQfpCf8TM3GMH7QupWppDh6tVYjRR9jKkAv003aTJRhqJMAHprH tq84pGMaQVi7I0ZDVoUL/svWX7wj/HTts6VHhs1dfheREi7LE08L8AG3pcU/rFn5 r2P9UhSHyBtIhn+XQSQVn+Ssf2bwoqPcuY/C9w1aynt9qUpuPTxUPOPFtLZvzBLo 1fIWFZGFTzpf3qQk8saeSMNLT09NKscDhQC7jfkTHlRAbdJxK1fbD0QFgcAlZVzP dHNx0TqJENTCvMS9ZSYhvOB6EQQB9mQH342RkMzkWMKc6cSEp9i7ts3B7hLy83uB 53OILAVKuFWc9mocjCmhKy2WgvhAMxYozw3sHi/n46vulRDxloMjMvJn2tKF7lmA wghxYnzPibtLgFT1XiRQFLjL5oADOfu86rTn4p1NvA9oACVIqA/eClbzcQUjgnz3 uKiM/Noy3zCGeYg4wrHf4dKnsXz8exSYXPQwCl0hIH04ccZgANqz8a+dN1pYj+BZ Pn00z8K2vg+Ym7YSgMn/8Sc1JpvF+uY7thoWQPJOZZ2mMGyuMoC7TpbDyd/E69rl 9+HybOJZH2aWonU6aJ4C0CC4SNpYXGZByYskyHgf5cGDLrD469mAISyysFO1AATX KW09FeePhDwdtC6CzynL =pPPj -----END PGP SIGNATURE-----
participants (3)
-
Hugh McMaster -
Jonathan Vollebregt -
Stefan Dösinger