共用方式為


使用 RtmUpdateAndUnlockRoute 就地更新路由

下列程式概述用來就地更新路由的步驟。 下列範例程式碼示範如何實作程式。

若要就地更新路由,用戶端應該採取下列步驟

  1. 呼叫 RtmLockRoute來鎖定路由。 目前,此函式實際上會鎖定路由的目的地。 路由表管理員會傳回路由的指標。

  2. 使用路由表管理員 RTM_ROUTE_INFO 結構的指標,在步驟 1 中取得,對路由進行必要的變更。 在就地更新時,只能修改 RTM_ROUTE_INFO 結構的特定成員。 這些成員包括: TimePrefInfoEntitySpecificInfoBelongsToViewsNextHopsList

    注意

    如果用戶端將資訊新增至 「檔案 」或 「NextHopsList 」成員,用戶端必須呼叫 RtmReferenceHandles ,以明確遞增路由表管理員保留在下一個躍點物件上的參考計數。 同樣地,如果用戶端從 NextHopsList 成員移除資訊,用戶端必須呼叫 RtmReleaseNextHops 來遞減參考計數。

     

  3. 呼叫 RtmUpdateAndUnlockRoute 以通知路由表管理員已進行變更。 路由表管理員會認可變更、更新目的地以反映新資訊,然後解除鎖定路由。

下列範例程式碼示範如何使用路由表中實際路由資訊的指標,直接更新路由。

Status = RtmLockRoute(RtmRegHandle,
                      RouteHandle,
                      TRUE,
                      TRUE,
                      &RoutePointer);

if (Status == NO_ERROR)
{
        // Update route parameters in place (i.e., directly on 
// the routing table manager's copy)
    
    // Update the metric and views of the route
    RoutePointer->PrefInfo.Metric = 16;

    // Change the views so that the route belongs to only the multicast view
    RoutePointer->BelongsToViews = RTM_VIEW_MASK_MCAST;

    // Set the entity-specific information to X
    RoutePointer->EntitySpecificInfo = X;

    // Note that the following manipulation of
    // next-hop references is not needed when
    // using RtmAddRouteToDest, as it is done
    // by the routing table manager automatically
    
    // Change next hop from NextHop1 to NextHop2
    NextHop1 = RoutePointer->NextHopsList.NextHop[0];

    // Explicitly dereference the old next hop
    RtmReleaseNextHops(RtmRegHandle, 1, &NextHop1);

    RoutePointer->NextHopsList.NextHop[0] = NextHop2;

    // Explicitly reference next hop being added
    RtmReferenceHandles(RtmRegHandle, 1, &NextHop2);

    // Call the routing table manager to indicate that route information
    // has changed, and that its position might
    // have to be rearranged and the corresponding destination
    // needs to be updated to reflect this change.
    
    Status = RtmUpdateAndUnlockRoute(RtmRegHandle,
                                     RouteHandle,
                                     INFINITE, // Keep forever
                                     NULL,
                                     0,
                                     NULL,
                                     &ChangeFlags);
    ASSERT(Status == NO_ERROR);
}