1. He bought him a cooking book.

2. He bought him a cookbook.

(Which one is correct, 'cooking book' or 'cookbook'?)

3. Who wrote the song to her?
4. Who wrote the song for her?
(Do we have to use 'to' or 'for'? Are both OK?)

3 answers

I take it that "he" and "him" are two different people? Yes or no?

#4 is correct, but#3 is not.
1. I bought him a cooking book.
2. I bought him a cookbook.

(Thank you. I need to change the subject. Are both expressions OK, 'cookbook' and 'cooking book'?)
No, only "cookbook" is used in this way.

If he is buying the cookbook for himself, then you'd say, "He bought himself a cookbook."

If he is buying the cookbook for someone else, then you'd say, "He bought his brother a cookbook."
(Substitute whatever you need for "his brother.")