Dynamic array doesn't work properly

Started by
1 comment, last by Xeile 19 years, 6 months ago
I'm using MS Visual C++ 6.0

void DragFiles( HWND hWnd, HDROP hDrop)
{

	TCHAR szFileName[_MAX_PATH + 1];
	DragQueryFile( hDrop, 0, (LPTSTR)szFileName, _MAX_PATH + 1 );
	OFSTRUCT sOFile;
	HFILE hFile = OpenFile( (LPTSTR)szFileName, &sOFile, OF_READ );
	
	DWORD BytesToRead;
	BytesToRead = GetFileSize( (void*)hFile, NULL );
	TCHAR* lpBuffer = new TCHAR[BytesToRead]; // Problematic
	ULONG lpBytesRead;
	if( !( ReadFile( (void*)hFile, &lpBuffer , BytesToRead, &lpBytesRead, NULL ) ) )
	{
		return;
	}
}

When I want to use a new operator to declare new TCHAR array, the function doesn't work properly anymore. When I execute this, then the reading-result should be in lpBuffer. But instead it is assigning it in the szFileName array. What am I doing wrong?
Advertisement
read this line carefully:
if( !( ReadFile( (void*)hFile, &lpBuffer , BytesToRead, &lpBytesRead, NULL ) ) )


You are sending the adress of your pointer to ReadFile. The pointer should be sent by value, that way readfile will write to wherever the pointer is pointing to.
You are right! Stupid mistake. I now know what I did and how to solve it. Thank you for the reply.

This topic is closed to new replies.

Advertisement