Wednesday 11 December 2013

Use Mintty with Git-Bash

Mintty is a great tool as terminal for bash shell on windows. Git for windows by default has a special bash installed. Use Mintty for Git can make you get "intellisense" for git commands while having benefits of a modern terminal window.


Step 1: Download Mintty
Step 2: Unzip Mintty into git's installation directory, e.g. C:\Program Files\Git\bin
Step 3: Create a shortcut like:
"C:\Program Files (x86)\Git\bin\mintty.exe" /bin/bash --login -i

Step 4: Run the shortcut, then create a folder for Mintty to save it's config file (.minttyrc)
mkdir /home/$LOGNAME/test

MinGW + MSYS + Mintty = Light Unix Env on Windows :)

This is a good choice for those who just want to use Bash Shell on windows but reluctant to install the huge cygwin.
Step 1:  Install MinGW and MSYS. You can follow the wiki here.
Step 2:  Download mintty.
Step 3: Unzip mintty to MSYS’s bin folder. Usually it is
C:\MinGW\msys\1.0\bin       or
C:\msys\1.0\bin
Step 5. Create a shortcut with command like
C:\MinGW\msys\1.0\bin\mintty.exe /bin/bash --login -i
Then you can enjoy the mintty with bash shell.
Note: To make bash load your .bashrc or .bashrc_profile so you can customize your environment, you need to add following code to /etc/profile :

Then create a file e.g.~/ .bashrc and add anything you like. Ibasically  like to put in

Saturday 7 December 2013

PHP to use .Net (C#) Assembly (Complete & Simple Tutorial)

After reading this section of PHP document  I thought interaction between PHP and .Net would be just a piece of cake, but actually it depends. It took me considerable time to get it work, eventually, on my Win7 64-bit and VS2010. So this tutorial came up with detailed steps and error handling experiences.
Basically there are two classes to help use a .Net dll in PHP:
  • DOTNET
  • COM
Actually they both use COM interoperability, I cannot see big difference except the latter can use .Net 4 assembly.
So here we go…

Prerequisite

You need PHP 5.5.6 installed on windows. There are versions for X86 and X64, whichever you like. And a tool to create C# library -  I have Visual Studio 2010 installed.

How to use DOTNET class

1. Create a new project as class library in VS2010.



Here I set the project name as RegPhpDemo

2. Make your assembly as COM component

Go to properties of your project. Make it COM visible.



Warning: Make sure your Target .Net framework is NOT .Net 4!!

This is very important. Use .Net 2 or 3.5!  
Currently PHP’s DOTNET class can only find the assembly in
%windir%\assemly\
However, from .Net 4 Microsoft changed GAC directory to
%windir%\Microsoft.NET\assembly\GAC_<PlatformTarget>\
Where PHP wouldn’t load from!

3. Write any public class, here I created a new class file

4. Install your DLL into GAC

Go to your project’s properties then choose signing tab.
You can create a new key file or choose an existing one, whatever you like.
Then build the project, locate the dll afterwards, and run command to install it into GAC:
> gacutil /f -i RegPhpDemo.dll
Here /f simply forces the dll to be reinstalled.

5. Create PHP script to Test

Please note detailed information of your DLL must be provided, including Name, Version, Culture and PublicKeyToken.
Hint: Getting the stupidly long string can be an easy job for gacutil.exe:
>gacutil -l RegPhpDemo
The output would have something like following.


So just copy the string into code.
Finally run above script. We get


How to use COM class



This is an alternative to DOTNET class. The benefit is .Net 4 is available again!

Build DLL

Just follow Step from 1 to 4 in DOTNET class example above, but set the target platform to .Net 4 (.Net 2 and .Net 3.5 are fine of course but her ewe use .Net 4 as the bonus).

Register DLL

Build your project and register the DLL as COM component:
>regasm RegPhpDemo.dll
Warning: X86 or X64 means different! On windows X64, you may have multiple versions of regasm.exe. When register your COM component, they can inject information into individual entries. For 32bit regasm, it writes into Wow6432Node section in registry while 64bit regasm won’t. PHP x86 will be searching Wow6432Node while x64 will act as usual.
To keep it simple, just follow the table below.
PHP
Regasm to be used
64bit
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm
32bit
%windir%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe

Create an new PHP script using COM class

Then run it to get output same as before!

Summary

DOTNET class

Pros

·         The DLL can be used by either PHP X86 or X64 version on Windows at same time.

Cons

·      Cannot use .Net 4 as your DLL’s target framework.


COM class

Pros

·         DLL can use .Net 4

Cons


·      PHP X86 and X64 version both needs registration.

Thursday 5 December 2013

Python vs Ruby Simple Code (1)

Playing with Python or Ruby is fun, compared to many other strongly type language like C++, C# or Java.

This is just compare the two language in very details as sort of note for myself.

Lemme start from the very basic one:
print
In Python, print is just a function, defined with default separator(' '), end-line(\n) and stream(sys.stdout). To print something to console,

In Python, I write

In Ruby, I write

Though Ruby is more flexible, I prefer the way python coding. Ruby is hiring two special character: $, and $\ to represent separator (sep= in Py) and append (end= in Py)

Ruby has puts which is being more widely used same as python's print (with sep param set as '\n'). However python seems simpler and more clear.

Wednesday 4 December 2013

A really simple observable dictionary

In my current WPF project, people are widely using observable container like ObservableCollection<T>. The other day someone whinged why there is no observable dictionary like ObservableDictionary<T>??

All right, I googled and then found an implementation. It greatly implemented so many interfaces...oh!! I even don't notice some of them when I was using containers! 

As a lazy and stupid man, I tailored it and worked out a simple version by inheriting dictionary directly. Here coming code.