11/15/10

PInvoke SetWindowText PowerShell Script

P/Invoke examples for the SetWindowText Windows API Function.
These examples set the window text/title for Notepad.exe.
#get notepad window handle
$notepad = get-process notepad
$notepad.mainWindowHandle //100000, intptr structure

$pinvoke::setWindowText($notepad.mainWindowHandle, "a")

#auto-convert int to intptr
$pinvoke::setWindowText(100000, "b")

#create intptr
#out-null to suppress bool result
$pinvoke::setWindowText((new-object intPtr(100000)), "c") | out-null

$pinvoke::setWindowTextCustomWrapper(100000)

Implementation #1 simply exposes the SetWindowText method, the member definition uses C# syntax.
$pinvoke = add-type -name pinvoke -passThru -memberDefinition @'

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);

'@

Implementation #2, same as #1 but written on one line.
$pinvoke = add-type -name pinvoke -passThru -memberDefinition '[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern bool SetWindowText(IntPtr hwnd, String lpString);'

Implementation #3, SetWindowText is private, add custom wrapper method.
$pinvoke = add-type -name pinvoke -passThru -memberDefinition @'

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SetWindowText(IntPtr hwnd, String lpString);

public static void SetWindowTextCustomWrapper(IntPtr handle)
{
string customText = string.Format("handle: {0}", handle);
SetWindowText(handle, customText);
}

'@


11/3/10

Git Custom Difftool Configuration

Original solution by David Tchepak: Setting up Git difftool on Windows

This example shows the configuration for ExamDiff Pro.
Tested on Windows 7 and msysgit version 1.7.3.1

1. Set the path environment variable to include "c:\program files\git\cmd\;"
2. Create a shell script wrapper file, note the forward slashes in the executable path
#c:\program files\git\cmd\diff.sh
#!/bin/sh
"c:/program files/examdiff pro/examdiff.exe" "$1" "$2" | cat

3. Edit the .gitconfig file
#c:\users\john\.gitconfig
[diff]
tool = examdiff

[difftool "examdiff"]
cmd = diff.sh "$LOCAL" "$REMOTE"
...

4. Verify it works, run the difftool command: git difftool --no-prompt


Setting Windows Path Environment Variable with PowerShell

#get all environment vars
gi env:
(gi env:) | sort name

#get path value
gi env:path
(gi env:path).value
(gi env:path).value.split(';')
(gi env:path).value.split(';') | sort

#set path value (note the "$" prefix)
$env:path = 'c:\windows;c:\windows\system32;'