What is the issue, exactly? JPEG is lossy, certainly. What was the original compression on the images? Why don't you just compress the images as you write them rather than later using the optimizer?
I'm afraid I don't understand your code. Why would a plug-in send a WM_CLOSE message to Acrobat?