Click to See Complete Forum and Search --> : using gloablAlloc and accessing variables in another thread.


flynny1st
May 29th, 2007, 11:25 AM
Hi,

Hopefully an easy one here. I want to some variables form a file and then hook into a progrma, in this example calculator. The hook will then access these variables.

I read in the file and create the variables using the following


LPTSTR token = strtok( line, seps );

HGLOBAL memPtr = GlobalAlloc(GMEM_FIXED, strlen(token)+1); //pointer to mem block

if( memPtr && (hookList[i][0] = (LPTSTR)GlobalLock(memPtr)) != NULL )
hookList[i][0] = (LPTSTR) RtlMoveMemory(hookList[i][0], token, strlen(token)+1);
else
{
sprintf(buf, "Error: Could not allocate Memory");
MessageBox(NULL, buf, NULL, 0);
}



where line is the line from the text file and seps the seperation. Now it seems to be reading the variables in ok using this and can print them out.

However when i hook into calculator and try and access the array it returns null. is it possible to do what i am trying to do? and if so how?

Many thanks,

Matt.

Arjay
May 29th, 2007, 11:53 PM
What do you mean, "hook into calculator?"

flynny1st
May 30th, 2007, 04:30 AM
Sorry,

what i mean i i want to create a hook using the win32 api, to hook into the events in an application, for example calculator.

However the variables i have read in fromthe file prior to installing this hook are in a seperate thread? and so when i try and access them inside the callback method of the hook, it simply returns null.

As in the above code i have tried to use globalAlloc to solve this. however it still returns null inside this callback method.

Is what i am trying to do possible? and if so am i executing it right? and if not what am i doing wrong?

Many thanks,

Matt.

Arjay
May 30th, 2007, 10:51 AM
GlobalAlloc only works for memory within a process, so it isn't suitable for systems that span multiple processes.

To pass around data across multiple process (such as when you hook into calculator), you will need to use some form of Interprocess Communication (http://en.wikipedia.org/wiki/Inter-process_communication).

flynny1st
May 30th, 2007, 11:53 AM
Thats great thanks for the reply.

I'll probably just read the file in through the callback process, as its looks like its going to be more trouble than its worth.

Thanks again,

Matt.