Pointer to file -> Pointer from file

hi,i have 2 application, one has some variables. this application writes the addresses of the variables into "C:\\bin\\Settings.bin" file. after it is finished with writing (but still running) the other program is reading the addresses from the file. but the reader application doesnt gets pointers correctly. here is the codes.
To file:
1
2
3
4
5
int File, *mypointer, variable;
mypointer=&variable
File=open("C:\\bin\\Settings.bin",O_WRONLY | O_TRUNC | O_BINARY);
write(File,&mypointer,4);
close(File);


From file:
1
2
3
4
int File, *mypointer;
File=open("C:\\bin\\Settings.bin",O_RDONLY | O_BINARY);
read(File,&mypointer,4);
close(File);


interestingly, if i use these 2 codes in the same project, i get correct results. for instance:
1
2
3
4
5
6
7
8
9
int File, *mypointer, *secondpointer, variable;
mypointer=&variable
File=open("C:\\bin\\Settings.bin",O_WRONLY | O_TRUNC | O_BINARY);
write(File,&mypointer,4);
close(File);

File=open("C:\\bin\\Settings.bin",O_RDONLY | O_BINARY);
read(File,&secondpointer,4);
close(File);

here, mypointer and secondpointer holds same address. how can i make my reader application read correctly.
Last edited on
Do you know what a file lock is? Usually if two programs try to access the same file at the same time Windows says "NO" and which ever one got there first has right of way. So this isn't going to work the way you think unless you don't mind being sloppy and having the "Reader" program loop attempts to open the file. This is also why open returns a value if it worked *hint hint*.
Hi,

Each process has its own address space, which means that the pointers in 'to file'
are only valid within its own address space.
When 'from file' reads the pointers they dont point to the same memory.

The easiest solution would be to write the variables to file.

Hope this was helpful
Shredded

@ shredded: What? No it's not. The "easiest solution" is to have the "Writer" program call the "Reader" program and pass the variables in as arguments, why write to the disk at all? It's comparitivley slow and so isn't reliable if timing is a factor.

EDIT: Also the OP never mentioned what the variables addresses were. What if he meant they were memory addresses to drivers loaded into Memeory for example? Those probably won't change within the life span of the program. And contrary to popular belief they ARE accessable from User Memory space, check the Windows SDK to see how.
Last edited on
i have made some search, may be i am wrong but i guess using mutex will solve my problem. is that an easy way? i red many web pages but didn't understand what actually mutex is and how to use it. if it is a suitable solution, how do i use it? if its not, how can i do that:
"Writer" program call the "Reader" program and pass the variables in as arguments"


by the way, i have used CreateProcess to run writer program using the reader. does that mean "reader" calls "writer"?
In that case yes, if you used CreateProcess to run the writer from the reader then that is what happend. The way you wrote it above makes it seem like you want the reader to execute AFTER the writer has finished, the cleanest way to do this is to allow the writer decide when it is done with the file and then call the Reader.

If you want the Reader to execute, then use CreateProcess to call the Writer, then have the Reader continue what it was doing after the Writer is done, then you want to pass the hThread handle in the PROCESS_INFORMATION structure populated by CreateProcess(...) function to the WaitForInputIdle(...) function so that the two applications don't get in eachothers way. If you're unfamiliar with any of these please read the following links and if you are still having trouble we may be able to help.

- CreateProcess(): http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx

Populates

- PROCESS_INFORMATION Structure: http://msdn.microsoft.com/en-us/library/ms684873(VS.85).aspx

From it's 10th argument labled LPPROCESS_INFORMATION lpProcessInformation on the link above. Pass hThread from this Structure as the first argument to

- WaitForInputIdle(...): http://msdn.microsoft.com/en-us/library/ms687022(VS.85).aspx

So that the two programs do not step on eachother.
Last edited on
i made some changes. now the first application is writing the addresses into the file. then calling the reader with CreateProcess(). reader first reads the addresses from the file, holds them in pointers. where should i put WaitforinputIdle()? when second app is ready it is trying to change the value of an address like *pointer=true.
1
2
3
4
5
6
7
8
9
10
FIRST APPLICATION
.....
//// write to file////
open(.....
write(.....
///call reader////
CreateProcess().....                                                                                 SECOND APPLICATION
//////                                                                                                 open(........)
what is now??????                                                                                    read(........)
                                       /////here second app wants to reach memory        *Pointer=true; 


Topic archived. No new replies allowed.