Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2006, 1:59 AM   #1
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
Question Thread in C++

void Processfile()
{
	char FileName[] = "C:\\abc.txt";

	while( true)
	{
		if (fexists(FileName))
		{
			printf("File Exist\n\n");

			ifstream infile("C:\\abc.txt");
			string sLine = "";
			while( getline(infile, sLine) )
			{
				if( strlen(sLine.c_str()))
				{
					printf( "%s\n", sLine.c_str());
				}
			}

			DeleteFile();
		}
		else
		{
			printf( "Not Exist\n\n" );
		}

	}
}

This function, i need to create it as a thread...

I do this
             DWORD   d_threadID = 0;
	CreateThread(NULL,
		0,
		Processfile,
		NULL,
		0,
		(LPDWORD)&d_threadID);

Error
Quote:
error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (void)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
:o
myName is offline   Reply With Quote
Old Mar 14th, 2006, 2:26 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,224
Rep Power: 5 grumpy is on a distinguished road
The error message is fairly self explanatory. The third argument of CreateThread is a pointer to a function that accepts one argument of type void * and returns an unsigned long. The compiler is complaining because you have attempted to pass it a function that accepts no arguments and returns void.
grumpy is offline   Reply With Quote
Old Mar 14th, 2006, 3:03 AM   #3
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
still can not understand..... please guide...
myName is offline   Reply With Quote
Old Mar 14th, 2006, 3:13 AM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
I guess this is Win32 API?

You need to use a specific prototype/calling convention for your thread procedure.

DWORD WINAPI ThreadProc( LPVOID pVoid )
Cache is offline   Reply With Quote
Old Mar 14th, 2006, 4:02 AM   #5
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
ok.. i add (LPTHREAD_START_ROUTINE) for the 3th parameter...

              DWORD   d_threadID = 0;
	CreateThread(NULL,
		0,
		(LPTHREAD_START_ROUTINE)Processfile,
		NULL,
		0,
		(LPDWORD)&d_threadID);

Now there is no error..
BUTTTT... :o
Why the process file did not run?
What's wrong????
Anybody see what wrong? please inform.. your help will be very much appreciate...

void Processfile()
{
	char FileName[] = "C:\\abc.txt";

	while( true)
	{
		if (fexists(FileName))
		{
			printf("File Exist\n\n");

			ifstream infile("C:\\abc.txt");
			string sLine = "";
			while( getline(infile, sLine) )
			{
				if( strlen(sLine.c_str()))
				{
					printf( "%s\n", sLine.c_str());
				}
			}

			DeleteFile();
		}
		else
		{
			printf( "Not Exist\n\n" );
		}
                Sleep(200);

          }
}
myName is offline   Reply With Quote
Old Mar 14th, 2006, 5:32 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,224
Rep Power: 5 grumpy is on a distinguished road
Sigh. As I said earlier, your function ProcessFile() takes no arguments and returns void. It needs to accept a void pointer and return an unsigned long.
unsigned long ProcessFile(void *)
{
     // the body of your function
}

Using a cast, as in
CreateThread(NULL,
		0,
		(LPTHREAD_START_ROUTINE)Processfile,
		NULL,
		0,
		(LPDWORD)&d_threadID);
tells the compiler not to complain when you pass your version of ProcessFile() as the third argument to CreateThread(). It does not magically allow CreateThread() to work correctly when it (or the thread it creates) attempts to execute your function by passing an argument to it and checking the return value. In fact, using the cast in this instance (passing a function to CreateThread() that is of a different from from what CreateThread() expects) is a good way to ensure your program does not work correctly.

In this case it would also be a good idea to eliminate the (LPDWORD) cast as well from your call. Telling the compiler that &d_threadID is a pointer to a DWORD does not magically turn d_threadID into a DWORD.

Last edited by grumpy; Mar 14th, 2006 at 5:50 AM.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC