In this work we study several c and b states dynamically generated from the meson-baryon interaction in coupled channels, using an extension of the local hidden gauge approach in the Bethe-Salpeter equation. These molecular states appear as poles of the scattering amplitudes, and several of them can be identified with the experimentally observed c states, including the c (2790), c (2930), c (2970), c (3055) and c (3080). Also, for the recently reported b (6227) state, we find two poles with masses and widths remarkably close to the experimental data, for both the J P = 1/2 − and J P = 3/2 − sectors.