The RtlSetVolatileMemory function provides RtlFillMemory behavior (for example, setting the contents of a buffer) in situations where the developer needs to be sure that the setting operation occurs (for example, isn't subject to compiler optimizations). 函式會傳回已填滿記憶體的指標。
Syntax
volatile void * RtlSetVolatileMemory(
[out] volatile void *Destination,
[in] int Fill,
[in] size_t Length
);
Parameters
[out] Destination
要填滿之內存區塊起始位址的指標。
[in] Fill
要填滿記憶體區塊的位元組值。
[in] Length
要填入的記憶體區塊大小,以位元組為單位。 This value must be less than the size of the Destination buffer.
Return value
Returns a pointer to the filled memory block (Destination).
Remarks
函式無法辨識為編譯程式內部函數,因此編譯程式永遠不會優化呼叫(完全或以對等的指令序列取代呼叫)。 This differs from RtlFillMemory which is subject to various compiler optimizations.
當呼叫傳回時,已以所需的值覆寫緩衝區。 This function's memory accesses to the Destination will only be performed within the function (for example, the compiler can't move memory accesses out of this function).
如果平台允許,函式可能會執行未對齊的記憶體存取。
函式可能會在其作業中多次存取記憶體位置。
此函式適用於所有版本的 Windows,而不只是最新版本。 您必須取用最新的 WDK,才能從 wdm.h 標頭取得函式宣告。 您也需要最新的 WDK 連結庫 (volatileaccessk.lib)。 不過,產生的驅動程式將會在舊版 Windows 上正常執行。
Example
UCHAR SensitiveData[100];
// Imagine we temporarily store some sensitive cryptographic
// material in a buffer.
StoreCryptographicKey(&SensitiveData);
DoCryptographicOperation(&SensitiveData);
// Now that we are done using the sensitive data we want to
// erase it from the stack. We can use RtlSetVolatileMemory
// to fill the buffer and get a pointer to the filled memory.
// This call will not be optimized away by the compiler.
volatile VOID* clearedBuffer = RtlSetVolatileMemory(&SensitiveData, 0, sizeof(SensitiveData));
Requirements
| Requirement | Value |
|---|---|
| 最低支援的用戶端 | See Remarks |
| Header | wdm.h (包括 Wdm.h) |
| Library | volatileaccessk.lib (核心模式),volatileaccessu.lib (使用者模式) |