Posts

Showing posts from February, 2011

C# Lock, Sleep, and Hibernate Windows

This could be a very uncommon occurrence but you might find a need in C# to set the computers sleep or hibernate state. You might even need to lock the entire pc. .NET has two of the needs built in, the third you will need to do some pinvoking. // set the computer to hibernate bool retVal = Application.SetSuspendState( PowerState.Hibernate, false, false ); if( retVal == false ) { MessageBox.Show( "Unable to hibernate the system." ); } // set the computer to suspended bool retVal = Application.SetSuspendState( PowerState.Suspend, false, false ); if( retVal == false ) { MessageBox.Show( "Unable to suspend the system." ); } // lock the workstation // we need to import from user32.dll, the LockWorkStation function [DllImport("user32.dll", SetLastError = true)] static extern bool LockWorkStation(); bool result = LockWorkStation(); if (result == false) { // TODO: an error occured } The Application.SetSuspendState is the big ...

MFC get your external ip address

I recently wrote a program in MFC that would monitor your ip address.  This was great but I soon realized if your behind a NAT of some kind you get your internal ip address.  I soon realized a need to know my external ip address.  There is no built in functionality to do this in windows so you have to ping an outside server or website that will tell you what your ip address is.  There are several ways to do this sockets, an http request, or urlmon.  Many people say sockets is the best since you can use any version of windows and create a socket.  The issue is the socket code is extremely long and complicated.  I only need to support windows 2000 and above so I decided using the urlmon way was much simpler and faster.  Here is some code in MFC to hit an outside website which returns your IP address in file that you then parse for the data. This is in a class with a CString member variable called m_strExternalIp. m_strExte...

Using google maps API for driving directions

Image
This is a bit different than my usual blog postings but I think it will help a lot of people.  I recently have found a need to generate a Google Map with driving directions based on some customer input.  After a few hours looking over some javascript API's I found a very simple solution on Google.com itself.  There are a few different versions of the API and this solution uses version 2.0.  I believe the current version is 3.0.  I have no clue when if ever they will deprecate the API but it could happen.  They also state if you call this service more than 2,500 times a day you will get banned so don't abuse the service.  My use is light at best maybe once or twice a day.  I have found it to be fairly fast and light weight. UPDATE!! Looks like Google wants you to have a key to use the maps. You will need to signup for a key here: http://code.google.com/apis/maps/signup.html It takes a few seconds and you will need to add the key to the que...

C# using a transaction with ODBC

Let's say your writing some data to the database and you need to make it transactional.  You know where you need to rollback multiple statements or use multiple statements in one go.  Normally you would do this in the database itself.  But I have found a few times where you have to do this in .net.  My latest was using mySQL to get the new index after I did an insert.  I found the SQL to do so would work fine in a mySQL command prompt or in the phpMyAdmin SQL pane but their .net ODBC driver simply wouldn't allow it.  Epic .net fail mySQL I guess your love for PHP has clouded your vision.  So you end up needing to wrap up multiple SQL calls in a transaction to accomplish this.  I have also had to do this it SQLSERVER but not on a scale like this.  But regardless this is how you can use a .net transaction with multiple SQL statements. You need to create an OdbcConnection.  Then an OdbcCommand and OdbcTransaction.  Set the command an...

C# Anti Aliasing when drawing

Image
In a previous post I talked about how to set anti-aliasing on text in C#. Now you may find yourself needing to set anti aliasing when drawing shapes. This is very easy to do using the GDI+ graphics object. The trick is to use the SmoothingMode and PixelOffsetMode . Now some quick code protected override void OnPaint( PaintEventArgs e ) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; e.Graphics.DrawEllipse( new Pen( new SolidBrush( Color.Red ), 1.0f ), new Rectangle( 5, 5, 200, 200 ) ); e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; e.Graphics.DrawEllipse( new Pen( new SolidBrush( Color.Red ), 1.0f ), new Rectangle( 210, 5, 200, 200 ) ); e.Graphics.SmoothingMode = SmoothingMode.HighSpeed; e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; e.Graphics.DrawEllipse( new Pen( new SolidBrush( Color.Red ),...