The case of the Create­File­Mapping that always reported ERROR_ALREADY_EXISTS - The Old New Thing
Skip to main content
Search<br>Search
No results
Cancel
Raymond Chen
A customer reported that whenever their program called CreateFileMapping to create a named file mapping, the call succeeded, but the resulting mapping was not the size they wanted. They requested a 1 megabyte mapping, but the mapping they got back was only 4KB, which they noticed because the program crashed once it accessed the 4097th byte. As an additional data point, if they call GetLastError() after creating the file mapping, they get ERROR_ALREADY_EXISTS, suggesting that the file mapping already created. But this happens even the first time their program was run, and it even happens immediately after a reboot so there shouldn’t be any leftover mappings.
HANDLE h = CreateFileMappingW(INVALID_FILE_HANDLE, nullptr, PAGE_READWRITE,<br>0, 65536, L"MyFileMapping");
My guess is that they are getting ERROR_ALREADY_EXISTS beacuse the mapping already exists. (Quelle surprise !)
After a fresh reboot, the customer used Process Explorer to search all processes to see if any of them already had a handle to their file mapping, and lo and behold, they found one: It was some companion software for their webcam, and it chose the exact same uncreative file mapping name.
The customer appended a GUID to their file mapping name, thereby removing the possibility of an accidental name collision. (Of course, there is still the possibility of an intentional name collision. Not much you can do to protect yourself against an attacker at the same or higher privilege.)
Related reading : You can name your car, and you can name your kernel objects, but there is a qualitative difference between the two.
Category<br>Old New Thing
Topics<br>Code
Share
Create­File­Mapping that always reported ERROR_ALREADY_EXISTS" data-bi-id="post_page_body_share post" data-post-id=112327 data-bi-name="Share on X" aria-label="Share on X" target="_blank" rel="noreferrer noopener" class="d-flex share-post">
Author
Raymond Chen
Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.
2 comments
Join the discussion.
Leave a commentCancel reply<br>Sign in
Code of Conduct
Sort by :
Newest
Newest<br>Popular<br>Oldest
Another victory for the UWP sandbox.
The issue here is that the name for this type of object is optional. This means that you only really name the object to share it. This means that the UWP BNO isolation has as much of a chance of interfering here.
What’s more, avoiding accidental collisions ends up being a matter of how much effort that you are willing to put into it. If you really want to increase the odds of not running into this kind of issue, then private namespaces are available to desktop applications.
Read next
May 14, 2026
A constant-space linear-time algorithm for deleting all but the 10 most recent files in a directory
Raymond Chen
May 13, 2026
The case of the hang when the user changed keyboard layouts
Raymond Chen
Stay informed
Get notified when new posts are published.
Email *
Country/Region *<br>Select...United StatesAfghanistanÅland IslandsAlbaniaAlgeriaAmerican SamoaAndorraAngolaAnguillaAntarcticaAntigua and BarbudaArgentinaArmeniaArubaAustraliaAustriaAzerbaijanBahamasBahrainBangladeshBarbadosBelarusBelgiumBelizeBeninBermudaBhutanBoliviaBonaireBosnia and HerzegovinaBotswanaBouvet IslandBrazilBritish Indian Ocean TerritoryBritish Virgin IslandsBruneiBulgariaBurkina FasoBurundiCabo VerdeCambodiaCameroonCanadaCayman IslandsCentral African RepublicChadChileChinaChristmas IslandCocos (Keeling) IslandsColombiaComorosCongoCongo (DRC)Cook IslandsCosta RicaCôte dIvoireCroatiaCuraçaoCyprusCzechiaDenmarkDjiboutiDominicaDominican RepublicEcuadorEgyptEl SalvadorEquatorial GuineaEritreaEstoniaEswatiniEthiopiaFalkland IslandsFaroe IslandsFijiFinlandFranceFrench GuianaFrench PolynesiaFrench Southern TerritoriesGabonGambiaGeorgiaGermanyGhanaGibraltarGreeceGreenlandGrenadaGuadeloupeGuamGuatemalaGuernseyGuineaGuinea-BissauGuyanaHaitiHeard Island and McDonald IslandsHondurasHong Kong SARHungaryIcelandIndiaIndonesiaIraqIrelandIsle of ManIsraelItalyJamaicaJan MayenJapanJerseyJordanKazakhstanKenyaKiribatiKoreaKosovoKuwaitKyrgyzstanLaosLatviaLebanonLesothoLiberiaLibyaLiechtensteinLithuaniaLuxembourgMacau SARMadagascarMalawiMalaysiaMaldivesMaliMaltaMarshall IslandsMartiniqueMauritaniaMauritiusMayotteMexicoMicronesiaMoldovaMonacoMongoliaMontenegroMontserratMoroccoMozambiqueMyanmarNamibiaNauruNepalNetherlandsNew CaledoniaNew...