Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I am new to using win32 and I am having some issues trying to get the function FindFirstFile to work.

I am wondering if someone can point out what I am doing wrong with the following code:

LPCTSTR lpFileName = L"C:\\temp.txt";
LPWIN32_FIND_DATA lpFindFileData = {0};
		
		if( INVALID_HANDLE_VALUE == FindFirstFile(lpFileName, lpFindFileData))
		{
				cerr << "error:" << GetLastError() ;
		}


The previous code compiles, however, I get a access violation exception for the address of the variable lpFindFileData.

I have also tried using the address of lpFindFileData like this:
if( INVALID_HANDLE_VALUE == FindFirstFile(lpFileName, &lpFindFileData))


The previous code, however, will not build. I get the error message:
CSS
error C2664: 'FindFirstFileW' : cannot convert parameter 2 from 'LPWIN32_FIND_DATA *' to 'LPWIN32_FIND_DATAW'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


If anyone is willing to help me overcome this learning curve it will be greatly appreciated!!!

robNo.

Edit: Perhaps it would be beneficial to mention that I am using Visual Studio 2010 to compile and build this code.
Posted
Updated 20-May-11 18:02pm
v2
Comments
Richard MacCutchan 21-May-11 4:36am    
The version of Visual Studio is irrelevant, it's the programming language and version that matters.

NOOO, you are using a NULL pointer there: lpFindFileData = {0}; You either (probably) need to do lpFindFileData = new WIN32_FIND_DATA (I didn't try this personally), OR declare a WIN32_FIND_DATA structure and use its address in the call for FindFirstFile(), like this:
WIN32_FILE_DATA fdata;
FindFirstFile(lpFileName, &fdata);

The rest remains the same.
Hope you understand my english :)
 
Share this answer
 
Comments
RobNO 21-May-11 11:03am    
Thanks for your reply!

Your English was great, I understood what you meant perfectly and it worked. So far I have just tried using new and delete. Once i get some more time ill try to declare the whole structure and pass it by the address. I will let you know the outcome!

Thanks for everything! your help is greatly appreciated!
i do not work that much with c/c++ but it look like pointer porblem. Try the code below

//LPWIN32_FIND_DATA lpFindFileData
LPWIN32_FIND_DATA *lpFindFileData
 
Share this answer
 
Comments
RobNO 21-May-11 0:13am    
Thanks for your reply!
trying this:
LPWIN32_FIND_DATAW* lpFindFileData = {0};
if( INVALID_HANDLE_VALUE == FindFirstFile(lpFileName, lpFindFileData)) ..
, gives me the error message:
cannot convert parameter 2 from 'LPWIN32_FIND_DATAW *' to 'LPWIN32_FIND_DATAW'

trying this:
LPWIN32_FIND_DATAW* lpFindFileData = {0};

if( INVALID_HANDLE_VALUE == FindFirstFile(lpFileName, &lpFindFileData))

gives me the error message:
cannot convert parameter 2 from 'LPWIN32_FIND_DATAW **' to 'LPWIN32_FIND_DATAW'

and trying this:
LPWIN32_FIND_DATAW* lpFindFileData = {0};
if( INVALID_HANDLE_VALUE == FindFirstFile(lpFileName, *lpFindFileData))

compiles! However, i get the exception: Access violation reading location 0x00000000.
Which, is the address of lpFindFileData.

Thanks for the idea!
CS2011 21-May-11 0:36am    
not sure about this exception. but look like you are trying to read some other memory location(it just my view i might be wrong)
Richard MacCutchan 21-May-11 4:30am    
You created a pointer but did not initialise it; see my answer below.
C++
LPCTSTR lpFileName = L"C:\\temp.txt";
LPWIN32_FIND_DATA lpFindFileData = {0};
		
		if( INVALID_HANDLE_VALUE == FindFirstFile(lpFileName, lpFindFileData))
		{
				cerr << "error:" << GetLastError() ;
		}

LPWIN32_FIND_DATA is a pointer to a WIN32_FIND_DATA structure so it needs to point to something; you have initialised it to NULL. Try adding before your if statement:
C++
lpFindFileData = new WIN32_FIND_DATA;

alternatively you could instantiate the structure on the stack and reference it by address thus:
C++
WIN32_FIND_DATA findFileData;
		
	if(INVALID_HANDLE_VALUE == FindFirstFile(lpFileName, &findFileData))
...
 
Share this answer
 
Comments
RobNO 21-May-11 11:04am    
Thanks for your reply!

This:
lpFindFileData = new WIN32_FIND_DATA;

works perfectly.

Thanks for your help!

robNO
Richard MacCutchan 21-May-11 14:21pm    
Happy to help, but I would suggest you spend some time looking at the structures used in Windows, and the difference between StructType and lpStructType.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900