Eugenio Velez, Juan Uribe and Pablo Sandoval each had two hits and drove in a run for the Giants, who had lost six of their last eight. San Francisco remained five games behind NL wild card-leading Colorado with six games left.
Cain (14-7) gave up three hits, struck out eight and walked two to set a career high for wins.
He also laid down a nice bunt that caused the crowd to giving a standing ovation.
Randy Wells (11-10) allowed five runs and 10 hits over 5 2-3 innings for Chicago. Bobby Scales had an RBI triple.
Cain has not allowed a run against the Cubs over the last 23 innings.