Texture coordinates export problem

I need to export Blender scenes into my application. And I’ve encountered a problem with texture coordinates. I’ve found the following method

m = bmesh.new()
m.from_mesh(mesh)
...
uv = m.loops.layers.uv.active
uv1Node = doc.createElement("UV1")
loop = m.faces[i].loops[0]
uv1Node.setAttribute("X", str(loop[uv].uv.x))
uv1Node.setAttribute("Y", str(loop[uv].uv.y))
polygonNode.appendChild(uv1Node)

But the coordinates match the coordinates that are used by Blender only if I set coordinates to UV and projection to flat. Any other mode, e. g. generated coordinates or sphere projection makes my render result doesn’t match render result in Blender. E. g. if I set coordinates to generated I get the following difference:
http://img805.imageshack.us/img805/650/blenderscreenshot2.png
Tell me please how to get the correct texture coordinates that are used by Blender.