Solved

CreateThread problem

Posted on 2011-02-28
3
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 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