Family Shot

Posted in Uncategorized | 1 Comment

Doing a join in an update in SQL for PostgreSQL

Was writing some SQL queries for PostgreSQL for updating some tables and did a touch of trial and error for getting the syntax right for doing a join within an update statement. Below is how I did it. I’m thinking there should be a neater way.

update table1 set columnZ = newValue from table1 t1
inner join table2 t2
on t1.columnX = t2.columnY
where table1.uniqueID = t1.uniqueID
and t2.filterColumn = filterValue

Posted in Programming Tips and Tricks | 1 Comment

Recovered my dead / bricked acer aspire one

Had the Windows 7 Release Candidate on my Acer Aspire One, in the process of trying to reinstall XP from a USB key I ended up bricking my machine. Luckily I found the solution on this web page , my exact steps were:

I downloaded the latest BIOS from Acer Taiwan here here.

Freshly formatted a USB key to FAT32.

Copied the 3 files (3310.fd, flashit.exe, 3310.bat) to the USB key.

Renamed 3310.fd to ZG5IA32.fd, renamed 3310.bat to ZG5IA32.bat, and changed the contents of the batch file (ZG5IA32.bat) to point to ZG5IA32.fd

Put the USB key in the left side USB port

With the netbook off, held down FN+ESC, turned on laptop, released FN+ESC when the power light started flashing, pressed the power button once, and the USB key started flashing for about 1 minute and then the laptop rebooted. Fixed!

Posted in Uncategorized | 1 Comment

How to show a Child Window from a Console window in C#

An amusing little example I came up with of how to open a window that is a child window of a console window. Just in case I need it in the future. Also useful for spawning a window from an OpenGL app, or hanging a child window off a separate thread.

// This is an example of how to open a Child Window from a Console

// Window in C#.

// By John Stewien 2010

 

using System;

using System.Text;

using System.Threading;

using System.Runtime.InteropServices;

using System.Windows.Forms;

 

namespace ConsoleApplication {

 

  /// <summary>

  /// A simple form with a CheckBox that can be used to verify the

  /// responsiveness of the Form.

  /// </summary>

  public class Form1 : Form {

    public Form1() {

      CheckBox checkBox1 = new System.Windows.Forms.CheckBox();

      checkBox1.AutoSize = true;

      checkBox1.Text = "Check / Uncheck to test responsiveness";

      Controls.Add(checkBox1);

      Text = "Form1";

    }

  }

 

  class Program {

 

    // Import the required Windows API functions

 

    [DllImport("User32.dll", CharSet = CharSet.Auto)]

    static extern IntPtr FindWindow(char[] lpClassName, char[] lpWindowName);

 

    [DllImport("User32.dll", CharSet = CharSet.Auto)]

    static extern int SetWindowLongW(HandleRef hWnd, int nIndex, IntPtr dwNewLong);

    const int GWLP_HWNDPARENT = -8;

 

    // Store these parameters as statics as they are called from static methods

 

    static IntPtr consoleHWnd;

    static Form1 form;

 

    /// <summary>

    /// Method for running the form, called from a thread

    /// </summary>

    static void RunForm() {

      form = new Form1();

      SetWindowLongW(new HandleRef(form, form.Handle), GWLP_HWNDPARENT, consoleHWnd);

      Application.Run(form);

    }

 

    // Delegate for invoking a method with no parameters

    delegate void SimpleDelegate();

 

    static void Main(string[] args) {

 

      // Set the Console Title and then find its HWND

      Console.Title = "Child Window On Console Test";

      consoleHWnd = FindWindow("ConsoleWindowClass".ToCharArray(), Console.Title.ToCharArray());

 

      // Run the Form

      Thread thread = new Thread(new ThreadStart(RunForm));

      thread.Start();

 

      // Do some text stuff on the Console to show it is working

      Console.WriteLine("Enter Text. Enter \"exit\" to exit.");

      string text = "";

      while (text != "exit")

        text = Console.ReadLine();

 

      // Close the Form so we can exit

      form.Invoke(new SimpleDelegate(form.Close));

    }

  }

}

 

Posted in Uncategorized | Leave a comment

Getting Oz Solomon’s Project Line Counter to work in VS2008

I’m a big fan of Oz Solomon’s Project Line Counter from here http://www.wndtabs.com/, however there isn’t a version for Visual Studio 2008. I’ve got a work around though 🙂

