With the H1 detector at the ep collider HERA, D* meson production cross sections have been measured in deep inelastic scattering with four-momentum transfers Q^2>2 GeV2 and in photoproduction at energies around W(gamma p)~ 88 GeV and 194 GeV. Next-to-Leading Order QCD calculations are found to describe the differential cross sections within theoretical and experimental uncertainties. Using these calculations, the NLO gluon momentum distribution in the proton, x_g g(x_g), has been extracted in the momentum fraction range 7.5x10^{-4}< x_g