mIRC Home    About    Download    Register    News    Help

Print Thread
#155299 05/08/06 01:42 AM
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
For e.g, take this dll function

Code:
	int WINAPI Test_Function(HWND, HWND, char *data, char *parms, BOOL, BOOL) 
	{
		
		CreateThread(NULL, 0, Test_Thread, data, 0, 0);

		return 1;
	}


and here's the thread:

Code:
DWORD WINAPI Test_Thread(LPVOID param)
{
	char r[1024];
	strcpy(r, "/echo -a ");

	char *data = (char*) param;

	strcat(r, data);

	sendmirc(mapHWND, r);

	return 1;
}


now if I do something like:

$dllcall(test.dll, 0, Test_Function, hello!) - this works fine. it echos 'hello!' as expected.

BUT, if i call the function multiple times, it gives mIRC errors, E.g.

Code:
alias test2 {
  var %x 1
  while (%x <= 5) {

    $Dllcall(test.dll, 0, Test_Function, %x)
    inc %x
  }
}


The dll should make mIRC echo 1,2,3,4,5 (5 times).., but instead i keep get this:


edit* and now i get same problem but it says stack error:


Last edited by pouncer; 05/08/06 01:56 AM.
#155300 05/08/06 07:39 PM
Joined: Dec 2002
Posts: 580
N
Fjord artisan
Offline
Fjord artisan
N
Joined: Dec 2002
Posts: 580
Does this crash not happen if you use the mLoad function to keep the DLL loaded? Sounds like $dllcall is unloading the DLL as soon as it returns, yet the thread is still running (in unloaded memory).


NaquadaBomb
www.mirc-dll.com
#155301 06/08/06 03:04 AM
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
it crashes either way, whether the dll is loaded or not!

#155302 08/08/06 11:47 AM
Joined: Dec 2002
Posts: 5,420
Hoopy frood
Offline
Hoopy frood
Joined: Dec 2002
Posts: 5,420
The data passed by mIRC to Test_Function is no longer valid once the the Test_Function exits. You must copy the data to a local variable and pass that on to your thread function.

#155303 08/08/06 12:31 PM
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
ah I see now. Thanks.

For anyone interested, here's a fix:

Code:

	int WINAPI Test_Function(HWND, HWND, char *data, char *parms, BOOL, BOOL) 
	{
		char *temp = malloc(strlen(data) + 1);
		strcpy(temp, data);
		CreateThread(NULL, 0, Test_Thread, temp, 0, 0);

		return 1;
	}


Code:
DWORD WINAPI Test_Thread(LPVOID param)
{
	char r[1024];
	strcpy(r, "/echo -a ");

	char *data = (char*) param;

	strcat(r, data);

	free(data);

	sendmirc(mapHWND, r);

	return 1;
}

#155304 14/08/06 05:11 PM
Joined: Dec 2002
Posts: 580
N
Fjord artisan
Offline
Fjord artisan
N
Joined: Dec 2002
Posts: 580
Good eye. smile


NaquadaBomb
www.mirc-dll.com

Link Copied to Clipboard