Click to See Complete Forum and Search --> : Can not compile this code....


housefreak
June 8th, 2004, 03:15 AM
Why can't compile this Code:


// dirCon.cpp : Definiert den Einsprungpunkt für die Konsolenanwendung.
//
#if WINVER < 0x0400
#define WINVER 0x0400
#endif

#include "stdafx.h"

#include <Windows.h>
#include <iostream>

using namespace std;


int main(int argc, char* argv[])
{
HANDLE file;
file = FindFirstChangeNotification("C:\\test", TRUE, FILE_NOTIFY_CHANGE_FILE_NAME);

WaitForSingleObject(file, INFINITE);
cout << "treffer mit datei\n";

while (true)
{
FindNextChangeNotification(file);
WaitForSingleObject(file, INFINITE);
cout << "treffer mit Datei\n";
}

FindCloseChangeNotification(file);

////////////////////////////////////////////////////////////
HANDLE hDir = CreateFile("C:\\test",
FILE_LIST_DIRECTORY,
FILE_SHARE_READ || FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

FILE_NOTIFY_INFORMATION Buffer[1024];
DWORD BytesReturned;

while (ReadDirectoryChangesW(hDir, &Buffer, sizeof(Buffer),
TRUE,
FILE_NOTIFY_CHANGE_SECURITY |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_FILE_NAME,
&BytesReturned,
NULL,
NULL))
{
//
}
return 0;
}


I get the following error, that the function ReadDirectoryChangesW() is never declared? Can anyone help me why?

I use Win2k

Loada
June 8th, 2004, 03:59 AM
You must be using UNICODE for it to work.
Make sure you have _UNICODE and UNICODE in your pre-processor project settings.

housefreak
June 8th, 2004, 04:07 AM
Thats now my pre-processor settings:

WIN32,_DEBUG,_CONSOLE,_MBCS,_UNICODE

and it never work with this settings? why?

Mick
June 8th, 2004, 09:36 AM
1. If you are using precompiled headers <stdafx.h> or <whateverpcheader> do not put anything before them.
2. ReadDirectoryChangesW(...) is wrapped in a:
#if(_WIN32_WINNT >= 0x0400)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/using_the_windows_headers.asp

housefreak
June 8th, 2004, 10:17 AM
i added the preprocessor option but this code never works too:


// dirCon.cpp : Definiert den Einsprungpunkt für die Konsolenanwendung.
//
#if (_WIN32_WINNT >= 0x0400)
#define WINVER 0x0400
#endif
#include "stdafx.h"

#include <afx.h>


#include <process.h>
#include <afxmt.h>
#include <AFXPRIV.H>

#include <windows.h>
#include <iostream>





using namespace std;

int main(int argc, char* argv[])
{
HANDLE file;
file = FindFirstChangeNotification(CString("c:\\test"), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME);

WaitForSingleObject(file, INFINITE);
cout << "treffer mit datei\n";

while (true)
{
FindNextChangeNotification(file);
WaitForSingleObject(file, INFINITE);
cout << "treffer mit Datei\n";
}

FindCloseChangeNotification(file);

////////////////////////////////////////////////////////////
USES_CONVERSION;
HANDLE hDir = CreateFile(CString("C:\\test"),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

FILE_NOTIFY_INFORMATION Buffer[1024];
DWORD BytesReturned;

while (ReadDirectoryChangesW(hDir,
&Buffer,
sizeof(Buffer),
TRUE,
FILE_NOTIFY_CHANGE_SECURITY |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_FILE_NAME,
&BytesReturned,
NULL,
NULL))
{
//
}
return 0;
}



why

Mick
June 8th, 2004, 10:45 AM
You need to define _WIN32_WINNT to be 0x400 or greater. And DO NOT PUT any includes or defines or whatever before your precompiled header that is: #include <stdafx.h>

housefreak
June 8th, 2004, 10:52 AM
i do that what you say but it never work?

this is my actual code:

// dirCon.cpp : Definiert den Einsprungpunkt für die Konsolenanwendung.
//


#include "stdafx.h"

#include <process.h>
#include <AFXPRIV.H>

#include <windows.h>
#include <iostream>

#define _WIN32_WINNT >= 0x0400


using namespace std;

int main(int argc, char* argv[])
{
HANDLE file;
file = FindFirstChangeNotification(CString("c:\\test"), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME);

WaitForSingleObject(file, INFINITE);
cout << "treffer mit datei\n";

while (true)
{
FindNextChangeNotification(file);
WaitForSingleObject(file, INFINITE);
cout << "treffer mit Datei\n";
}

FindCloseChangeNotification(file);

////////////////////////////////////////////////////////////
USES_CONVERSION;
HANDLE hDir = CreateFile(CString("C:\\test"),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

FILE_NOTIFY_INFORMATION Buffer[1024];
DWORD BytesReturned;

while (ReadDirectoryChangesW(hDir,
&Buffer,
sizeof(Buffer),
TRUE,
FILE_NOTIFY_CHANGE_SECURITY |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_FILE_NAME,
&BytesReturned,
NULL,
NULL))
{
//
}
return 0;
}


Is that alright or where is the error?

Paul McKenzie
June 8th, 2004, 10:58 AM
The following program compiles with no problems if the preprocessor definition is used:

#include <windows.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
HANDLE file;
file = FindFirstChangeNotification("c:\\test", TRUE, FILE_NOTIFY_CHANGE_FILE_NAME);

WaitForSingleObject(file, INFINITE);
cout << "treffer mit datei\n";

while (true)
{
FindNextChangeNotification(file);
WaitForSingleObject(file, INFINITE);
cout << "treffer mit Datei\n";
}

FindCloseChangeNotification(file);

////////////////////////////////////////////////////////////
HANDLE hDir = CreateFile("C:\\test",
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

FILE_NOTIFY_INFORMATION Buffer[1024];
DWORD BytesReturned;

while (ReadDirectoryChangesW(hDir,
&Buffer,
sizeof(Buffer),
TRUE,
FILE_NOTIFY_CHANGE_SECURITY |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_FILE_NAME,
&BytesReturned,
NULL,
NULL))
{
//
}
return 0;
}

1) You don't need all those headers. If you turn off pre-compiled headers for the moment, and comment out stdafx.h, what results do you get?

2) Why are you using CString? Just use the literal. I hope you weren't using MFC just for those unneccessary uses of CString. In any event, use std::string instead of CString if you're going to code a Win32 application.

Regards,

Paul McKenzie

Mick
June 8th, 2004, 10:59 AM
#define _WIN32_WINNT 0x0400

do that before you include any system headers (as they may #include <winbase.h> indirectly where ReadDirectoryChangesW lives at this moment) but not before you include your precompiled headers, or just move it into your precompiled headers <stdafx.h>...or add it to your preprocessors in the c++ settings.

cvogt61457
June 8th, 2004, 11:00 AM
You say it doesn't work ??
What errors are you getting?

What exactly do you mean? Be specific !!!

housefreak
June 8th, 2004, 11:06 AM
when i use the code as follows:

// dirCon.cpp : Definiert den Einsprungpunkt für die Konsolenanwendung.
//
#include "stdafx.h"

#define _WIN32_WINNT >= 0x0400

#include <process.h>
#include <AFXPRIV.H>

#include <windows.h>
#include <iostream>



using namespace std;

int main(int argc, char* argv[])
{
HANDLE file;
file = FindFirstChangeNotification(CString("c:\\test"), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME);

WaitForSingleObject(file, INFINITE);
cout << "treffer mit datei\n";

while (true)
{
FindNextChangeNotification(file);
WaitForSingleObject(file, INFINITE);
cout << "treffer mit Datei\n";
}

FindCloseChangeNotification(file);

////////////////////////////////////////////////////////////
USES_CONVERSION;
HANDLE hDir = CreateFile(CString("C:\\test"),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

FILE_NOTIFY_INFORMATION Buffer[1024];
DWORD BytesReturned;

while (ReadDirectoryChangesW(hDir,
&Buffer,
sizeof(Buffer),
TRUE,
FILE_NOTIFY_CHANGE_SECURITY |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_FILE_NAME,
&BytesReturned,
NULL,
NULL))
{
//
}
return 0;
}


i get that error: fatal error C1017: Ungueltiger Ausdruck fuer Ganzzahlkonstante


when i use the code shown 2 posts above i get the error that the function ReadDirectoryChangesW() is an unknwon argument!

hope thats specific enough!!

Thanks much

Paul McKenzie
June 8th, 2004, 11:07 AM
To housefreak,

I defined the constant that Mick stated in the Project Options preprocessor definitions. Here is what is stated in those preprocessor definitions:

WIN32,_DEBUG,_CONSOLE,_MBCS,_AFXDLL,_WIN32_WINNT=0x0500

Is this what you have?

The ReadDirectoryChangesW gets recognized by the compiler without all of those extra headers you have in your code. The code I posted, plus the preprocessor settings, compiles the code with no problems.

Maybe if you show us exactly where you added these definitions, and how you added them (did you make a mistake, i.e. a typo?)

Regards,

Paul McKenzie

Paul McKenzie
June 8th, 2004, 11:09 AM
when i use the code shown 2 posts above i get the error that the function ReadDirectoryChangesW() is an unknwon argument!

hope thats specific enough!!

Thanks much You are getting closer with the code that I posted. Now, the reason why you have an error is that you are not setting the preprocessor define correctly. Either you mistyped it in your settings, or something else. But it isn't being recognized.

Regards,

Paul McKenzie

MrViggy
June 8th, 2004, 11:10 AM
The mistake is what Mick pointed out earlier. Don't do:

#define _WIN32_WINNT >= 0x0400

Do:

#define _WIN32_WINNT 0x0400

Better yet, look at Paul's example. It is a *lot* cleaner!

Viggy