I've been trying to remember the name of this story and author for days.
The story is Walter Mittyish type about a man who lives a mundane Earthbound office job life during the day, but at night when he's asleep he seamlessly switches into a world of Medieval swords and horses where he's a dashing prince. When the prince falls asleep, the life switches back to the office worker's life. (This has been happening since both of the characters' childhoods, for as long as they can remember.) In the story, the prince's kingdom is threatened, and the office worker attempts to help by memorizing the formula for making gunpowder. The prince doesn't think that his "other" life is real, but he tries the recipe, makes the gunpowder, and the gunpowder helps the prince to win the decisive battle. However, the prince is mortally wounded and dies, and so does the office worker, but the last image is that the face of the office worker has the dashing adventurous grin of the prince on his face.
My father gave me a collection of short stories by this author that included this one. My recollection is that he was a pre-Golden Age author, and might have been foreign (i.e., not a U.S. author). He also relied on dreams in many of his stories.