The main problem with guns in medieval fantasy is one of technological plateaus. A technological plateau is the limit to which any particular technology can be pushed; after that, it simply does not get any better without some kind of fundamental revolution. These technological plateaus depend on fundamental technologies that go into them, and it is those fundamental technologies which determine when the “stasis” occurs, or if it occurs. “Breakthrough” technologies are ones that break through those plateaus and enable further development. I will be giving some examples of plateaus, and what they might mean. Important point is that breakthrough technologies – such as gunpowder – are revolutionary rather than evolutionary, and often discovered by accident. As such, they serve as logical cutoff points for enforcing a stasis. Of course, since they can be circumvented – such as jumping from crossbows to railguns – these are not perfect either, but important point is that they cannot be circumvented without different breakthrough.
For a stone age society, technological plateau is based around usage of stones. They can have spears, knives, bows and arrows; however, that is all. Fortifications will be wood or uncut, only minimally shaped stones. There will be no swords, as they cannot be made with stones; any armour will be based on leather, bone and possibly wood.
For a bronze age society, technological plateau is much higher. They can have bows, arrows, spears, shields and full body armour. However, swords will have to be short, and many inventions will have to take into account limitations of bronze. This is the lowest level at which a society may have cannons, if gunpowder is introduced (in fact, bronze is superior to iron as material for cannon).
For iron age society, technological plateau is one of Roman Empire (for the most part – early medieval Europe was in some ways technologically more advanced). Longer swords are possible, as well as plate armour, but iron still has some fundamental weaknesses compared to steel. Gothic plate armour is thus unlikely; much more likely are variations on mail/chain and scale armour. Possibly, something akin to lorica segmentata will be deployed.
For a technology with developed production of steel, technological plateau is one of Late Renaissance. This means Gothic / Maximilian plate armour, organized armies, pikes, heavy cavalry and combined arms armies. It also means heavy presence of crossbowmen and pikemen, which will limit tactical impact of heavy cavalry. However, since crossbows and other weapons are still limited by muscle power (albeit in different ways) and mechanic workings, there is little possibility of weapons developing enough to require abandonment of plate armour in favour of much thicker half-plate, Since there are no gunpowder weapons, fortifications will remain essentially the same as in earlier eras, except likely bigger and stronger (counterweight trebuchet says hi!).
For a gunpowder society without access to electricity, technological plateau lies in middle-to-late 19th century. If cartridge is not invented, technological plateau is somewhere in the middle of 19th century: breech-loaded cannons firing solid-iron projectiles, ironclads (either wooden-hulled or iron-hulled) with rammin spikes (since cannons will not be able to penetrate armour – see Battle of Vis) and muskets. Fortifications will be those of middle-19th century as well, which means fortresses with thick walls: possibly star forts or similar. Navies take on greater significance: instead of being fundamental to combat, rowers become impediment as they get in the way of MOAR DAKKA! As a result, ships become ships of the line and eventually ironclads; they become much longer-ranged, proper oceanic vessels, and at the same time also gain much greater striking power, and can threaten shore installations on their own instead of being merely way to get armies to proper place. This in turn brings about the colonial era, colonial wars and eventually world wars. Gunpowder weapons mean that somewhat-trained-peasants are actually dangerous, which means that a) armies are no longer necessarily small, professional establishments, and b) a replay of French revolution is very likely. But because armies now also require much more developed logistical support – supplying cannon, gunpowder etc. – and because fortifications are much less useful, it also means that nobles cannot play war any more, and that an army – be a professional one or a mass insurgency – will have to be organized and supported by the state. Logistical support can no longer be localized, meaning an end to feudalism.
If cartridge ammunition is invented, it means essentially World War I technology, except without dirigibles/Zeppelins and aircraft. There will be trenches, rifles, heavy artillery, possibly steam locomotives and railroads etc. Any fortifications that are not trenches will be dug into earth. However, there will be no telegraph or telephone, which will impact politics as well as military tactics and strategy. Soldiers will no longer line up in shiny lines as they will be shredded, and invention of machine gun is almost a given. This in turn means industrial society and mass production. Depending on whether railroad and telegraph have developed, warfare may end up as either advanced form of Napoleonic wars (if they have not developed), or a replay of World War I trench warfare (if they have developed); reason is that railroad and telegraph confer a huge advantage on the defender, thus invariably leading to trench warfare.
For electrical society, technological plateau lays much beyond the modern age, in era of railguns, coilguns, nuclear weapons etc. Nuclear weapons themselves may or may not be developed; this however is unlikely as a society with electronic microscope is almost certain to start playing around with atoms sooner or later.
What this means is, if one aims at creating a technological stasis – or at least a society which will not change for long periods of time – there are certain technologies whose introduction should be avoided. It is possible to justify not introducing a completely new technology, or simply ignore it; it is much harder to avoid developing an already existing technology. It is possible to delay it: gunpowder only became useful in guns after Europeans figured out wet-grinding process to produce corned powder in late 14th century. Before that, it could only be used for rockets. Also, if technology is introduced from the outside, improvements may not happen at all – when Commodore Perry landed in Japan, Japanese were still using the same matchlock muskets that Portugese had introduced them to some 250 years before. Neither did Africans ever learn to make their own muskets.
Further, some technologies automatically carry certain implications. Gunpowder means that there are very few problems which cannot be solved by merely applying more gunpowder to problem in question. It also introduces scientific principles (chemistry), which makes it harder – though not impossible – to avoid further advances. In particular, it invalidates the “four elements” conception of universe as a sound scientific principle – which suggests that, if four elements actually is sound principle in the setting, gunpowder may never develop – unless it happens to be a combination of Earth and Fire, which is unlikely (or else clay pots become an impossible technology). In pre-industrial societies, steam engines, mechanical clocks, cranes and possibly electrical batteries were all known – but none were applied to their full extent.
It is however possible to use substitutes. Instead of gunpowder, one can use Greek Fire. The only difference is that it does not explode, but that difference by itself is significant. Instead of guns and cannons, there will be various types of flamethrowers and flammable projectiles. Clay pot with liquid fire is about 1300 years old technology which even today nobody is sure how to exactly reproduce. But due to its nature, Greek Fire has much less potential for future development, making it inherently more digestible by a typical fantasy setting.
Also, “technologies” can exist that will lead to similar effects to already noted. If magic is a thing in the setting, and mages have power comparable to gunpowder artillery, then either one of two things will happen: either their power will be nullified magically, or it will be adjusted to tactically. Magical nullification can take form of various magical shields and protections which nullify mages’ long range attacks. But this may well ask into question existence of mages in the first place, as they would have no impact – unless they also have noncombat roles (e.g. scouting). If such nullification is not present, adjustment will be tactical. In this case adjustment will be similar to historical adjustment to gunpowder weapons. Depending on the power of combat mages, it may range from “almost no adjustment at all” – in which case massed formations will continue to be used, perhaps thinned a bit (such as replacing tercio-like formations with hollow squares or even lines) – to “significant adjustment”, likely adopting loose formations – which however will make infantry vulnerable to cavalry. This could lead to cavalry-mage combination becoming a dominant battlefield weapon. And presence of magic itself may prevent development of gunpowder, as it will simply not be necessary.