initApp function

void initApp(
  1. Function winMain
)

Sets up a WinMain function with all the relevant information.

Add the following line to your command line app:

void main() => initApp(winMain);

Now you can declare a winMain function as:

void winMain(int hInstance, List<String> args, int nShowCmd) {
...
}

Implementation

void initApp(Function winMain) {
  final nArgs = calloc<Int32>();
  final args = <String>[];
  final lpStartupInfo = calloc<STARTUPINFO>();

  // Parse command line args using Win32 functions, to reduce ceremony in the
  // app that uses this.
  final szArgList = CommandLineToArgv(GetCommandLine(), nArgs);
  for (var i = 0; i < nArgs.value; i++) {
    args.add(szArgList[i].toDartString());
  }
  LocalFree(szArgList.address);

  final hInstance = GetModuleHandle(nullptr);
  GetStartupInfo(lpStartupInfo);

  try {
    winMain(
        hInstance,
        args,
        lpStartupInfo.ref.dwFlags & STARTF_USESHOWWINDOW == STARTF_USESHOWWINDOW
            ? lpStartupInfo.ref.wShowWindow
            : SW_SHOWDEFAULT);
  } finally {
    free(nArgs);
    free(lpStartupInfo);
  }
}