Sign in to follow this  
Xeile

Dynamic array doesn't work properly

Recommended Posts

Xeile    256
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
IMG25    122
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this