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);
  if (szArgList.address != 0) {
    final numberOfArgs = nArgs.value;
    for (var i = 0; i < numberOfArgs; i++) {
      final arg = szArgList[i].toDartString();
      args.add(arg);
    }
    LocalFree(szArgList);
  }

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

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