8 Replies - 445 Views - Last Post: 13 March 2013 - 05:57 AM Rate Topic: -----

#1 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

How to find out if a file is loaded in memory?

Posted 12 March 2013 - 02:03 AM

Hello!

Let's say I load a "data.bin" file in dinamic memory using a program.

Now, can I by means of C++ code, find out using another program, if a file called "data.bin" is loaded in memory?

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: How to find out if a file is loaded in memory?

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: How to find out if a file is loaded in memory?

Posted 12 March 2013 - 04:11 AM

It is possible to read a given processes memory using functions in the Windows API. I guess you could search through the processes memory for the contents of the file. It's also possible to get information about memory allocated by a process using VirtualQueryEx, which might make the task a bit easier.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3459
  • View blog
  • Posts: 10,666
  • Joined: 05-May 12

Re: How to find out if a file is loaded in memory?

Posted 12 March 2013 - 05:22 AM

Are you looking for a memory content match? As noted above, you can inspect other programs memory, as long as you are running with debug privileges.

Or a file access notification? Again, you'll need debug privileges. Get your hands on Richter's Inside Windows NT, and he demonstrates the techniques that go into watching all the file handles and which process requested which file handle which is incorporated in Process Monitor.
Was This Post Helpful? 1
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: How to find out if a file is loaded in memory?

Posted 12 March 2013 - 05:35 AM

Damn. Just get hold of any hex editor, load the file into it. Next debug your code and see if the bytes in the buffer match those in the hex editor during your debug session.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3459
  • View blog
  • Posts: 10,666
  • Joined: 05-May 12

Re: How to find out if a file is loaded in memory?

Posted 12 March 2013 - 05:50 AM

If it's a 20MB "data.bin" file, but a program only loads 10 bytes at a time, finding a match is going to be a matter playing the probabilities of finding a unique enough 10 bytes and praying that the 10 bytes you've selected are not split up with x bytes being read in one iteration, and 10-x bytes being read in the other iteration.

Good suggestion though if it's a small enough file, and you know that the program you are looking for loads the entire file, or a "significant" portion of the file.
Was This Post Helpful? 1
  • +
  • -

#6 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to find out if a file is loaded in memory?

Posted 12 March 2013 - 06:07 AM

Thank you all guys.
It seems that it's much more difficult than I expected.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: How to find out if a file is loaded in memory?

Posted 12 March 2013 - 06:19 AM

What is the endgame here? We may be able to provide an alternative means to accomplish your task.
Was This Post Helpful? 0
  • +
  • -

#8 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to find out if a file is loaded in memory?

Posted 13 March 2013 - 12:54 AM

The endgame is to implement a MSDI application, i.e. a Multithreaded Single Document Interface. I don't need just MDI because I don't want to modify the design of my application. So, the problem is that I find it to hard to do (MSDI). I found a code on the internet here:http://www.codeguru.com/cpp/w-d/doc_view/viewmanagement/article.php/c6129/Creating-a-Multithreaded-SDI-Application.htm but I just cannot make it to work properly in my app because my app is way to complex than the example. I think I will try harder to understand where I'm going wrong in my code.

This post has been edited by livium: 13 March 2013 - 12:56 AM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3459
  • View blog
  • Posts: 10,666
  • Joined: 05-May 12

Re: How to find out if a file is loaded in memory?

Posted 13 March 2013 - 05:57 AM

It's actually quite simple. In psuedo code:

main(int argc, char * argv[])
{
    bool isFirst = false;

    Channel channel = GetGlobalCommunicationsChannel();
    if (channel == null)
    {
        channel = CreateGlobalCommunicationsChannel();
        isFirst = true;
    }

    if (channel.AlreadyHasWindow(argv[1]))
    {
        channel.SetFocus(argv[1]);
        return;
    }

    channel.CreateWindow(argv[1]);

    if (isFirst)
    {
        while (!quit)
        {
            // keep listening for messages on the channel
            // keep current windows alive
        }
    }
}



The "channel" above can be a simple shared memory. It can be a named pipe. It can be a combination of Windows broadcast messages and semaphores.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1