Reasons to jailbreak iPhone: LockLight
LockLight is now one of the top reasons I jailbreak. This awesome tweak will allow you to open the iPhone Flashlight with full screen brightness right from the lock screen–without even looking!
The tweak is called LockLight, and is in the ModMyI repository in Cydia. Searching for LockLight should bring up the application.
LockLight uses the Cydget platform, which allows for multiple lock screen views. Cydget is configurable in Settings.
Configure Cydget such that AwayView is first, and LockLight is lower down in the list. Unfortunately this tweak works best with just LockLight and AwayView, but there are other cool Cydget views that are worth looking at, such as CyCalc.
Once you’ve configured Cydget, Respring your device and enjoy using LockLight!
LockLight Activation:
To start using LockLight, press the power or home button to view the lock screen. Tap the home button to cycle through your enabled Cydgets. If just LockLight and AwayView are enabled, tapping the home button will alternate between the two.
Awesome Windows 7 Logon background
Just something I thought may be good enough to share.
How To: Set logon background in Windows 7:
- Save the image as backgroundDefault.jpg.
- Place the image in c:\Windows\System32\oobe\info\backgrounds\
Note that you’ll need to create the info and backgrounds folders.
- Open gpedit.msc, the Group Policy editor and Navigate to:
Computer Configuration -> Administrative Templates -> System -> Logon
- Set the Always use custom logon background policy to Enabled.
Windows will now load the custom background each time the logon screen is invoked.
Screenshot of Windows 7 Logon UI:
iPhone 3.1 Jailbreak “No Service” fix.
After upgrading my iPhone with pwnage tool 3.1, I no longer had AT&T cellular service. The phone would not display the AT&T Carrier text, and data could only be transferred via WiFi. The text would alternate between Searching… and No Service.
I’ve realized that the issue is that pwnage tool will automatically select “Activate” when Jailbreaking the firmware image.
In order to use your iPhone with OS 3.1 and AT&T, remember not to select Activate when jailbreaking.

Select General to modify Activation options.