Install the version for Visual Studio 2005, which fortunately installs even if Visual Studio 2005 isn’t present, then copy the following to notepad, save it and rename it to a .reg file, then double click it to enter it into the registry. After that, start VS2008 and you will see it there.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Addins\LineCount]
"FriendlyName"="Project Line Counter"
"AboutBoxDetails"="Project Line Counter Addin  —  Build 2.21.2.1024
Copyright (c) 1999-2006 by Oz Solomon
http://www.wndtabs.com"
"AboutBoxIcon"="C:\\Program Files\\WndTabs.com\\LineCount\\1033\\LineCountResources.dll,1"
"CommandLineSafe"=dword:00000000
"CommandPreload"=dword:00000001
"Descrption"="Counts lines of code in your projects"
"LoadBehavior"=dword:00000001
"SatelliteDllName"="LineCountResources.dll"
"SatelliteDllPath"="C:\\Program Files\\WndTabs.com\\LineCount\\"

Posted in Uncategorized | 1 Comment

Taking a sunset photo with fill flash

I’ve recently acquired a new Canon 500D digital SLR camera, and I’ve been experimenting with different scenes. Some are trickier than others, e.g. to take a photo of my daughter with a sunset in the background required a bit of figuring out. Here’s one way of doing it:

1) Turn the mode dial to Aperture Priority
2) Set aperture to F stop 8.0
3) Set ISO to 200
4) Set exposure metering to spot metering
5) Set Auto Focus to One Shot
6) Close the built in flash
7) Point camera at daughter and half depress the shutter button to focus

While maintaining the shutter button in the half-depress (to hold the focus) do the following:

8) Point camera at the sky and press the Auto Exposure Lock button (AE button)
9) Press the flash release button to open the built in flash – this also changes the function AE button to a Flash measure button
10) Point camera at the whitest/brightest object in the foreground and press the AE button to fire off a pre-flash and set the flash strength
11) Repoint the camera at daughter and push the shutter button all the way down

One alternative is get up to step 9 by going full manual and setting up the exposure that way.

Posted in Uncategorized | Leave a comment

HttpListener Proxy

Just in case you find a use for some web proxy code…

 

I wrote a HttpListener proxy based on the HttpListener MSDN docs, and the web request code I did for something else. I had to do this because I needed to use the X-Plane updater from work which let me specify only an anonymous proxy, not the authenticated type that is at work. Here’s the code I got working for streaming binary data (note the HttpListener MSDN code only works for text as I discovered).

 

To test it I guess you could set a proxy in your web browser as 127.0.0.1:8080. The code below is the complete program.

 

using System;

using System.Net;

using System.Collections.Generic;

using System.Text;

using System.IO;

 

namespace HttpListenerProxy

{

      class RequestData

      {

            public HttpWebRequest WebRequest;

            public HttpListenerContext Context;

 

            public RequestData(HttpWebRequest request, HttpListenerContext context)

            {

                  WebRequest = request;

                  Context = context;

            }

      }

 

      class Program

      {

            static void Main(string[] args)

            {

                  // Create a listener.

                  HttpListener listener = new HttpListener();

                  listener.Prefixes.Add("http://*:8080/&quot;);

 

                  listener.Start();

                  try

                  {

                        while (true)

                        {

                              Console.WriteLine("Listening…");

                              // Note: The GetContext method blocks while waiting for a request.

                              HttpListenerContext context = listener.GetContext();

                              string requestString = context.Request.RawUrl;

                              Console.WriteLine("Got request for " + requestString);

                              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestString);

                              request.KeepAlive = false;

                              request.Proxy.Credentials = CredentialCache.DefaultCredentials;

                              request.Timeout = 200000;

 

                              RequestData requestData = new RequestData(request, context);

                              IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), requestData);

                        }

                  }

                  catch (Exception e)

                  {

                        Console.WriteLine("\nMain Exception raised!");

                        Console.WriteLine("Source :{0} ", e.Source);

                        Console.WriteLine("Message :{0} ", e.Message);

                  }

 

