Auto-Java-Updater 1.0 for Windows is Published!

I don’t really have a good impression on Oracle product, though it is truth that their product perform well, they are just less user orientated.

So this time, I am having trouble with updating Java Runtime Environment, or JRE in short. It has been a long problem. I got several desktops in my company which require JRE for their work. But after a month or two, the users will complaint to me that they cannot use their application and show me a screenshot which is actually a pop up saying JRE is out of date.

Oracle never provided an automatic update mechanism for JRE, and because the user doesn’t have administrator right, they can’t perform their installation on their on. At the end of the day, I have to update the JRE on their desktop, one by one.

So finally today I am looking for solution to the problem, and found this, a batch script to update the Java. I tried, and found a few bugs and issues. So I decided to fork it and create my own one, which is the birth of my own Auto-Java-Updater.

You may visit the project page here or download the script at the project release page here.

Anyway, my day has gone to make this script, and hopefully saving me the trouble onward until JRE 8 is obsoleted.

Advertisements

Visual Studio Code Zip / Portable Version for Windows

Recently I was given an exercise to work on Node.js. A few years ago I was using Aptana Studio on Node.js. So after these few years I guess there should have some IDE or editor available for Node.js. So I ended up found Visual Studio Code and Atom.

I tried both Visual Studio Code and Atom, and I choose Visual Studio Code at this moment because the autocomplete suggest is much more better than Atom.

For software that I use, I usually prefer portable version. I found someone has made one, but I don’t like unofficial release. I looked again and lucky found a post here which mentioned that there is a portable version of Visual Studio Code.

After further checking, it is actually a zip version and officially the download link is located deep inside the FAQ section of Visual Studio Code.

Anyway for those who like no installer version, below is the link to download it directly.

http://code.visualstudio.com/docs?dv=winzip

Edit 2016-08-13: Finally the they have provided the Zip archive download link in their download page.

Error 1918 when Installing MySQL ODBC Connector 5.3.6 on Windows

It is somewhat a frustration that even the official website at the time of writing this doesn’t correctly list out the dependency software.

Anyway in short, make sure you have installed Visual C++ Redistributable Packages for Visual Studio 2013 if you are installing MySQL ODBC Connector 5.3.6.

The official website and many other website I found only mentioned to install Microsoft Visual C++ 2010 Redistributable Package. Some websites even gave an inappropriate installation instruction to user. Although Microsoft Visual C++ 2010 Redistributable Package might be required for older version of ODBC connector, but definitely not version 5.3.6.

Also, it is better if you are installing the latest version of Visual C++ as well. Many website I found (such as this, this, this and this) are still providing link to the old version of Visual C++ even a new version is already available at the time of their writing. You may check the latest version of Visual C++ at Microsoft website here but, even the official website cannot be trusted as I can find a more updated version of Visual C++ 2015 here at the time of writing this article…

In summary, trust yourself.

Reading UTF File with BOM to UTF-8 Encoded std::string in C++11 on Windows

I got a task from boss and during the task I need to write a C++ function to read a text file into UTF-8 encoded string. I can’t believe this easy task in high level language like VB.NET cost me 2 days to figure the solution out. Some solutions use libraries like UTF8-CPP or ICU, while some use Windows API. These ways works but I don’t quite like them because:

  1. Usability, some solutions work good for most characters but failed to handle non-BMP characters
  2. Portability, we are foreseeing our code will be migrated to Linux, use of Windows API means adding work to our migration work in the future
  3. Dependency, I don’t want to depend on third-party library, especially the C++ function later will integrated with the program which only depend on very few third-party library.

Surprisingly there is not much solution that I am satisfied from the Internet, but good that at the end I still able to figure the code out. It still used one Windows API function though, but it is much easier to port to Linux already than ten functions. Anyway here it is:

// Reading ASCII, UTF-8, UTF-16LE, UTF-16BE with auto BOM detection using C++11 on Windows platform
// Code tested on Microsoft Visual Studio 2013 on Windows 7
// Part of the code is referencing http://cfc.kizzx2.com/index.php/reading-a-unicode-utf16-file-in-windows-c/

#include <stdio.h>
#include <tchar.h>
#include <string>
#include <fstream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <iostream>
#include <io.h>
#include <fcntl.h>

#define TEXT_FILE_PATH      "D:\\test.txt"
#define ENCODING_ASCII      0
#define ENCODING_UTF8       1
#define ENCODING_UTF16LE    2
#define ENCODING_UTF16BE    3

std::string readFile(std::string path)
{
	std::string result;
	std::ifstream ifs(path.c_str(), std::ios::binary);
	std::stringstream ss;
	int encoding = ENCODING_ASCII;

	if (!ifs.is_open()) {
		// Unable to read file
		result.clear();
		return result;
	}
	else if (ifs.eof()) {
		result.clear();
	}
	else {
		int ch1 = ifs.get();
		int ch2 = ifs.get();
		if (ch1 == 0xff && ch2 == 0xfe) {
			// The file contains UTF-16LE BOM
			encoding = ENCODING_UTF16LE;
		}
		else if (ch1 == 0xfe && ch2 == 0xff) {
			// The file contains UTF-16BE BOM
			encoding = ENCODING_UTF16BE;
		}
		else {
			int ch3 = ifs.get();
			if (ch1 == 0xef && ch2 == 0xbb && ch3 == 0xbf) {
				// The file contains UTF-8 BOM
				encoding = ENCODING_UTF8;
			}
			else {
				// The file does not have BOM
				encoding = ENCODING_ASCII;
				ifs.seekg(0);
			}
		}
	}
	ss << ifs.rdbuf() << '';
	if (encoding == ENCODING_UTF16LE) {
		std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utfconv;
		result = utfconv.to_bytes(std::wstring((wchar_t *)ss.str().c_str()));
	}
	else if (encoding == ENCODING_UTF16BE) {
		std::string src = ss.str();
		std::string dst = src;
		// Using Windows API
		_swab(&src[0u], &dst[0u], src.size() + 1);
		std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utfconv;
		result = utfconv.to_bytes(std::wstring((wchar_t *)dst.c_str()));
	}
	else if (encoding == ENCODING_UTF8) {
		result = ss.str();
	}
	else {
		result = ss.str();
	}
	return result;
}