Remember to unselect Activate the Phone. This will allow the iPhone to work on AT&T with the proper activation.
Ping remote host in Javascript using WMI
I’ve been working on a simple Windows Sidebar Gadget that will determine if a remote host is online or not. For the connectivity test I chose ping, since it’s lightweight and unlikely to disturb any services on a device.
Ping using WMI
function Ping(host) { var wmi = GetObject("Winmgmts:"); var eStatus = new Enumerator(wmi.ExecQuery( "Select StatusCode from Win32_PingStatus Where Address='" + host + "'")); return eStatus.item().StatusCode == 0; }
The function Ping will return true if the host is online, false otherwise. Optionally, a Timeout parameter may be specified in the WMI query.
Faster Windows Gadget development & testing
Testing and debugging of Windows Sidebar Gadgets may be painful if you wish to test or debug a gadget that has a configuration. Removing and re-creating a gadget by dragging it out of the Gadget window will delete any saved configuration. I’ve found that I would like to restart the sidebar and refresh all the gadgets at once. Restarting the Sidebar process is also the only way to flush the image cache. If you’re having caching problems, restarting the Sidebar will reload the data from disk.
To restart the sidebar with just a click – create the following batch file:
taskkill /F /IM sidebar.exe && "c:\Program Files\Windows Sidebar\sidebar.exe"
Save the file as restart_sidebar.bat. Elevation is not required to restart the sidebar.
C#: Don’t display the startup form.
Something that often seems to be a problem with beginning C# developers is the display of the initial form. Normally one would always want the startup form to be displayed as soon as possible–but sometimes the application lives in the tray, or should not initially display a window.
One may first think that an event in System.Windows.Forms.Form can be handled, and the form may be directed to Hide. Unfortunately, this won’t work as expected, the Form will always get the SW_SHOW message when Application.Run() is executed.
When creating a new project, Visual Studio will generate a Program.cs which looks similar to the following:
namespace CamOn { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
When Application.Run is invoked with a Form parameter, it will create and show the window. The easiest way to override this behavior is to simply not call Application.Run with the form:
namespace CamOn { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); new Form1(); Application.Run(); } } }
Form1 will still be created and pump messages – but it won’t initially be shown.
Get to your Windows desktop faster – secure automatic logon.
The common advice for automatically locking the computer after logging in is to create a batch file and add it to the startup folder. Unfortunately the experience isn’t very elegant, or secure. The desktop and shell will appear while applications are loading, long before the computer will lock itself. The possibility exists for someone to tamper with the computer while it is in this intermediate loading state.
A solution to this problem is to lock the computer sooner in the logon process. There are several good places within the logon process to put a task, but the one that may execute the soonest is certainly Userinit. Userinit is the first program which is executed after logon.
From MSDN: http://msdn.microsoft.com/en-us/library/aa378750(VS.85).aspx
Userinit.exe is an application that is executed when the user has logged on. It runs in the newly logged-on user’s context and on the application desktop. Its purpose is to set up the user’s environment, including restoring network uses, establishing profile settings such as fonts and screen colors, and running logon scripts. After completing those tasks, Userinit.exe executes the user shell programs. The shell programs inherit the environment that Userinit.exe sets up. The specific shell programs that Userinit.exe executes are stored in the Shell key value under the Winlogon registry key.
Add the lock command to Userinit
Create a batch file that will lock the computer:
rundll32.exe user32.dll,LockWorkStation
Name the file lock.bat and place it in the System Root. (c:\windows\system32).
Open regedit and navigate to:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon
The registry value Userinit should contain the path to Userinit.exe
C:\Windows\system32\userinit.exe
Change the value to contain the lock batch file before the Userinit.exe, with a comma between.
C:\Windows\System32\lock.bat, C:\Windows\system32\userinit.exe
Log off to see the result. Note that using a batch file does not discriminate between automatic logon, and user-initiated logon. One could write a program that would only call LockWorkStation if the computer was up for less than 10 minutes.
To enable automatic logon
If automatic logon isn’t already enabled, execute control userpasswords2 at the Run prompt, and uncheck Users must enter a username and password to use this computer.
Set Digsby Status from Command Line
There was a request today on the Digsby forums about setting the status based on a twitter feed. I thought this was a great idea, since this is something I could use. Normally I don’t use status message at all – but I do twitter.
I set to work figuring out how to set the status from outside the application. I went with the same approach that I had used before, simulating a local user via Win32 Window messages. This is a great solution because it doesn’t interfere with any actions that a user may be performing locally – you don’t even know it is happening.
I broke this project up into the two logical components, the part that gets the twitter feed and the part that actually does the work of setting the status. What I’ve come up with is a little command line utility to set the status.
It can be used like so:
DigsbyStatus.exe "This is my new status message"
Or, to clear the status:
DigsbyStatus.exe ""
Quotes are needed as to simulate a “blank” argument. The status classification can’t be changed, so you will still be available or away based on the protocol and the last state of the computer. I figure someone else might find this useful to, say, update status based on some unsupported media player.
Download: DigsbyStatus_100.zip [10KB]
C#: WebClient Usage
Microsoft has provided a great utility since .NET 1.0 for doing quick I/O via websites. The WebClient class provides basic functionality for downloading from and uploading to webservers. the WebClient example on MSDN leaves a lot to be desired, especially for beginners. I’d like to expand on that, as well as provide some example code.
C#: Using WebClient to fetch a page:
// create a new instance of WebClient WebClient client = new WebClient(); // set the user agent to IE6 client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"); try { // actually execute the GET request string ret = client.DownloadString("http://www.google.com/"); // ret now contains the contents of the webpage Console.WriteLine("First 256 bytes of response: " + ret.Substring(0,265)); } catch (WebException we) { // WebException.Status holds useful information Console.WriteLine(we.Message + "\n" + we.Status.ToString()); } catch (NotSupportedException ne) { // other errors Console.WriteLine(ne.Message); }
(This code uses DownloadString, you can also use DownloadData for a more binary-friendly version)
This is great for fetching simple pages that have data encoded in the querystring, but there are some problems with the basic DownloadString method of WebClient. It’s synchronous, so it will block until the operation completes. So for slow connections, or large files, this would need to run in another thread. There is a better way. But first, here’s another example of another basic, but important, method, DownloadFile.
// create a new instance of WebClient WebClient client = new WebClient(); // set the user agent to IE6 client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"); try { // actually execute the GET request client.DownloadFile("http://www.google.com/","google_fetch.txt"); // ret now contains the contents of the webpage Console.WriteLine("File Saved."); } catch (WebException we) { // WebException.Status holds useful information Console.WriteLine(we.Message + "\n" + we.Status.ToString()); } catch (NotSupportedException ne) { // other errors Console.WriteLine(ne.Message); }
This example is almost identical to the above, aside from the client.DownloadFile method. This will save the file to disk, instead of returning a string–often what you would end up doing with the string anyway.
But can I send POST data?
Yes! Using WebClient.UploadString or WebClient.UploadData you can POST data to the server easily. I’ll show an example using UploadData, since UploadString is used in the same manner as DownloadString.
byte[] bret = client.UploadData("http://www.website.com/post.php", "POST", System.Text.Encoding.ASCII.GetBytes("field1=value1&field2=value2") ); string sret = System.Text.Encoding.ASCII.GetString(bret);
UploadData returns a byte array (byte[]) which contains the contents of the page. This can easily be converted to a string by using the System.Text.Encoding.ASCII.GetString() method. You can also use the other encoding types in System.Text.Encoding (such as UTF8) to do the same.
Also note the use of GetBytes(), the UploadData method will only take a byte array as a buffer for the upload. This works well if you are uploading a binary file.
What about the asynchronous methods of WebClient?
WebClient provides asynchronous methods for fetching webpages as well, they are named similarly to the synchronous methods, DownloadFileAsync, DownloadDataAsync, DownloadStringAsync, UploadFileAsync, UploadDataAsync, UploadStringAsync, and UploadValuesAsync.
In order to execute an Async request, you will need to attach event handlers before the method is executed. Without these event handlers you will not have any sense of when the operation finishes, or what stage it is at.
C# WebClient Asynchronous call example:
void do_upload() { WebClient client = new WebClient(); // add event handlers for completed and progress changed client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged); client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted); // carry out the operation as normal client.UploadFileAsync("http://www.daveamenta.com/up.php", @"c:\somefile.bin"); } void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { Console.WriteLine(e.ProgressPercentage); } void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e) { if(e.Result != null) { Console.WriteLine(System.Text.Encoding.ASCII.GetString(e.Result)); } }
The call to client.DownloadFileAsync will no longer block the thread, and will execute in the background, periodically calling the event handlers to display progress. The other methods can be used in this same way.
What is P/Invoke (PInvoke)?
P/Invoke, or Pinvoke stands for Platform Invocation Services. PInvoke is a feature of the Microsoft .NET Frameowrk that allows a developer to make calls to native code inside Dynamic Link Libraries (DLL’s). When Pinvoking, the .NET framework (or Common Language Routine) will load the DLL and handle the type conversions automatically. The most common use of P/Invoke is to use a feature of Windows that is only contained in the Win32 API. The API in Windows is extremely extensive and only some of the features are encapsulated in .NET libraries. For example, Form.Show(); is really a wrapper for the ShowWindow() API found in shell32.dll.
ShowWindow Declaration API for C#:
[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
Note the use of the DllImport attribute. This attribute, coupled with extern tells the .NET CLR where it can find the native declaration for ShowWindow. shell32.dll must be registered on the system for this to work properly.
Note: Remember import the System.Runtime.InteropServices namespace! This is where the [DLLImport] attribute is found.
using System.Runtime.InteropServices;
How are P/Invoked methods called?
Methods that are loaded in using DLLImport are Pinvoked by simply treating them as native .NET functions, and calling them as you would call any other. Win32 API do tend to use data types that are not used often in .NET, for example – the IntPtr datatype, which is actually a pointer to an integer–in this case, a Window Handle. nCmdShow is an integer that represents the show command. But what values of nCmdShow mean what? These can either be found on MSDN, or PInvoke.net.
C# Constants for ShowWindow in shell32.dll:
const int SW_HIDE = 0; const int SW_SHOWNORMAL = 1; const int SW_NORMAL = 1; const int SW_SHOWMINIMIZED = 2; const int SW_SHOWMAXIMIZED = 3; const int SW_MAXIMIZE = 3; const int SW_SHOWNOACTIVATE = 4; const int SW_SHOW = 5; const int SW_MINIMIZE = 6; const int SW_SHOWMINNOACTIVE = 7; const int SW_SHOWNA = 8; const int SW_RESTORE = 9; const int SW_SHOWDEFAULT = 10; const int SW_FORCEMINIMIZE = 11; const int SW_MAX = 11;
These constants can be used to call ShowWindow(). So in our origional example of Form.Show(), the P/Invoked way of doing this would be:
ShowWindow(Form.Handle, SW_SHOW);
This isn’t nearly as clean as the Object-Oriented way of using Form.Show(), but this is what is actually happening under the hood of .NET.
Why would I ever want something so overly complicated?
ShowWindow is an example of an API that has been wrapped nicely by the .NET CLR, it is unlikely that you will ever need to import and call it. But something more useful might be: Deleting a file to the recycle bin.
What resources are out there for PInvoking?
The best resource I have found is PInvoke.net, which has declarations for both VB.NET and C# for commonly used Win32 API.



