wchar* question

div0 div0 at users.sourceforge.net
Thu Sep 3 14:21:23 PDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Max Samukha wrote:
> Sam Hu wrote:
> 
>> Given below code(Win32 SDK):
>>
>> int /*LRESULT*/ wndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
>> {
>> switch(msg)
>> {
>> case WM_LBUTTONDOWN:
>> {
>> wchar* szFileName=cast(wchar*)(new wchar[1024]);// ***questioned line
>> HINSTANCE hInstance=GetModuleHandleW(null);
>> GetModuleFileNameW(hInstance,szFileName,1024);
>> MessageBoxW(null,cast(const wchar*)szFileName, "Full Path:",
>> MB_ICONINFORMATION);
>> }
>>
>> break;
>> }
>>
>> My program runs OK and can print the exe path as expected.But if I change
>> wchar* szFileName=cast(wchar*)(new wchar[1024]);
>>
>> to
>> wchar* szFileName;
>>
>> The program runs also but prints blank.Why?
>>
>> Thanks for your help in advance.
>> Regards,
>> Sam
> 
> I guess MessageBox checks if the pointer to the message text is NULL. If it 
> is NULL, it prints nothing.
> 
> You should check the return value of GetModuleFileName. It may fail for 
> various reasons. I guess it fails if you pass a null pointer to it.

Exactly; MSDN is your friend:

http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx

The szFileName parameter is a pointer to a buffer that
GetModuleFileNameW *fills*. As you've passed a null pointer,
there is no buffer to fill.

The fact your program isn't crashing when you call MessageBox is because
windozes checks for it.

Which arguably it shouldn't. If it had crashed it would probably have
helped you work out what was wrong.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFKoDNTT9LetA9XoXwRAiZ0AJ9ooxiI2JB4O8gRZGlNpuZlTecncwCePcVA
TwPQBMZ4QMhjn/dR7bw67kA=
=mtJ+
-----END PGP SIGNATURE-----


More information about the Digitalmars-d-learn mailing list