DEV Community

Gurkirat Singh
Gurkirat Singh

Posted on • Edited on

Windows System Programming: Creating Directory

Before diving into the practical of system programming, you should learn about the system programming itself.

System programming is the part of development for system (OS) itself. No, you won't be working with windows OS core code, but you will leverage the Windows API (also called Win-APIs) to create software which provides services to the computer hardware.

Most of the time it deals with

  • Memory management
  • I/O Operations
  • Process creations
  • Network socket handling

In this series you will learn about writing software that extends the OS's functionality, like drivers, system utilities and many more.

Prerequisite Knowledge / Resources

  • Basic of C/C++ and OS
  • Windows 10
  • Visual Studio IDE

So in this post, you will learn about creating directory in windows using Windows API.

You will realise that folder creation is pretty straight-forward in windows, for creating a folder that supports ANSI strings use CreateDirecotryA function and if you want to use wide characters, you can use CreateDirectoryW.

Note: By default windows supports wide character inputs. But in this whole series, I will be using ANSI strings for the ease.

The header file that contains declarations of the function is fileapi.h

What is this Windows.h is doing then? It is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the functions in the Windows API. Read More

The function returns, BOOL which is basically typedef'ed into int. I will be using such datatypes for making the code more compatible.

The CreateDirectoryA function takes in two arguments

  • lpPathName → The path of the directory to be created. It accepts an ANSI string of max 248 characters only. The datatype of this parameter is LPCSTR which is abbv. for Long Pointer Constant STRing, and is typdef'ed to const char *. So you can straightaway use argv[1] which will contain the name of the directory to be created
  • lpSecurityAttributes → It is basically a pointer to the Security Attributes that contains the security descriptor (object's owner and primary group that controls access) of the directory. To set the security attributes, you can follow this link

For now, you can set it to NULL, and it will inherit the security description from the parent directory.

#include <Windows.h>
#include <iostream>
#include <fileapi.h> // for create directory

int main(int argc, char** argv)
{

    if (argc == 1) {
        printf("usage: %s <directory name>\n", argv[0]);
        return 1;
    }

    /** 
        * Documentation for the function 
        * https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createdirectorya
        */  
BOOL flag = CreateDirectoryA(argv[1], NULL);

    if (flag) {
        printf("Directory Created\n");
    } else {
        switch (GetLastError())
        {
        case ERROR_ALREADY_EXISTS:
            printf("ERR: Directory already exists\n");
            break;
        case ERROR_PATH_NOT_FOUND:
            printf("ERR: Path not found\n");
            break;
        default:
            break;
        }
        return 1;
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Have you got into thinking what is ERROR_? These are the macros of error code returned by GetLastError function (declared in errhandlingapi.h). To see the full list of system errors, open this link. In this case, there can be only two error codes ERROR_ALREADY_EXISTS and ERROR_PATH_NOT_FOUND.

I hope you have enjoyed this post. Follow the links to reach me

Top comments (1)

Collapse
 
ajinkyax profile image
Ajinkya Borade

thanks for creating such a lovely series. I recently finished C++ fundamentals, hope to learn practicals from your posts :)