winzip icon

Non-Volatile RAM (using disk files)

Submitted on: 1/7/2015 3:52:00 AM
By: James Vincent Carnicelli (from psc cd)  
Level: Advanced
User Rating: By 5 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 1117
     Conceptually, non-volatile RAM is any RAM (random access memory) that is not lost because the memory device loses (external) power. The NvRam class implements this in a primitive but powerful way: by writing to and reading from a data file in real time. In principle, your code need only identify the location in this "memory" (as in an array) that a given variable is stored in and the class does the rest.

Here's a simple example of how it works:

    Dim MyVar As Long
    Set Nvr = New NvRam
    Nvr.Connect "C:\MyData.dat"

    'Read in value,
    'do something with it,
    'and write it back out.

    Nvr.ReadVar MyVar, 20
    MyVar = MyVar + 1
    Nvr.WriteVar MyVar, 20


Here are some good applications for this class:

- Protecting in-"memory" data from power failures.
- Maintaining large amounts of data with almost no memory overhead.
- Maintaining program state across run-time sessions.
- Sharing data among multiple processes.

While it does not yet have built-in support for storing user-defined data types or objects, all primitive data types (e.g., Long and String) are supported. (Variants are allowed, so long as they are late-bound to one of the primitive data types.) Also missing is a way to shrink the size of the data file, the equivalent of ReDim Preserve. Feel free to make suggestions or add support for such features, so long as credit is given for the original code.

While tested with VB6/NT4, this should be compatible with any version of Windows that supports the CopyMemory() API call and which has data types with the same sizes as with Win32 systems like NT. Your comments and vote are welcome.

winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net

Other 20 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.