Monday, 14 May 2012

Using pointers in C#

Due to the implementation of CLR, using pointers in .NET is very different from C/C++, in the .NET environment you are supposed to run managed code, although you can rum unmanaged code CLR doesn’t like it very much (read this), so if a programmer wants to run some unmanaged code it must inform CLR of its intensions, this way CLR will know that it is not an error, for this just use the reserved word “unsafe” and the library System.Runtime.InteropServices. This is an example of how to access memory positions using a reference type Point:

using System;
using System.Runtime.InteropServices;

class Point 
{
 public int x, y;
 public Point(int x, int y) 
 {
  this.x=x; this.y=y;
 }
 
 public override String ToString() 
 {
  return String.Format("({0},{1})", x, y);
 }
}

class Program 
{
 unsafe private static void printAdresses(Point p1, Point p2)
 {
  // Printing Point1 addresses
  fixed(int* p1_Xpointer = &p1.x)
  {
   IntPtr addr = (IntPtr)p1_Xpointer;
   Console.WriteLine("Point1 X address:" + addr.ToString("x"));
  }
  
  fixed(int* p1_Ypointer = &p1.y)
  {
   IntPtr addr = (IntPtr)p1_Ypointer;
   Console.WriteLine("Point1 Y address:" + addr.ToString("x"));
  }
  
  // Printing Point2 addresses
  fixed(int* p2_Xpointer = &p2.x)
  {
   IntPtr addr = (IntPtr)p2_Xpointer;
   Console.WriteLine("Point2 X address:" + addr.ToString("x"));
  }
  
  fixed(int* p2_Ypointer = &p2.y)
  {
   IntPtr addr = (IntPtr)p2_Ypointer;
   Console.WriteLine("Point2 Y address:" + addr.ToString("x"));
  }
 }
 
 
 static void Main(string[] args)
 {
  // Creating two reference types of Point, p1 and p2,
  Point p1 = new Point(1, 2), p2 = new Point(3, 4);
  Console.WriteLine("Point1:{0}\nPoint2:{1}\n", p1,p2);
  printAdresses(p1, p2);
  Console.WriteLine("p1 and p2 point to diferent addresses\n");
  // pointing p2 to p1, addresses should be the same now
  p2 = p1;
  printAdresses(p1, p2);
  Console.WriteLine("p1 and p2 point to the same address, the old p2 is now gone\n");
 }
}
}

To compile this source code use "csc /unsafe FILENAME.cs"

Wednesday, 28 March 2012

Notepad++ 6


The new Notepad++ has arrived with new features:
1. Support for PCRE (Perl Compatible Regular Expressions).
2. Add Document Map feature(better "navigation" on big text files).
3. Enhance the loading performance for the large file.
Download available here

Friday, 17 February 2012

The Clean Coder


Just finished reading this great book by Robert C. Martin (or Uncle Bob) and it really is “A Code of Conduct for Professional Programmers”, it is that and so much more, Mr. Martin takes us on a trip round memory lane back to when he was a young programmer, and talks about some mistakes he made (so that we don’t do the same, kudos on that). Every chapter has a personal episode, some of them are really funny and all of them have something to teach, which is good since we like to learn. Throughout the book he focuses on topics like Unit Testing, Test Driven Development, Xtreme Programming, and how programmers could benefit from these techniques. This book is really great if you’re just giving your first steps on programming and want to be inspired and build a solid knowledge base as the book gives many tips on what and where to learn.

Martin, Rovert C. The Clean Coder: A Code of Conduct for Professional Programmers. Prentice Hall 2011
ISBN-10: 0137081073
ISBN-13: 978-0137081073

Friday, 20 January 2012

Integrating Microsoft JDBC Driver, MSSQL Server 2008 and Eclipse on Windows XP/7


Install and configure the JDBC Driver


Download Microsoft JDBC Driver for SQL Server from here (I used V2.0 in XP and V3.0 in win7)
Unpack the driver to c:\Program Files\ Microsoft SQL Server JDBC Driver 2.0 (or 3.0 depending on your version)
Add the driver path to Windows PATH this procedure is the same in XP and in Win7, do this “rightclick” on “My Computer” and select “Properties” this opens the “System Properties” window and select the “Advanced” tab




Click on “Environment Variables” (bottom of the window)



