Friday, November 11, 2011

What makes Android Programming Simpler and iOS difficult?

ios-5-android-ice-cream-sandwich-apple-google

I cannot start writing iPhone and iPad software because?

I don't have an Apple... No! don't bring me a KG of fresh red apples please, I am referring to the Apple which is a computer. It's impossible to code stuff up without an Apple machine, even for sake of learning. Apple MUST create a tool chain for people running Windows as we know it's the most widely used operating system, just like the iPhone which is a crazily used "Somewhat Smart" Phone. They could create a development environment, and a few emulators to get new going with iOS development. I believe it would cost less than $100k to develop such a solution, maintenance cost won't go beyond this as well.

Friday, October 7, 2011

C# string utility functions containsAtLeastOneChar-containsOnlySpecifiedSpecChars-excludesSimilar

c-sharp-green

A utility class to perform simple operations on strings using C#. The logic is simple, one can transform the code given below to C++, PHP, or JavaScript within 2 minutes. I know each one of the programming languages I mentioned here, and all other first class modern programming languages have got sophisticated regular expression packages. But, the use of regex is not an option for everyone. Some got the skill to learn, but just don't have the time needed to read docs. Some don't want to import in a full blown namespace like System.Text.RegularExpressions
And some other are plain lazy.
Here goes the code, the names are descriptive enough, first one containsAtLeastOneChar  checks whether a string contains at least one character of a certain character set. For example, we want to check whether a password generated by a automatic password generator contains at least one capital letter, one number, and one special character.
Second one, containsOnlySpecifiedSpecChars returns true when the supplied string conains only specified characters.
Third function excludesSimilar will return true when a string does not contain repeating characters.
The class Commons contains static member variables, you could try passing these values to the static member functions of this class and experiment  around to check the results. It will also save you some typing :)
You could just copy paste the code given below as it is, it's supposed to work.
Happy coding!
public class Commons
{
public static bool containsAtLeastOneChar(string strWord, string strCompairTo)
{
 int nIdx = -1;
 foreach (char c in strWord)
 {
  nIdx = strCompairTo.IndexOf(c);
   if (nIdx >= 0) return true;
 }
 return false;
}
public static bool containsOnlySpecifiedSpecChars(string strWord, string strSpecialChars)
{
foreach (char c in strWord)
 {
  if ((strCaps.IndexOf(c) < 0) & (strSmalls.IndexOf(c) < 0) & (strNums.IndexOf(c) < 0))
 {
  if (strSpecialChars.IndexOf(c) < 0)
   return false;
  }
}
return true;
}
public static bool excludesSimilar(string strWord)
{
foreach (char c in strWord)
{
 if (strWord.IndexOf(c) != strWord.LastIndexOf(c))
  return false;
 }
 return true;
}

public static string strCaps = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
public static string strSmalls = "abcdefghijklmnopqrstuvwxyz";
public static string strNums = "0123456789";
}//class

Sunday, September 4, 2011

Reverse of a string by words C# example

string-reversal-example


Reverse of a string in C#, VB .Net, or C++ is pretty easy. Please not that we are talking about a word by word string reversal, for example


Hello How Are You
Becomes
You Are How Hello


Reversing characters inside a word is a different story, not covered here.
Some people find word order reversal difficult, so I'm putting the C# example code here.

Thursday, August 4, 2011

Nested conditional statement other than switch case in C#, C++, Java, PHP, JavaScript

nested-conditional-statement


Switch statements are nice, sometimes we need to just return some value based on an input variable. For example in our case I needed to get the key field of an LDAP object class we preferred to use in our software. So I had a simple C# function which went something like this


public static string getKeyByObjectClass(string strObjectClass){
switch(strObjectClass){
    case "user":
    return "samAccountName";
    case "user-mailbox":
    return "samAccountName";
    case "user-mail":
    return "samAccountName";
    case "contact":
    return "CN";
    case "contact-mail":
    return "CN";
    case "group":
    return "samAccountName";
    default:
    return "";
 }
}

I thought it is possible to simplify this switch statement with a nested conditional statement. And following was the result of some deliberation ;)








