Are you in a separate process? If so you will need to do something else to get your DLL loaded in their process. Any address that is returned to you by this function will only be valid in the process that contains the window. Your problem may be related to that fact, but I am not certain.
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life!