                  listener.Stop();

            }

 

            static void RespCallback(IAsyncResult asynchronousResult)

            {

                  try

                  {

 

                        // State of request is asynchronous.

                        RequestData requestData = (RequestData)asynchronousResult.AsyncState;

                        Console.WriteLine("Got back response from " + requestData.Context.Request.Url.AbsoluteUri);

 

                        using(HttpWebResponse response = (HttpWebResponse)requestData.WebRequest.EndGetResponse(asynchronousResult))

                        using (Stream receiveStream = response.GetResponseStream())

                        {

                              HttpListenerResponse responseOut = requestData.Context.Response;

 

                              // Need to get the length of the response before it can be forwarded on

                              responseOut.ContentLength64 = response.ContentLength;

                              int bytesCopied = CopyStream(receiveStream, responseOut.OutputStream);

                              responseOut.OutputStream.Close();

                              Console.WriteLine("Copied {0} bytes", bytesCopied);

                        }

                  }

                  catch (Exception e)

                  {

                        Console.WriteLine("\nMain Exception raised!");

                        Console.WriteLine("Source :{0} ", e.Source);

                        Console.WriteLine("Message :{0} ", e.Message);

                  }

            }

 

            public static int CopyStream(Stream input, Stream output)

            {

                  byte[] buffer = new byte[32768];

                  int bytesWritten = 0;

                  while (true)

                  {

                        int read = input.Read(buffer, 0, buffer.Length);

                        if (read <= 0)

                              break;

                        output.Write(buffer, 0, read);

                        bytesWritten += read;

                  }

                  return bytesWritten;

            }

      }

}

 

Posted in Uncategorized | 1 Comment

We went Sailing

Posted in Uncategorized | 1 Comment

fixed: error C2784 from compiling adding an entry to a std::map

Got the above error, when compiling something that looked like this:

std::map<std::string, myType> myMap;
myMap[“Test”] = myType();

Looked around on the net, didn’t find the answer, thought about it for a bit and then realised I was missing #include <string> which fixed the problem. The error was a bit cryptic. Look for yourself at this dump from the compiler output:

 1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(142) : while compiling class template member function ‘bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const’

1>        with

1>        [

1>            _Ty=std::string

1>        ]

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\map(72) : see reference to class template instantiation ‘std::less<_Ty>’ being compiled

1>        with

1>        [

1>            _Ty=std::string

1>        ]

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(26) : see reference to class template instantiation ‘std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>’ being compiled

1>        with

1>        [

1>            _Kty=std::string,

1>            _Ty=std::string,

1>            _Pr=std::less<std::string>,

1>            _Alloc=std::allocator<std::pair<const std::string,std::string>>,

1>            _Mfl=false

1>        ]

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(68) : see reference to class template instantiation ‘std::_Tree_nod<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(94) : see reference to class template instantiation ‘std::_Tree_ptr<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(112) : see reference to class template instantiation ‘std::_Tree_val<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\map(82) : see reference to class template instantiation ‘std::_Tree<_Traits>’ being compiled

1>        with

1>        [

1>            _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>

1>        ]

1>        c:\projects\flewse\source\x-planeflewse\VisualSettings.h(16) : see reference to class template instantiation ‘std::map<_Kty,_Ty>’ being compiled

1>        with

1>        [

1>            _Kty=std::string,

1>            _Ty=std::string

1>        ]

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’

1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2676: binary ‘<‘ : ‘const std::string’ does not define this operator or a conversion to a type acceptable to the predefined operator

 

Posted in Uncategorized | 21 Comments

Handling all mouse movements at the Main Form level in C#

In a C# WinForms application, mouse movement events always go to the child control that the mouse moves over. Sometimes you might want to track mouse movements at the Form level instead of handling them at the child control level. To do this you need to handle the raw windows messages at the application level. Here’s the source code for doing this:

      public partial class MainForm : Form

      {

            [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]

            public class TestMessageFilter : IMessageFilter

            {

                  const int WM_MOUSEMOVE = 0x200;

                  static int count = 0;

                  public bool PreFilterMessage(ref Message m)

                  {

                        // Blocks all the messages relating to the left mouse button.

                        if (m.Msg == WM_MOUSEMOVE)

                        {

                              System.Diagnostics.Trace.WriteLine("Mouse Moved " + (++count).ToString());

                              return true;

                        }

                        return false;

                  }

            }

 

            public MainForm()

            {

                  InitializeComponent();

                  Application.AddMessageFilter(new TestMessageFilter());

            }

 

The WM_MOUSEMOVE constant is from C/C++ programming and is part of the Win32 API. It’s declared in Winuser.h which can be found in the Windows SDK if you have Visual C++ installed.

Posted in Uncategorized | Leave a comment