Code Snippets

This is a collection of code snippets, in various languages, that I'd like to share. Some are just functions, subroutines, or classes and others are complete scripts that can be used as-is.
 

VBScript: Create Shortcut (.lnk) File

0
Your rating: None

Following is a starting point for a script which will create a standard shortcut on the desktop.

VB.NET: Get User's SID from Active Directory

0
Your rating: None

This function will connect to Active Directory and search for a specified username, and if found, return that user's SID.

Usage: GetSIDUsingADSearch("john.doe")
Returns: S-1-5-21-53580104-3876648466-4083845538-427214

VB.NET: Parse "Special" Shortcuts

0
Your rating: None

I recently had a problem in one of my applications where, when trying to parse .lnk files for target (using the WshShell approach), I would get something like C:\Windows\Installer\{GUID} instead of the path to the executable. Turns out some .lnk files (shortcuts) are of a different breed, ala "MSI Shortcut" or "Windows Installer Advertised Application".

Here is a module which will let you get the Target path of such a shortcut. It returns NULL if it's a standard .lnk, which you could use to determine whether to use the WshShell way instead.

VB.NET: Encrypt/Decrypt String (DES)

0
Your rating: None

These functions will encrypt and decrypt (using DES) a string using a secret key.

Encrypt("I need this string to be a secret", "$ThIsIsAsUpErSeCrEtKeY!") will return a string with the encrypted text.

Decrypt("5FkBed78KfrLmoU==BNu37N5kl", "$ThIsIsAsUpErSeCrEtKeY!") will return the original string that was encrypted with Encrypt() and the same key.

If a different key is used for Decrypt() than was originally used for Encrypt(), an exception will be thrown.

VBScript: Recursively Delete Files

0
Your rating: None

This script will attempt to delete all files in a particular location, including subfolders, but silently continue on error.

VBScript: Get a computer's IP address(es)

0
Your rating: None

Returns a string of addresses, separated by pipe (|), with a leading pipe:
|192.168.0.12|172.16.31.64|10.1.1.2

VBScript: Get Script's Location

0
Your rating: None

The scriptLocation variable will contain the path to executing script.

  1. Set objFSO = CreateObject("Scripting.FileSystemObject")
  2. scriptLocation = objFSO.GetFile(Wscript.ScriptFullName).ParentFolder

CSS: Remove indent and bullet from list items

0
Your rating: None

This style will remove the indent and bullet from a list

  1. ul, ol
  2. {
  3. list-style: none;
  4. margin-left: 0;
  5. padding-left: 1em;
  6. text-indent: -1em;
  7. }

PHP: Create a Microsoft GUID

0
Your rating: None

This function will return a standard 128-bit GUID, consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits.

Example: 6B29FC40-CA47-1067-B31D-00DD010662DA

Visual Basic .NET: Get carat position in a RichTextBox control

0
Your rating: None

This function can be called by KeyPress, Click, and similar events to return a string with the X and Y coordinates of the carat within a RichTextBox control.

Example: "Line 35, Col 18"

  1. Function CursorPosition(ByVal Control)
  2. Return "Line " & (Control.GetLineFromCharIndex(Control.SelectionStart) + 1).ToString & ", Col " & (Control.SelectionStart - Control.GetFirstCharIndexOfCurrentLine() + 1).ToString
  3. End Function