Thread overview
wchar* question
Sep 03, 2009
Sam Hu
Sep 03, 2009
Sam Hu
Sep 03, 2009
Max Samukha
Sep 03, 2009
div0
Sep 04, 2009
Sam Hu
September 03, 2009
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

September 03, 2009
Sorry.Under  dmd2.031 ,windows XP
September 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,
> 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.

September 03, 2009
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
September 04, 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