public static string getKeyByObjectClass(string strObjectClass){
              return (strObjectClass == "user") ? "samAccountName" : (strObjectClass == "user-mailbox") ?"samAccountName" :(strObjectClass == "user-mail") ? "samAccountName" : (strObjectClass == "contact") ? "CN" :(strObjectClass == "contact-mail") ? "CN" : (strObjectClass == "group") ? "samAccountName" : "";

You can definitely use || operator, minimize the code, and refine the crude version of nested conditional statement we created earlier, let's just do that

public static string getKeyByObjectClass(string strObjectClass){
              return (strObjectClass == "user" || strObjectClass == "user-mailbox" || strObjectClass == "user-mail") ? "samAccountName" :                                      (strObjectClass == "contact" || strObjectClass == "contact-mail" ) ? "CN" : "";
}

But now we definitely have some compromise on flexibility, maybe we'll want to use a different key for an object class say "user-mail" and then we'll have to modify this code in tiny bit different way(maintenance hell anyone?) ;)
I'm sure many intelligent folks have already done this or even a better variation but I also know that there are dumb guys and gals just like me out there who would most probably yell "Eurika!!!" on understanding the magic of nested conditional statements.
Its not just and only C# stuff, in fact the code will work with JavaScript, PHP, Java, C++ and many others with little or no variation.

Saturday, July 30, 2011

Cannot rename htaccess.txt to .htaccess in cPanel File Manager

Just today I was trying to fix the 404 broken link problem with the Joomla installation on my upcoming website, http://www.ldapfilters.com which will contain certain knowledge base about LDAP and directory servers in general, along with discussion forums.
So, they say WARNING! Apache users only! Rename htaccess.txt file to .htaccess before activating.
I opened the File Manager from CPanel, scrolled down to the htaccess.txt file right clicked and renamed it and the File Manager finally said "No! You can't rename, a file already exists." I tried and retried several gimmicks but it didn't work.
Solution: Then I decided to try legacy file explorer, I opened the CPanel Legacy File Manager, located htaccess.txt, clicked it, selected Rename from the menu, typed in .htaccess and it worked well ;)

A Confusion: I got confused and start thinking, shall I rename htaccess.txt to htaccess.htaccess or just remove the .txt from end of file name, and put a dot at the start of file name? Well the answer is yes, I had to take the second option. Therefore, just rename it to .htaccess that means a file with no name but just an extension.
And one more thing, I renamed htaccess.txt to htaccess_.txt and then tried to access my website, even the home page of my Joomla based website didn't show up let alone the broken links which I was trying to fix :p
Therefore, be very careful when you touch these configuration files, they are absolutely sensitive. Some times you catch the bug right after saving that file and refreshing that page but at some other time the setting you've just changed has got a much deeper effect which gets unleashed by some twisted user action sequence, so here's the GOLDEN ADVICE

Back those files up!!!

Monday, March 7, 2011

System.DirectoryServices A referral was returned from the server Exception

System.DirectoryServices showed " A referral was returned from the server " Exception just today, and it left me wondering what's wrong?
I was trying to pull data from a Microsoft Active Directory Group, and while attempting to query the data the aforementioned exception was occurring. An eagle eye debugging showed the cause and it was simple but stupid.

Following is the correct format of LDAP connection string

LDAP://abc.xyz.pqr.lab:389/CN=TheGroup,
CN=Users,DC=abc,DC=xyz,DC=pqr,DC=lab





But our dear programmer had done it a little differently, I think it was just a type, the wrong format of LDAP connection string was following

LDAP://abc.xyz.pqr.lab/CN=TheGroup,
CN=Users,DC=abc,DC=xyz,DC=pqr,DC=lab:389

This was causing the system to show the Active Directory related exception in C# .Net on Visual Studio 2008.









Notice the wrong positioning of LDAP port number, people some times make absolutely silly mistakes.

Keep in mind that 389 is default port for insecure communication with LDAP directory server. For secure exchange of data use LDAPS, default port for LDAPS is 636.

These port numbers are recommended, and LDAP servers use these out of the box. But not all LDAP servers always use port 389 and 636. It is possible to change the default LDAP port numbers, please check your directory server software's documentation for details.

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app.

https://www.odesk.com/users/~012d73aa92fad47188



Friday, February 18, 2011

Using Excel 2003 File As Data Source Of Visual Studio .Net Data Driven Unit or Functional Test

First of all this will be the attribute tag for setting up data access of a data driven Unit or Function test in Visual Studio, I am using Visual Studio 2008 but I think same will work in Visual Studio 2010

[DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\FunctionalTestsDataFiles\\Smoke_Cred.xls;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", "ldapdest$", DataAccessMethod.Sequential), TestMethod]

Put the name of your .xls file in place of "Smoke_Cred.xls" and put the name of your excel sheet in place of "ldapdest$", don't forget to put the $ sign after the name of your Excel sheet.

Be very careful with the "System.Data.OleDb" because the people at Microsoft have made a case sensitive comparison in their code which wasted so many of my hours, and gave me too much tension.
If you write "System.Data.OleDB" instead, it will fail in functional/unit test but it will work fine in other connection string testing utilities.


Second thing, the syntax of spread sheets inside excel files is different from syntax used for Access databases. You need to put a $ character after the name of the sheet you are trying to access.
I found an article on MSDN about running data driven test cases with Ms Access, but there was no mention of these little pieces of shit which I am sure must have been killing time of many poor developers.
Instead of solving problems, you have to run after crappy case sensitive and idiotic connection strings, what an irony!!!

If you use "System.Data.OleDB" mistakenly, following error will be displayed
Error details: Unable to find the requested .Net Framework Data Provider.  It may not be installed.

And if you somehow luckily fix this, and don't know that you need to put a $ sign after the name of that God damn excel sheet, you'll get following error

Error details: The Microsoft Jet database engine could not find the object 'ldapdest'.  Make sure the object exists and that you spell its name and the path name correctly.









One more twist, the TestContext used by some s of b in MSDN article is not found anywhere by default, and you need to define it before using it. I wonder what Microsoft's trying to do with us developers.

 private TestContext testContextInstance; 
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}

The link of the piece of crap MSDN article is following:
http://msdn.microsoft.com/en-us/library/ms182527.aspx

I know I'm using rash and bad language but what else do you expect from someone on 4:30 AM? When you come to know that only problem with your code was that you used "B" instead of "b" and you must be intelligent enough to put a $ in the end of table name of excel sheets here. I mean why in the world Microsoft idiots cant just fix these tiny issues??? I'm too much pissed right now... literally pissed...


RECOMMENDATION : Don't use Excel files as a data source for your dynamic tests, instead use .csv provider. It works well on both 32 and 64 bit machines. Excel will only increase the pain.

Saturday, January 22, 2011

Turn On or Off A Windows Computer's Display With SendMessage User32.dll Win32API Call In C++ or C#

How Can I Turn Off My Laptop's Display Whenever I Want?
Update: I have moved the .exe download from 4Shared to my own domain (http://www.ldapfilters.com), therefore the download is a one step process now.
I recently felt the need to turn off my laptop's monitor on my will, but you know there ain't such a button on almost all laptops. (Not interested in tech stuff? scroll down directly to Download, and get the executeable) So I tried to search around the internet for some utility, sadly speaking I didn't find any such thing. But you know I'm a programmer after all, therefore instead of searching a trust-worthy utility, I sought a do it yourself code example. I found the example here on code project: http://www.codeproject.com/KB/system/display_states.aspx The code needed to turn your monitor off/on is literally a one liner. All you need is one line, and your monitor will be powered off as soon as you run the .exe file. Here's the code friends:
 #include <Windows.h>
int main(int argc, char* argv[]) {         
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER,           (LPARAM) 2);// this c++ code will turn the monitor off
return 0;
}

You will be wondering, how the monitor/display will be turned back on?








The good news is Windows handles that part automatically. Whenever we turn off display with this cut throat application of ours, moving mouse or pressing a key will bring the monitor back to life. Although broadcasting another message will fire the display up, but we don't need that I think. Still, here's the code:
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 1); Take this piece of code to any C++ IDE of your choice(of course Windows duffer!!!), just compile build and your very own application to turn off the monitor will be ready. Place the executable anywhere you want, create a shortcut on desktop, whenever you'll click it the monitor will go dark. And if you don't wanna do that, just download the ready to run executable of this cut throat advanced bleeding edge top class wonderful awesome @#%$#%&~!^&%$#@+ app that can shut down your PC or Laptop's display/monitor whenever you want here !!!
Instructions:
  1. Just place the .exe file anywhere in your system, double clicking it will turn off the monitor you may create a shortcut on desktop, or pin it to task bar for easy access (details below).
  2. Moving the mouse, or pressing a key on keyboard will turn your system's display back on.
Please keep that in mind:
  • This utility does not open any network communications port
  • This utility does not read/write any kind of files to/from your computer's hard disk
  • This utility does not read/write anything to/from Windows Registry.
Therefore, if the file you download tries to do any of the above, the file is not original and someone has tampered with it. Making Things Even Better (Value Addition): There's a very cool Windows 7 feature which I've used to make things even better on my laptop. The feature goes like you can access the shortcuts pinned to Windows by pressing Windows Button + a number. The leftmost minimized program or pinned shortcut will be activated by pressing Windows + 1, second left with Windows + 2, so on and so forth. For example see the snap shot of my current Windows Toolbar
desktop_snapshot_Turn_Off_Display_Monitor
The yellow numbers aren't found originally in the interface, I've annotated the snap shot on purpose. Right now, if I will press Windows Key and then press 2 key(not from num-pad) the Windows Media player which is second from left will be maximized. Similarly, Windows 5 will cause Google Chrome to open up. Windows 8 will maximize Ms Paint. I have pinned the .exe file of our cut throat power off display application to Task bar. Therefore, if I will press Windows key + 1 display of my laptop will be turned off. Technical Explanation: How Windows Works Basically we need to understand the architecture of Windows applications to get the core of what's happening in this single line of code. In a Windows environment, we don't interact with any application directly. Instead whenever we perform an action we are talking to Windows Operating System, the OS takes our command/action and passes it over to an application depending on the location of the action/command/event.  For Example: Like if you click in a Ms Word window, following will happen:
  1. Windows OS will catch the message 
  2. It will see that currently the window associated with Ms Word was active
  3. It will pass over your command in form of a message to Ms Word
  4. Each and every Windows based application contains a "Windows Message Message Pump" loop, which keeps watching for incoming messages from Windows OS
  5. In this case, the message pump inside Ms Word will catch the message that "user clicked somewhere".
  6. Then Ms Word will handle the message what ever way it wants to.
  7. If an application has not handled a certain message, the message will simply have no affect on its health and the application will just discard the message.
People writing code for .Net platform (usually) don't know these things, but that's the basic Ms Windows architecture, and its not going anywhere any time soon. The "Windows Message Pump" plus WndProc method is alway in there. Where does SendMessage() fit in? SendMessage is found in User32.dll, and its a Win32 API call(scary bare bone stuff for .Netters). In this program we are broadcasting a message, which is not intended for any specific application. Like the fishermen broadcast help requests on radio. We are just saying "Hey anybody out there, I am sending  a WM_SYSCOMMAND which is related to SC_MONITORPOWER with a value of 2". Luckily there is a portion of Windows which is handling this message, its the same part which turns your display off after a certain amount of time. This function call won't affect anything else on your system, cause no other application is consuming this message. So, in the end, your display is powered off so that you can sit back and relax ;)

You might have noticed that I just provided C++ source code, and there ain't not C# .Net code as sample. I think its quite easy to setup p/Invoke calls these days, I am intentionally avoiding the topic as I want to keep the article short and to the point. Just if you've understood the scheme of things, you can easily setup a C#.Net or VB.Net version.

DISCLAIMER: There is no guarantee or warantee that this program will work as specified in this post. Author will not be responsible for any damage due to use of instructions provided in this post.

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app.

https://www.odesk.com/users/~012d73aa92fad47188