Skip to content

Instantly share code, notes, and snippets.

@oscarkuo
oscarkuo / Win32AppStarter.cpp
Last active April 17, 2017 19:00
Starter code for a minimal win32 application with idle processing
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void RunMessageLoop(const MSG& msg);
void OnIdle(const MSG& msg);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const wchar_t szClassName[] = L"myWindowClass";
@oscarkuo
oscarkuo / promise-then-catch.js
Created April 5, 2017 09:12
How catch works with multiple promises?
let test1 = new Promise(function(resolve, reject) {
resolve('promise 1 result');
//reject('promise 1 result');
});
test1
.then((test1Result) => {
console.log('then 1 ' + test1Result);
return new Promise(function(resolve, reject) {
@oscarkuo
oscarkuo / SendViaGmail.cs
Created March 18, 2017 01:27
Code for sending an email with attachment via gmail
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace SendViaGmail
{
class Program
{
static void SendEmail(SmtpClient smtp, MailAddress from, MailAddress to, string subject, string body, string attachmentPath)
@oscarkuo
oscarkuo / backup-kvm-vms.sh
Created January 15, 2017 06:05
Backup KVM virtual machine hard disk images
#!/bin/bash
set -e
VMPATHS=()
VMNAMES=()
VMCONFS=()
SSPATHS=()
TGZNAME=/srv/backups/vmbackup-`date '+%y%m%d-%H%M%S'`.tar.gz
TGZLINK=/srv/backups/vmbackup-current.tar.gz
# source http://stackoverflow.com/questions/36200323/get-and-replace-assemblyversion-from-assemblyinfo-cs
function Update-Version($path, $pattern, $template, $newVersion)
{
(Get-Content $path) | ForEach-Object{
if($_ -match $pattern){
$fileVersion = [version]$matches[1]
$now = Get-Date
$newVersion = "{0}.{1}.{2}.{3}" -f $now.Year, $now.ToString("MMdd"), $now.ToString("HHmm"), ($fileVersion.Revision+1)
$template -f $newVersion
protected static WebRequest CreateRequest(string url, string secret)
{
var xml = @"
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns=""https://service/1/0"">
<soapenv:Header/>
<soapenv:Body>
<ns:Request>
<ns:FirstName>XXX</ns:FirstName>
<ns:LastName>YYY</ns:LastName>
</ns:Request>
using NLog;
using NLog.Targets;
/*
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<startup>
@oscarkuo
oscarkuo / recursive-get-keepass-entries.js
Created September 4, 2016 05:08
Recursively map entries in a keepass database using keepass.io
const path = require('path');
const kpio = require('keepass.io');
const db = new kpio.Database();
const dbPath = path.join(__dirname, 'TestDB_Password1234.kdbx');
db.addCredential(new kpio.Credentials.Password('Password1234'));
const findGetFieldValue = (array, key) => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
let fs = require('fs');
let mw = require('nodemw');
let count = 0;
let client = new mw({
protocol: "http",
server: 'en.wikipedia.org',
path: '/w',
debug: false
@echo off
set cassie_running=0
set creeper_running=0
set roguecode_running=0
call:stopVmIfItIsRunning "roguecode" roguecode_running
call:stopVmIfItIsRunning "creeper" creeper_running
call:stopVmIfItIsRunning "cassie" cassie_running