After calling which function are you calling GetLastError()? Note that GetLastError() only returns a valid error code under the conditions documented by the proceeding function call. Just calling GetLastError() after any function call and without checking first whether the function succeeded or failed will not give a reliable value.