Skip to content

Instantly share code, notes, and snippets.

View timsneath's full-sized avatar

Tim Sneath timsneath

View GitHub Profile
#include "windows.h"
#include "Shobjidl.h"
// vtable_start 4
MIDL_INTERFACE("42f85136-db7e-439c-85f1-e4075d135fc8")
IFileDialog : public IModalWindow
{
public:
virtual HRESULT STDMETHODCALLTYPE SetFileTypes(
/* [in] */ UINT cFileTypes,
// Basic Petzoldian "hello world" Win32 app
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
final hInstance = GetModuleHandle(nullptr);
int mainWindowProc(int hWnd, int uMsg, int wParam, int lParam) {
final result = MessageBox(
NULL,
TEXT("This is one of those messages that nobody will ever read. They'll "
"just click the default button, and about a millisecond later, "
"they'll experience a pang of concern as they wonder whether Windows "
"was giving them one last opportunity to perform a critical task "
"before all their files were wiped.\n\nWhat even is the difference "
"between Cancel, Try Again and Continue?"),
TEXT('Critical system message'),
MB_ICONEXCLAMATION | // Warning
// C signature for Win32 API in kernel32.dll:
// BOOL WINAPI SetConsoleCursorPosition(
// _In_ HANDLE hConsoleOutput,
// _In_ COORD  dwCursorPosition
// );
typedef nativePrototype = Int32 Function(
IntPtr hConsoleOutput, Int32 dwCursorPos);
typedef dartPrototype = int Function(int hConsoleOutput, int dwCursorPos);
@timsneath
timsneath / PolarCoordinate.dart
Last active May 27, 2023 18:08
Demonstrates a polar coordinate system with Flutter
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/gestures.dart';
const double kTwoPi = 2 * math.pi;
class SectorConstraints extends Constraints {
const SectorConstraints({
int error() {
try {
return 0;
} finally {
print('finally entered');
}
}
void main() {
try {
// struct XrApplicationInfo {
// char applicationName[128]; // 128 bytes
// uint32_t applicationVersion; // 4 bytes
// char engineName[128]; // 128 bytes
// uint32_t engineVersion; // 4 bytes
// XrVersion apiVersion; // assume this is a uint64_t, i.e. 8 bytes
// };
class XrApplicationInfo extends Struct {
Pointer<Utf8> get applicationName => addressOf.cast<Utf8>();
@timsneath
timsneath / msgbox.dart
Last active June 16, 2020 21:21
Code fragment for embedding. See https://gist.github.com/timsneath/181092c75864001ca37b1b1495b9b396 for a full example that you can run.
typedef MessageBoxNative = Int32 Function(
IntPtr hWnd, Pointer<Utf16> lpText, Pointer<Utf16> lpCaption, Int32 uType);
typedef MessageBoxDart = int Function(
int hWnd, Pointer<Utf16> lpText, Pointer<Utf16> lpCaption, int uType);
final user32 = DynamicLibrary.open('user32.dll');
final win32MessageBox =
user32.lookupFunction<MessageBoxNative, MessageBoxDart>('MessageBoxW');
void showMessageBox(String message, String caption) => win32MessageBox(
@timsneath
timsneath / msgbox.dart
Last active April 10, 2021 15:22
MessageBox with Dart FFI -- a simple example
// msgbox.dart
// Demonstrates a simple example of calling a Win32 API directly from Dart code.
// This sample relies on the ffi package. Download these two files to an empty
// directory and run `pub get` to download the ffi package prior to running this
// example.
import 'dart:ffi';
import 'package:ffi/ffi.dart';
String numToString(BigInt number) {
final buf = StringBuffer();
for (var v = 0; v < 8; v++) {
final charCode = (number & BigInt.parse('0xFF' + ('00' * v))) >> v * 8;
buf.write(String.fromCharCode(charCode.toInt()));
}
return buf.toString();
}