Solved

CreateThread problem

Posted on 2011-02-28
3
490 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 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

856 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