You can also find the above code at https://gist.github.com/VeryCrazyDog/c20b2cb83896e9975d22

Building Ncat Portable for Windows

Background

I used to use telnet for testing if the firewall working properly in the office. Telnet is good for simple test, but when comes to testing a large number of destination IP and ports it will be very time consuming. After searching on the Internet I found Netcat, a tool for testing connection. It is build-in in most Linux OS but not on Windows. So I found Ncat here as part of the Nmap for Windows by nmap.org, but too bad that it won’t execute on some Windows as it requires Microsoft Visual C++ Redistributable Package installed.

A Ncat portable is an alternative, but the website only provided a beta version of Ncat 5.59BETA1, and require you to compile your own if you would like a newer version.

They did provide a documentation on how to compile Ncat with static linking library, but if you follow the steps you will encounter a few problems which you have to troubleshoot yourselves.

So here is my steps on compiling Ncat portable after I had gone through all the trouble.If you are someone who can edit the documentation on secwiki.org, please do that for me as I don’t want to get an extra account for just editing the document :)

If you want to avoid the trouble to compile, you can download the Ncat which I compiled here, however use it at your own risk as I might have inject some malware inside :)

Building Ncat Portable for Windows

Environment

The following steps has been tested using the following environment:

Steps

  1. First, follow all steps on the original documentation at https://secwiki.org/w/Nmap/Ncat_Portable till you copied the resulted static libs and include files from “C:\OpenSSL” to the “mswin32\OpenSSL” directory in the Nmap source tree in step 5. Below is a screenshot of the original documentation in case it is edited at the time you read this article.
    Building Ncat Portable
  2. Assuming that everything went fine till now, you’re about 7 steps away from building Ncat portable.
    1. Open Nmap solution in Visual Studio from mswin32\nmap.sln and switch the build configuration to “Ncat Static” like so:
      1. Right click on Solution “nmap” in the Solution Explorer sidebar and choose “Configuration Manager“.
      2. Switch the active solution configuration to “Ncat Static“. Check the “Build” check box for project “liblua“. Make sure that the nsock, nbase and ncat projects have switched to the “Static” configuration also. Then close the “Configuration Manager”.
      3. Right click on the ncat project and select “Set as StartUp Project“.
    2. Right click on the “nsock” project in Visual Studio and click “Properties“. In “Configuration Properties” > “General” > “C/C++” > “General“, in “Additional Include Directories“, add path “..\mswin32\OpenSSL\include
    3. Right click on the “ncat” project in Visual Studio and click “Properties“. In “Configuration Properties” > “General” > “C/C++” > “General“, in “Additional Include Directories“, add path “..\mswin32\OpenSSL\include” and “..\liblua
    4. Right click on the “ncat” project in Visual Studio and click “Properties“. In “Configuration Properties” > “General” > “Linker” > “General“, in “Additional Library Directories“, add path “..\mswin32\OpenSSL\lib
    5. Expand the “ncat” project, double click the file “ncat_ssl.c” and comment out the line “#include <openssl/applink.c>
    6. Right click on the “liblua” project in Visual Studio and click “Properties“. In “Configuration Properties” > “General” > “C/C++” > “Code Generation“, set “Runtime Library” to “Multi-threaded DLL (/MD)
    7. Right click on the “ncat” project in Visual Studio and click “Build“. Alternatively you can press the F7 key to start building.

Setting Keyboard Layout to US in Chinese Traditional (Hong Kong) Language for Windows 8

I used to have US keyboard layout with my Chinese language but since Windows 8, it is just not possible. When you click the Option, you can only see all Chinese input method, but none with US keyboard layout. It is frustrating when to manage two languages with each containing one input method. It wasn’t a matter to me as I am not a Windows 8 user until I have to work on Windows Server 2012 and face this problem, especially on a server without GUI. I found my way to configure the language setting using PowerShell, and along with that I also found a way to include US keyboard layout in Chinese Traditional (Hong Kong) language as well. End up the solution to have the above screenshot result is ironically by PowerShell script, not by GUI. Anyway below is the PowerShell script that I used. You can also access the code at
https://gist.github.com/VeryCrazyDog/4cafad4183b5a684a4b9

Edit 2015-08-10: For some reason the below code is altered by WordPress during display, resulting the code not workable. If that is the case for you, try access the code using the link above.

# Won't work on Windows 7
# WARNING: This will clear your language list setting
$langList = New-WinUserLanguageList zh-Hant-HK
$langList[0].InputMethodTips.Clear()
# Keyboard layout: US
$langList[0].InputMethodTips.Add('0C04:00000409')
Set-WinUserLanguageList $langList