0

Why do old windows API's require you to pass so many parameters?

example(from stack overflow) :

// additional information
   STARTUPINFO si;     
   PROCESS_INFORMATION pi;

   // set the size of the structures
   ZeroMemory( &si, sizeof(si) );
   si.cb = sizeof(si);
   ZeroMemory( &pi, sizeof(pi) );

  // start the program up
  CreateProcess( lpApplicationName,   // the path
    argv[1],        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread

);

Why so complicated? Every parameter is required, we do not need to force the users to pass in every parameters at once, they could be setup later, provide a "constructor" and so on.

edit : The C language doesn't support default values, so I remove this option

user438383
  • 5,716
  • 8
  • 28
  • 43
StereoMatching
  • 4,971
  • 6
  • 38
  • 70
  • Check out http://tronche.com/gui/x/xlib/window/XCreateWindow.html – user541686 Aug 16 '14 at 06:48
  • There are no default parameters in C – David Ranieri Aug 16 '14 at 06:49
  • Every one of those parameters has a specific purpose. That you're not using them means they don't fit *your* specific usage. You can always wrap them up to facilitate your needs only. That MS chose not to do that *for you* isn't a problem with the API. – WhozCraig Aug 16 '14 at 06:53

1 Answers1

2

MS chose to provide a "small" number of very flexible APIs, allowing the programmer to do a large number of things. If they didn't add all of those parameters, then there's various things that wouldn't be possible to do. And remember this API is in C- no default values (and really default values are a bad idea in such a large function call) and no classes with default constructors (hence the need to 0 out memory like in this example) that can set default values. And in a lot of cases MS didn't want to guess on what default values should be- they didn't know how the programmers were going to use their APIs and decided not to assume. Its a different philosophy from today's Java based, everything's a framework, hold the newbie's hand (and handcuff the guy who actually knows what he's doing) world.

In addition in some cases they decided to make things easier for backwards compatibility- for example the need to set si.cb to the size of the startup structure- this is so the startup structure could be versioned (later versions would have more fields thus a different size) and it could be made to work with either version. Of course this made the API a bit more complicated, but it made everyone happier than having all executables break every new OS.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127