Obtaining the horizontal angle of a gunbarrel is not difficult. The gun points where the unit looks to. Just get the direction of the unit with getDir and if that doesn't work out, try to get the direction of the unit's gunner. I've done this so I know it's possible.
Or do you need to measure the gun elevation? If that's what you want, you did not express yourself precisely. In this case it's hard to measure the elevation but you can manipulate it. Use camCreate to create an object to aim at (a PipeBomb will do well for that purpose), make sure the unit sees the object with reveal and then let the unit aim at the object. If you put up the object high in the air the gun will elevate to that object. The altitude of the object is easily calculated: altitude = distance * sin elevationAngle. However this is an approximate elevation angle, since the gun will elevate to hit the target which means that the shell has to take a ballistic trajectory. Make sure your gun fires projectiles of high velocity. The higher the velocity, the closer the elevation angle will be to the target angle.
If you know the target the gun aims at (and the gun fires projectiles of very high velocity and little air friction), you can calculate the approximate gun elevation like this:
elevationAngle = arcsin ((heightOfTarget - heightOfFiringUnit) / distance)
If your projectiles take a ballistic trajectory due to their low velocity, you need to calculate the gun elevation angle from the distance of the target, the difference in altitude of target and firing unit, the muzzle velocity of the projectile and the air friction of the projectile. I don't have the formula in my head and most likely this will require a lot of trial and error. The muzzle velocity and the air friction of the projectile can be obtained from the config.cpp file of the firing unit.
Unfortunately I don't know what you want to do since you didn't express yourself precisely enough. Another interpretation of what you want could be that you want to determine the bearing of a target relative to the compass direction your gun is pointing to. In this case: there is an easy solition: Use the atan2 operator to determine the bearing from the relative position of the object. Here is an extract of my guided missile script where I calculate the compass direction of the target relative to the missile (not using atan2 but atan, which is the more complex solution but works just as well):
_DeltaX = (_TargetPos Select 0) - (_MissilePos Select 0)
_DeltaY = (_TargetPos Select 1) - (_MissilePos Select 1)
? (_DeltaY == 0) && (_DeltaX >= 0) : _TargetAngle = 90
? (_DeltaY == 0) && (_DeltaX < 0) : _TargetAngle = 270
? _DeltaY > 0 : _TargetAngle = ATan (_DeltaX / _DeltaY)
? _DeltaY < 0 : _TargetAngle = ATan (_DeltaX / _DeltaY) + 180
? _TargetAngle < 0 : _TargetAngle = _TargetAngle + 360