All depends on the country. Some it is not just a unlock code if you want to switch vendors some phone models even with the same name are different on the inside.
Generally if you have fully paid for the phone and/or have completed whatever contract the vendor has with you they will give you a unlock code if you ask them. In many countries it is required by law they give it to you after the contract is fulfilled. Now until you meet the conditions of the contract to unlock the phones they have every right not to give it to you. Many times they give you the phone for free and expect to pay it back though usage.