Zaman Shah Durrani, the ruler of Afghanistan, invaded northwestern India multiple times. During his campaign in 1799, he captured Lahore. As he retreated, he granted Lahore to Ranjit Singh, recognizing his growing influence and control over the region.