Solved

CreateThread problem

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now