Skip to content

Instantly share code, notes, and snippets.

View derekgates's full-sized avatar

Derek Gates derekgates

View GitHub Profile
@derekgates
derekgates / s2ws.cpp
Created March 10, 2020 05:53
C++: convert strings to a usable string in MessageBox
//used to convert strings to a usable string in MessageBox, example:
//std::wstring stemp = s2ws(executablePath);
//MessageBox(0, stemp.c_str(), L"", MB_OK);
//https://stackoverflow.com/a/27296
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
@derekgates
derekgates / AutoHotKey.ahk
Last active March 30, 2020 16:29
AutoHotkey MediaKey support using built in media commands (or WinAMP if open) which should work with any player in modern Windows versions. I placed this file in %documents% folder so AHK would see it as default script.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; # is WinKey, + is shift
; https://www.autohotkey.com/docs/Tutorial.htm#s2
; https://autohotkey.com/board/topic/70048-ituneswindows-media-playerwinamp-autohotkey-script/
@keyframes yellowfade {
from { background: yellow; }
to { background: transparent; }
}
.new-item {
animation-name: yellowfade;
animation-duration: 1.5s;
}
@derekgates
derekgates / Controller.cs
Last active February 19, 2016 22:37
System for setting error/warning/info messages from MVC to view via Controller (using TempData and ViewBag). Filter is used to copy TempData entries to ViewBag on every view execution.
/// <summary>
/// Sets an error to display on the next postback. This will NOT survive a redirect, use RedirectWithErrorResult!
/// </summary>
/// <param name="error"></param>
public void SetPostBackError(string error)
{
this.ViewBag.Error = error;
}
/// <summary>
@derekgates
derekgates / gist:c070a687178aa14156ff
Created April 20, 2015 18:00
Subquery join in Entity (with TOP 1)
var inquery = from en in Course_Enrollments
from t in (
(from enr in Course_Enrollments
join usr in User_Users on enr.UserID equals usr.UserID
join offr in Course_CourseOfferings on enr.CourseOfferingID equals offr.CourseOfferingID
where offr.CourseOfferingID == 92 && enr.UserID == en.UserID
orderby enr.ModifiedDate
@derekgates
derekgates / gist:aa0d373adba1309739ad
Created October 4, 2014 03:24
Moving WAN port of a OpenWRT router (DD-WRT, Tomato, etc) such as when lightning kills the normal WAN port.
# http://www.dd-wrt.com/wiki/index.php/Switched_Ports#Second_WAN_port
# http://wiki.openwrt.org/toh/asus/wl500gp
# tested on an ASUS WL-520gu
# LAN4 was 4, so I made port 0 (WAN) bridge with port 4 (LAN4)
nvram set vlan0ports="1 2 3 5*"
nvram set vlan1ports="0 4 5"
#verify results
nvram find vlan
@derekgates
derekgates / adium updater.cfg
Created August 7, 2014 18:15
Adium Addon Updater. Updated from code on http://www.adiumxtras.com/index.php?a=xtras&xtra_id=5837 to handle Info.plist not existing.
/Applications/Adium.app/Contents/Resources/Contact List
/Applications/Adium.app/Contents/Resources/Dock Icons
/Applications/Adium.app/Contents/Resources/Emoticons
/Applications/Adium.app/Contents/Resources/Menu Bar Icons
/Applications/Adium.app/Contents/Resources/Message Styles
/Applications/Adium.app/Contents/Resources/Scripts
/Applications/Adium.app/Contents/Resources/Service Icons
/Applications/Adium.app/Contents/Resources/Sounds
/Applications/Adium.app/Contents/Resources/Status Icons
~/Library/Application Support/Adium 2.0/Contact List
@derekgates
derekgates / gist:8305638
Created January 7, 2014 19:49
ClonePublicPropertiesAndFields() clones all properties and fields of a given class; practically making a shallow copy.
/// <summary>
/// Clones all public properties and fiels on one object to another (excluding index based properties). The properties
/// must have the same name scheme. Type does not matter.
/// </summary>
/// <param name="o"></param>
/// <param name="destination">Where to clone the properties to.</param>
public static void ClonePublicPropertiesAndFields(this object o, object destination)
{
if (destination == null) throw new ArgumentNullException("destination", "Destination cannot be null!");
@derekgates
derekgates / gist:8305620
Created January 7, 2014 19:48
DynamicCast() casts an unknown type to a given type. This requires .net 4.0 for dynamics.
/// <summary>
/// Dynamically casts a type to another type without knowing the CLR knowing the object's type. Before calling this method,
/// ensure the object's type matches what you are trying to cast to!
/// </summary>
/// <exception cref="System.InvalidCastException"></exception>
public static dynamic DynamicCast(this Type T, dynamic o)
{
return typeof(ReflectionHelpers).GetMethod("Cast", BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(T).Invoke(null, new object[] { o });
}
@derekgates
derekgates / gist:8305602
Created January 7, 2014 19:47
PrintPublicPropertiesAndFields() makes a clean string representation of a class with it's properties and fields.
/// <summary>
/// Returns a string containing all of the public properties and fields for a given type.
/// </summary>
/// <param name="o">Type to print properties and fields for.</param>
/// <returns>Properties and fields seperated by newlines.</returns>
public static string PrintPublicPropertiesAndFields(this object o, string seperator = "\r\n")
{
StringBuilder sb = new StringBuilder();
Type otype = o.GetType();