Programmatically send keystrokes to active current window in Windows



Here is the code to send keys (key strokes) to active current application in Windows using C++/Win API.

void SendKey(WORD wVkey)
{
	INPUT inputs[2];
	inputs[0].type = inputs[1].type = INPUT_KEYBOARD;
	inputs[0].ki.dwExtraInfo = inputs[1].ki.dwExtraInfo = 0;
	inputs[0].ki.wVk = inputs[1].ki.wVk = wVkey;
	inputs[0].ki.dwFlags = 0;
	inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;
 
	SendInput(2, inputs, sizeof(INPUT));
}


Get size (rectangle) of complete virtual screen (all display monitors together)



Here is the code to get the complete virtual screen rectangle that includes all display monitors. Code is in C++/MFC.

//gets the coordinates of the complete virtual screen which is includes all the monitors present
CRect GetVirtualScreenRect()
{
	CRect rcVirtualScreen;
	rcVirtualScreen.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
	rcVirtualScreen.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
	rcVirtualScreen.right = rcVirtualScreen.left + GetSystemMetrics(SM_CXVIRTUALSCREEN);
	rcVirtualScreen.bottom = rcVirtualScreen.top + GetSystemMetrics(SM_CYVIRTUALSCREEN);
 
	return rcVirtualScreen;
}


Check if email address is valid in C++/MFC



 
Here is the C++/MFC code to check if an email address is valid. 
 
bool IsEmailAddressValid(CString emailAddress)
{
	emailAddress.Trim();
 
	if (emailAddress.IsEmpty()) return false;
 
	int idx;
	CString rightPart;
 
	idx = emailAddress.Find(_T('@'));
	if (idx == -1) return false;
 
	//is the first character @?
	if (idx == 0) return false;
 
	rightPart = emailAddress.Right(emailAddress.GetLength() - idx - 1);
 
	idx = rightPart.Find(_T('.'));
	if (idx == -1) return false;
 
	//character immediately after @ is .?
	if (idx == 0) return false;
 
	//is there only period?
	if (rightPart.GetLength() == 1) return false;
 
	///is the last character period and no characters after that?
	if (rightPart.GetLength() - 1 == idx) return false;
 
	return true;
 
}


Convert array/list to string using a separator in C++ (MFC)



Following is the code to convert array to string using a specific separator. Language used is C++ using MFC (Microsoft Foundation Classes).

This function can trim each term in the array and also ignore blank (empty) string.

You can easily modify it for list or any other similar collection class.

void ArrayToString(CAtlArray<CString> & arrValue, CString & strValue, CString strSeparator
					, BOOL bTrim/*=TRUE*/, BOOL bIgnoreEmptyString/*=TRUE*/)
{
	int i;
	strValue.Empty();
	CString strItem;
 
	//append all strings with ; as separator
	for(i=0;i<int(arrValue.GetCount());i++)
	{
		strItem = arrValue[i];
 
		if (bTrim)
		{
			strItem.Trim();
		}
 
		if (bIgnoreEmptyString)
		{
			if (strItem.IsEmpty())
			{
				continue;
			}
		}
 
		strValue += strItem;
 
 
		if (i < (int(arrValue.GetCount())-1))
		{
			strValue += strSeparator;
		}
 
	}
 
}


Looking for Sqlite Net PCL for your Xamarin project?



The phone app for Notezilla is created using Xamarin Forms. I like the way Xamarin Forms has grown. It works pretty well.

If you are looking for SQlite .Net PCL library, you might find multiple. But I will tell you which one is good.

For last few years I had used Sqlite.Net PCL and Sqlite-Net Extensions in my app. While I was debugging performance issue in the iOS version of my app, I found that the performance lag was due to a single linq query. It used to take 7 seconds. Xamarin Forms list view which listed our records was performing too slow. This was the case only in iOS. Not in Android.

So I looked for an alternative. And finally found the original Sqlite-Net-PCL project that is being maintained till date. I implemented Sqlite-Net-PCL and found that the same linq query executed in less than 1 second. This was a great deal for me.

In short, use Sqlite-Net-PCL. Do not use Sqlite.Net PCL because it is slow and it is not being maintained.

Sqlite-Net Extensions is dependent on Sqlite.Net PCL. Fortunately, we weren’t using Sqlite-Net Extensions. So we could easily remove it from our project.

Thanks.



XLabs Xamarin Forms Labs – Label does not un-strikethrough in Android



If you are trying to use un-strike-through (remove strike through) a text in extended label of XLabs Xamarin-Forms-Labs , you may not see it working. This is a bug.

To fix it, I have added to else part in both in ExtendedLabelRenderer.cs of Android platform. This fixes it.

  if (view.IsUnderline)
        {
            control.PaintFlags = control.PaintFlags | PaintFlags.UnderlineText;
        }else
        {
            control.PaintFlags &= ~PaintFlags.UnderlineText;
 
        }
 
        if (view.IsStrikeThrough)
        {
            control.PaintFlags = control.PaintFlags | PaintFlags.StrikeThruText;
        }else
        {
            control.PaintFlags &= ~PaintFlags.StrikeThruText;
        }

Thanks.



XLabs Xamarin Forms Labs – Label does not show strikethrough on multi-line in iOS



If you are trying to use strike through property of XLabs Xamarin-Forms-Labs , you may not see the strike through work on labels having multiline (wrapped text). This is a bug.

To fix it, add the following line after setting this.Control.AttributedText = new NSMutableAttributedString in the file ExtendedLabelRendered.cs

 ((NSMutableAttributedString)this.Control.AttributedText).AddAttribute (
                UIStringAttributeKey.BaselineOffset, new NSNumber(0) , new NSRange (0, view.Text.Length));

Thanks.



Splitting CString to array in MFC



The following function converts string to array in MFC & ATL. By default it trims each string in the array and also ignores empty strings. It uses CString MFC class and CAtlArray ATL class.

//converts semicolon separated string to array of strings
void StringToArray(CString & strValue, CAtlArray<CString> & arrValue, TCHAR szSeparator, BOOL bTrim/*=TRUE*/, BOOL bIgnoreEmptyString/*=TRUE*/)
{
	int idx;
	CString strTemp,strRest;
 
	strRest = strValue;
 
	if (strRest.IsEmpty()) {
		arrValue.RemoveAll();
		return;
	}
	do
	{
		idx = strRest.Find(szSeparator);
 
		if (idx != -1)
		{
			//take the left part
			strTemp = strRest.Left(idx);
 
			if (bTrim)
			{
				strTemp.Trim();
			}
 
			if ( (bIgnoreEmptyString) && strTemp.IsEmpty() )
			{
				//ignore
			}else
			{
				arrValue.Add(strTemp);
			}
 
			strRest = strRest.Right(strRest.GetLength()-idx-1);
 
		}else
		{
			if (bTrim)
			{
				strRest.Trim();
			}
 
			if (!strRest.IsEmpty())
			{
				//this is the last element, so lets add it
				arrValue.Add(strRest);
			}
		}
 
	}while(idx != -1);
 
}


PositiveSSL, Azure & COMODO RSA Certification Authority



Recently SSL 1 was declared unsafe. So the certification companies started supplying SSL 2 certificates.

My certificate for notezilla.net was about to expire so I purchased a new one from namecheap.com. It sells SSL certificate at very less price. The brand name is PositiveSSL.

After purchase, I followed the instructions to install SSL certificate on my Windows Azure cloud service. But it didn’t work. I tried about 4 times and spent several hours trying to fix it.

As a side note: SSL 2 has a different set of root and intermediate certificates. I had to re-download and install them from here.

Surprisingly, for first 2 days I found everything was working fine until I tried to access my web-app from Android phone. The Chrome browser showed a scary message – ‘Your connection is not private’. Then I checked my website using SSL Checker. It said that the certificate was broken. The chain did not complete to the root. Also my Android app created using Xamarin were throwing following exceptions:

{System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure

System.Net.WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure —> System.IO.IOException: The authentication or decryption has failed. —> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server. Error code: 0xffffffff800b010a

Fortunately, Boyan Tabakob’s post on this thread helped me.

I am further elaborating on this because I want to cover issue specific to the PositiveSSL certificate purchased from namecheap.com. Nothing is wrong with the certificate. But the way IIS determines the chain of certificates is incorrect.

Here is how your chain should look like:

Notezilla.Net certificate chain

Your certificate->COMODO RSA Domain Validation Secure Server CA->COMODO RSA Certification Authority->UserTrust (AddTrust External CA Root)

However, when you install the SSL certificate on Windows Azure, and log in to your cloud service using Remote Desktop to check the chain, it will look totally different. It will pick a different COMODO certificate and will not find your actual root certificate.

Problem: The problem is that IIS has another interm certificate named “COMODO RSA Certification Authority” under “Trusted Root Certification Authorities”. This one is with a different Thumbprint. The real “COMODO RSA Certification Authority” which I wanted was already in “Intermediate Certifications Authorities” folder.

Solution: Since IIS picked the former one from the root (may be because of the same name), I deleted the former one (via Remote Desktop) and the certificate chain was now complete till the root. Now my website was working fine. Remember to restart your VM instance (Web Role) after you make this change! Very important.

The bad part is that I will have to repeat this step every time deploy a new build of my MVC cloud service.

Thanks. Hope it helps someone :).