儲存 64 位值

若要儲存 64 位指標值,請使用 ULONG_PTR。 使用 32 位編譯器進行編譯時 ,ULONG_PTR 值為 32 位,而使用 64 位編譯器編譯時則為 64 位。

下列範例使用已移植到 64 位 Windows 的實際程式碼。 包含讓程式碼 64 位相容之步驟的敏感度。

範例 1:取得位址

下列程式碼說明取得位址的可攜式方式。

方法 結果
使用 ULONG (僅限 32 位的方法)
ULONG getAnAddress( )Int *somePointerReturn( (ULONG) somePointer );
使用可攜式方法ULONG_PTR ()
ULONG_PTR getAnAddress( )Int *somePointerReturn( (ULONG_PTR) somePointer );

 

範例 2:計算位址

下列程式碼說明計算位址的可攜式方式。

方法 結果
使用 ULONG (僅限 32 位的方法)
Int *somePointer;Int *someOtherPointer;somePointer = (int *)( (ULONG)someOtherPointer + 0x20 );
使用可攜式方法ULONG_PTR ()
Int *somePointer;Int *someOtherPointer;somePointer = (int *)( (ULONG_PTR)someOtherPointer + 0x20 );