Search code examples
winapimkdircreate-directory

Difference between mkdir and CreateDirectory


Is there a difference between mkdir(<name>) and CreateDirectory(<name>, NULL) under Win32.

As I can see, both are working (in the same way ??)


Solution

  • mkdir (and the recommended _mkdir are runtime library functions. CreateDirectory is specific to Windows. If you want portable code, call _mkdir. If you're fine making your program Windows-specific, or you need the ability to add security descriptors, then call CreateDirectory.

    Most likely, the _mkdir implementation for Windows calls CreateDirectory(name, NULL). So both end up doing the same thing.

    Edit: The Visual Studio 12 implementation of _mkdir() calls _wmkdir(), which then calls CreateDirectoryW:

    int __cdecl _wmkdir (
            const wchar_t *path
            )
    {
            ULONG dosretval;
    
            /* ask OS to create directory */
    
            if (!CreateDirectoryW(path, (LPSECURITY_ATTRIBUTES)NULL))
                dosretval = GetLastError();
            else
                dosretval = 0;
    
            if (dosretval) {
                /* error occured -- map error code and return */
                _dosmaperr(dosretval);
                return -1;
            }
    
            return 0;
    }