This is done by looking at the path of the registered version of the Engine, then looking in the registry to see what path matches the path of the Engine.
It might be easier to just create an instance of the Engine and query its version in an executable. However, this can be a little slow to start the Engine so it might not be ideal.