Select the “PATH” variable and add this to the end of the line “;c:\Program Files\ Microsoft SQL Server JDBC Driver 2.0\sqljdbc_2.0\enu\sqljdbc.jar” without the quotation marks but with the semicolon at the beguinnig, once again if you use V3.0 be sure to replace it accordingly.


Configure MS SQL Server 2008


Have an instance of MSSQL Server 2008 up and running (if you don’t know how read this), next, make sure your SQL Server as TCP-IP turned on, to do this go to the “SQL Server Configuration Manager”



Expand “SQL Server Network Configuration” and click on “Protocols for MSSQLSERVER” then on the right panel rightclick on TCP-IP and then “Enable”, this will allow for eclipse to access your SQL server through the MS SQL Driver.



After this you need to restart your SQL Server service like so:



Now it’s time to configure the user login that will be used to access the database, i created a test user for this (if you don’t know how to create a user go here), to configure a login expand your server instance, then “Security” and then “Logins”, locate your user login rightclick it and select “Properties”



On the “Login Properties” window (on the upper left corner) select “User Mapping” then choose which Databases your user will need to have access to (mine was SI_TP1) then select the “Database role membership” and be sure to select “db_datareader” and “db_datawriter”.



Use Eclipse to access your Database


Create a project and a class file that will be used to access your DB (if you don’t know how go here), next rightclick on your project in the “Package Explorer” go to “Build Path” and chose “Add Libraries”



In the “Add Library” window select “User Library” and click next



Next select “User Libraries…”



Click on “New…”



Give your Library a name like “MS_JDBC_2”



Then click on “ADD JARs..”



Navigate to your “c:\Program Files\ Microsoft SQL Server JDBC Driver 2.0\sqljdbc_2.0\enu\” folder and chose sqljdbc.jar file if your using XP or sqljdbc4.jar if your using Win7.
Click OK and return to Eclipse.


To access your database from eclipse use the following code:
SERVER_NAME = your server name
DB_NAME = the database you want do access
LOGIN = the login of your user
*** = that user password
SOME_TABLE = name of table in database
SOME_METHOD = ResultSet methods



import java.sql.*;
public class JDBCTest
{
public static void main(String[] args)
{
try
{
//Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionURL = "jdbc:sqlserver://SERVER_NAME;" +
"databaseName=DB_NAME;user=LOGIN;password=***;";
Connection con = DriverManager.getConnection(connectionURL);

//ENTER SQL STATMENTS HERE
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM SOME_TABLE");

while(rs.next())
{
System.out.print(rs.SOME_METHOD);
}

con.close();
}
catch(SQLException se)
{ System.out.println(se.getMessage()+"**"+se.getSQLState()+"**"+se.getErrorCode());
}

catch(Exception e)
{
e.printStackTrace();
}
}
}

If there is anything you found unclear or if you found an error please leave a comment.

Thursday, 5 January 2012

2012 – The year HTML5 wins

The fifth version of HTML gained much notoriety during 2011 by transforming the application development paradigm. HTML5 took the world by storm with the premise of being multi-platform, and giants like Microsoft, Google or Apple have its back.

Mobile devices are here to stay, every day more and more people buy them, and demand more and better apps and functionalities, hopping to be the next to come out with the next big, thing each manufacturer has created its own technological mushroom, this caused serious problems to content developers, because an app developed to run on iPhone doesn’t run on Android or Windows Phone and vice versa, but in the course of 2011 some big players changed their minds and now the path seems brighter, Apple banned Flash from their mobile devices and states that HTML5 is the future(1), Microsoft is revolutionizing its core, no more Win32 API, from now on its WinRT (Windows RunTime(2)) API and it has HTML5 and JavaScript in mind, another big step towards HTML5 was Microsoft dropping out of SilverLight, this is clearly a bet to try and catch the mobile train and it shows with Windows Mobile 7 and Metro style apps, Adobe as also recently stated that its dropping the ball on Flash(3).

HTML5 promises standardization and flexibility, it’s this ability to run on several platforms that is going to boost the standard.


Several companies are already making available some amazing content developed in HTML5, like Rovio who released AngryBirds for Chrome, or SuperGiants who released Bastion also available on Chrome Store.




1. http://www.apple.com/html5/
2. http://msdn.microsoft.com/en-us/library/windows/apps/hh464942.aspx
3. http://news.yahoo.com/adobe-drops-flash-mobile-devices-193154292.html