The explicit question is already answered by David Carlisle. Thus this answer adds a more complete use case. \pdflastxpos and \pdflastypos cannot be used in a direct way.
Previously \pdfsavepos must be called and the positions are availabe at shipout time.
Thus the values \pdflastxpos and \pdflastypos needs to be written to a auxiliary file.
Then in the next LaTeX run the stored values from the auxiliary file can be used.
Package zref-savepos (of project zref) provides an interface for the \pdfsavepos
feature that remembers the values in references: