www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - wchar* question

reply Sam Hu <samhudotsamhu gmail.com> writes:
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
next sibling parent Sam Hu <samhudotsamhu gmail.com> writes:
Sorry.Under  dmd2.031 ,windows XP
Sep 03 2009
prev sibling parent reply Max Samukha <spambox d-coding.com> writes:
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. 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
parent reply div0 <div0 users.sourceforge.net> writes:
-----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-----
Sep 03 2009
parent Sam Hu <samhudotsamhu gmail.com> writes:
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