I was wondering what's the best way to retain an allocated memory even if a particular program already terminated?. Actually, I'm creating an ASM CGI - Binary Trees(asked by Andy981) trying to port from the original win32 app. I have no idea on how to carry over the memory allocated on the current process to the next CGI call. It's like this:

1. Html file calls/passes parameters to test.exe
2. test.exe receives the parameters, creates binary tree, test.exe exits. Calls/passes parameters to test.exe again (Don't know how to retain the memory allocated on the previous process to be availabe on the current called process)
3. test.exe receives the parameters, add data to binary tree, test.exe exits. Calls/passes parameters (Don't know how to retain the memory allocated on the previous process to be availabe on the current called process)
4. test.exe receives the parameters, add data to binary tree, test.exe exits. Calls/passes parameters again (Don't know how to retain the memory allocated on the previous process to be availabe on the current called process)

...

n. When the exit button is pressed, deallocate all binary tree nodes).

There are some dirty tricks I can do like, put all data into a file before the CGI routines exits, then read the contents from the file on the next CGI call and recreate the binary tree... Use the registry ...

But I don't want these methods because this is slow and it's "cheating".

I'm thinking of creating another process(invisible window), find the window on the next cgi call ... and continue from there. The problem with this one is the hidden window resides on the server and what if there more than 1 users executing the current cgi app then this will not work properly. I can get the ip address dynamically assemble the source file ... but this is such a daunting task and I don't like to complicate the code for such a simple binary trees.

Can anyone recommend a better solution? I don't have better ideas on how to continue after step 2...

Thanks!!!
Posted on 2002-06-28 18:42:57 by stryker
I guess you can use either the file dumping method, or can try file mapping (not sure if that will work). API documentation says:
To fully close a file mapping object, an application must unmap all mapped views of the file mapping object by calling UnmapViewOfFile, and close the file mapping object handle by calling CloseHandle. The order in which these functions are called does not matter. The call to UnmapViewOfFile is necessary because mapped views of a file mapping object maintain internal open handles to the object, and a file mapping object will not close until all open handles to it are closed.
Posted on 2002-06-28 22:25:02 by comrade
You could do what alot of Windows API calls do if you run out of other ideas. Have the HTML file allocate a hunk of memory and pass the handle off to test.exe. This way you never really lose it. Test.exe can return a 1 for status ok and 0 or -1 (or whatever) if more memory is needed...

Just an idea to add to the others. This one has the advantage of simplicity and no overhead as well. :cool:
Posted on 2002-06-28 23:39:39 by Graebel
I take it that test.exe is spawned by the CGI, and that test.exe
doesn't "respawn" itself, but rather that the multiple invocations
of test.exe are done by multiple CGI requests? Or something like
that anyway :).

I'd probably design a "memory server" (sucky name, can't think of
anything better right now) application that will always be running.
The memory server (MS) will responsible for handling the structures -
the CGI app should only do parsing and html output, and use calls to
the MS to add/get data. The interface between the CGI and the MS can
be handled in a lot of ways; on NT I'd probably use named pipes, but
there are other methods like window posting and sockets. Even memory
mapped files, though you will have to be a bit careful to handle
synchronization (I prefer sockets or pipes, sync is easier there).

Memory mapped files by itself will not work, as the handles are closed
on program termination.
Posted on 2002-06-29 06:40:05 by f0dder
In Win9x you can create a shared heap (HeapCreate flag 4000000h). This prevents Heap from being destroyed when application finishes. But I'm afraid this doesn't work in Win2k/XP.
Posted on 2002-06-29 07:13:42 by japheth
I take it that test.exe is spawned by the CGI, and that test.exe doesn't "respawn" itself, but rather that the multiple invocations of test.exe are done by multiple CGI requests? Or something like that anyway
Yes that's right ...
I'd probably design a "memory server" (sucky name, can't think of
anything better right now) application that will always be running.
The memory server (MS) will responsible for handling the structures...
That would be an alternative.
In Win9x you can create a shared heap (HeapCreate flag 4000000h). This prevents Heap from being destroyed when application finishes. But I'm afraid this doesn't work in Win2k/XP.
How I wish I could do this on 2k ...


I think I'll do the files(keep recreating binary tree) or the hidden window(WriteProcessMemory/ReadProcessMemory), I'll create two versions and do some testings and decide from there.

Thanks for the input. :)
Posted on 2002-06-29 08:43:50 by stryker
Read/WriteProcessMemory? Eeek, better use memory mapped files
then :). And really, you ought to give the piped approach a try :).
Posted on 2002-06-29 08:51:44 by f0dder
Oh Ok I'll try everything what you guys suggested ... :alright:
Posted on 2002-06-29 08:55:12 by stryker
After really, really deep serious considerations and deep thinking, I've decided to just go with the files as of the moment and I'll probably implement a "Memory Server" in the future, I think this "Memory Server" is much cleaner, elegant and no "cheap tricks of data manipulation". I'll be back with the results. ;)
Posted on 2002-06-29 22:15:45 by stryker