Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CreateThread problem

Posted on 2011-02-28
3
Medium Priority
?
497 Views
Last Modified: 2012-05-11
Hello,
I'm having issues using CreateThread:

this is the code:

DWORD WINAPI DualP(LPVOID vImg)
{
	puts("inside");
       puts((const char*)vImg);

Open in new window

void main2()
{
	const char* lpKey = (const char*)"mykey123";
	FILE * iFile;
	long lSize;
	char* result;
	char * buffer;
	iFile = fopen("a.txt", "rb");
    fseek(iFile, 0, SEEK_END);
	lSize = ftell(iFile);
	rewind(iFile);
	buffer = (char*) malloc (sizeof(char)*lSize);
	fread (buffer,1,lSize,iFile);
	result = EnDeCrypt((const char*)buffer, lSize, lpKey);

	DWORD threadId = 0;

HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)DualP, (LPVOID)result, 0, &threadId);
if(hThread == NULL)
puts("CreateThread Failed..");
else
puts("CreateThread Succeeded");

}

Open in new window




as you can see I'm trying to decrypt some data and send the result of that decrypted data into the function DualP, but it's not even accessing DualP. The thread is created successfully, but I never see "inside" or the contents of vImg being displayed, which is what should happen when I'm inside DualP.

Again, the thread is being created, but it's not entering it.

Anyone see where I'm going wrong?

Thanks!
0
Comment
Question by:JoeD77
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 35002651
There's a good chance that 'result' has already gone out of scope until the thread is executing - does the following help?
HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)DualP, (LPVOID)result, 0, &threadId);
if(hThread == NULL)
puts("CreateThread Failed..");
else
puts("CreateThread Succeeded");

WaitForSingleObject(threadId,INFINITE);

Open in new window

0
 
LVL 2

Expert Comment

by:syahmixp
ID: 35002768
Code you posted looked just fine.
As jkr said, 'result' might already been destroyed in EnDeCrypt before DualP access it.

if you add this code after EnDeCrypt, can you see the contents of 'result' as expected?
result = EnDeCrypt((const char*)buffer, lSize, lpKey);
puts((char*)result);

Open in new window

0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 35003707
"HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)DualP, (LPVOID)result, 0, &threadId);"

You don't have to cast result to a void*. Just pass it as it is.

HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)DualP, result, 0, &threadId);

Take a look at
http://msdn.microsoft.com/en-us/library/ms682516%28v=VS.85%29.aspx

Alternately you could also try not passing in anything initially. First see whether you can start the thread execution
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question