小弟最近在做一个加密有关的项目。涉及到密码之类的东西。一旦密码所在的内存处于虚拟内存,而此时机器又突然断电的话,那么密码将会存储在硬盘上。请问如何组织某块内存被系统交换到虚拟内存种?

解决方案 »

  1.   

    以前还没有听说,看见LS所说的,查了一下MSDN,果然很强大……
    摘录了一些,看LZ有用没有。
    VirtualLock
    The VirtualLock function locks the specified region of the process's virtual address space into physical memory, ensuring that subsequent access to the region will not incur a page fault. BOOL VirtualLock(
      LPVOID lpAddress,   // first byte in range
      SIZE_T dwSize       // number of bytes in range
    );
    Parameters
    lpAddress 
    [in] Pointer to the base address of the region of pages to be locked. 
    dwSize 
    [in] Specifies the size, in bytes, of the region to be locked. The region of affected pages includes all pages that contain one or more bytes in the range from the lpAddress parameter to (lpAddress+dwSize). This means that a 2-byte range straddling a page boundary causes both pages to be locked. 
    Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. Res
    All pages in the specified region must be committed. Memory protected with PAGE_NOACCESS cannot be locked. Locking pages into memory may degrade the performance of the system by reducing the available RAM and forcing the system to swap out other critical pages to the paging file. Each version of Windows has a limit on the maximum number of pages a process can lock. This limit is intentionally small to avoid severe performance degradation. Applications that need to lock larger numbers of pages must first call the SetProcessWorkingSetSize function to increase their minimum and maximum working set sizes. The maximum number of pages that a process can lock is equal to the number of pages in its minimum working set minus a small overhead.Pages that a process has locked remain in physical memory until the process unlocks them or terminates.To unlock a region of locked pages, use the VirtualUnlock function. Locked pages are automatically unlocked when the process terminates. This function is not like the GlobalLock or LocalLock function in that it does not increment a lock count and translate a handle into a pointer. There is no lock count for virtual pages, so multiple calls to the VirtualUnlock function are never required to unlock a region of pages. Windows 95/98/Me: The VirtualLock function is implemented as a stub that has no effect and always returns a nonzero value.