digitalmars.D.learn - wchar* question
- Sam Hu (24/24) Sep 03 2009 Given below code(Win32 SDK):
- Sam Hu (1/1) Sep 03 2009 Sorry.Under dmd2.031 ,windows XP
- Max Samukha (8/37) Sep 03 2009 I guess MessageBox checks if the pointer to the message text is NULL. If...
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
Sep 03 2009
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, SamI 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. Also, you don't have to call GetModuleHandle to get the handle of the calling process' module. Just pass NULL as first argument to GetModuleFileName.
Sep 03 2009
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Max Samukha wrote:Sam Hu wrote: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-----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, SamI 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.
Sep 03 2009
Hi both: Thank you so much for your help! ps:just found other than write : wchar* szFileName=cast(wchar[])(new wchar[1024); It works also if I write: wchar[1024] szFileName; I am annoying everytime when pass D strings to/from OS API... Regards, Sam
Sep 03 2009