Solved

CreateThread problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 recursion in the C programming language.
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.

863 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

20 Experts available now in Live!

Get 1:1 Help Now