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
'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.