• Advertisement
Sign in to follow this  

Dynamic array doesn't work properly

This topic is 4875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You are right! Stupid mistake. I now know what I did and how to solve it. Thank you for the reply.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement