Tuesday, September 16, 2008

Execute Java jar file from .Net

Step1: Creating a Java Class

package ZAQ;

import javax.swing.JOptionPane;
public class Hello {
public static void main (String [] args) {
JOptionPane.showMessageDialog(null, "Hello World!");

Put this Class Inside ZAQ directory and set the class file name as Hello.java

Step2: Preparing of mainClass.txt file.
Write the following line in the file. Please remember to put a carriage return after the line.
Main-Class: ZAQ.Hello

Step3: Compile the Java Class and Build the jar file
Create class file for Hello.java and then build the jar file. Do the following commands
->Javac ZAQ/Hello.java
->jar cmf mainClass.txt ZAQ.jar ZAQ
->java –jar ZAQ.jar Hello
Now your jar is ready.

Step4: Create a batch file to be execute in .Net
Save a file with the following text:
java -jar D:\ZAQ.jar

Step5: .Net code is here.

int i = 0;
string sOut = ShellExec(@"d:\Comm.bat", "", "", out i);

And the function is here:

protected static String ShellExec(String cmd, String path, String parms, out int exit_code)
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(path + cmd, parms);
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;

System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
string tool_output = p.StandardOutput.ReadToEnd();
exit_code = p.ExitCode;
return tool_output;

Sunday, September 14, 2008

Use windows authentication for your own application.

This sample is very short and clear, if you wish to authenticate your application user as a windows or domain user then follow the next few steps.

Step1: declare an external function like this.
[DllImport("advapi32.dll", SetLastError = true)]
protected static extern bool LogonUser(string sUsername, string sDomain, string sPassword,
int iLogonType, int iLogonProvider, ref IntPtr ipToken);

Step2: Call the function.
IntPtr pExistingTokenHandle = new IntPtr(0);
pExistingTokenHandle = IntPtr.Zero;
// create token
string sUserName = "use1";
string sDomain = "domain1";
string sPassword = "Test123";
bool bImpersonated = LogonUser(sUserName, sDomain, sPassword,

This "bImpersonated" return True if user authenticated and false if not. And you can decide actions for your application based on this.

Sunday, September 7, 2008

Playing with Windows Registry, CRUD

In this article I’ll show the sample for each operation of CRUD for Win32 Registry. There is nothing theoretical that you should know before this, just should have idea what is win32 registry and how a developer can use it for a product.
Registry keeps information in a hierarchy format. You can put here data for using globally for a client PC. That means if you set a registry value you may use it for any number of product installed on this PC. But this place is not for storing huge data or operation data for your application, rather this place to keep information like settings information, license information, last instance value of your product, or even you can save URL for use external services. I can show hundreds of examples in where you can use Registry as a repository, but I think it’s up to you that how you would like take the advantage of this. Just leave it here and start play with this.

Create a Registry:
In Registry you will be able to save several data type but all will be under a key. In hierarchy it shows like a directory but registry will count this as a Key. So, you can add a key, string, binary, multi-string value and few more type of data under a Key. Here is the code:

string sPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\TestingRegistry\\ZAQ\\";
string sKeyName = "Address";

static void CreateSubKey(string sPath, string sKeyName, string sValue)
RegistryKey reg = null;
if (sPath.Contains("HKEY_LOCAL_MACHINE"))
reg = Registry.LocalMachine.CreateSubKey(sPath.Replace("HKEY_LOCAL_MACHINE\\", ""));
reg.SetValue(sKeyName, sValue);
else if (sPath.Contains("HKEY_CURRENT_USER"))
reg = Registry.CurrentUser.CreateSubKey(sPath.Replace("HKEY_CURRENT_USER\\", ""));
reg.SetValue(sKeyName, sValue);

Let’s consider you will call this function with this way.
So, this function will add a string value named “Phone” under the key of “ZAQ”. But if you will look at this hierarchy then you will find it will be more logical if there will be a Key name “Address” and the phone number data will be kept under this. To do this you just need to change the “sPath” and make it more extended.

string sPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\TestingRegistry\\ZAQ\\Address";

So now this one will create a Key name Address and then the Phone number will be kept.

Tuesday, September 2, 2008

Example for Encryption and Decryption with C#.

This example describes how a simple Encryption and Decryption can be written and how those functions can be used from client application.

First we will create a singleton Utility class which will contain Encryption and Decryption functions. For Cryptography operation two values will be required which are “Key” and “IV”. By default I have set those values and also I expose properties for both, so that you can use these values when you will use this class as per your demand.

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ZAQ.Util.CCryptography
public class CCryptography
protected string sKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456";
protected string sIV = "ABCDEFGHIJKL";

protected byte[] bKey;
protected byte[] bIV;

private CCryptography()
bKey = Convert.FromBase64String(sKey);
bIV = Convert.FromBase64String(sIV);

public static CCryptography Instance
return UIUtilityCreator.CreatorInstance;

public string EncryptString(string Value)
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputBytes = utf8encoder.GetBytes(Value);

TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform = tdesProvider.CreateEncryptor(bKey, bIV);

MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write);
cryptStream.Write(inputBytes, 0, inputBytes.Length);
encryptedStream.Position = 0;

Byte[] bResult = new Byte[encryptedStream.ToArray().Length];
encryptedStream.Read(bResult, 0, encryptedStream.ToArray().Length);
return Convert.ToBase64String(bResult);

public string DecryptString(string Value)
byte[] inputBytes = Convert.FromBase64String(Value);
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform = tdesProvider.CreateDecryptor(bKey, bIV);

MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputBytes, 0, inputBytes.Length);
decryptedStream.Position = 0;

byte[] bResult = new byte[decryptedStream.ToArray().Length];
decryptedStream.Read(bResult, 0, decryptedStream.ToArray().Length);

UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(bResult).ToString();

public string Key
sKey = value;

public string IV
sIV = value;

private sealed class UIUtilityCreator
// Retrieve a single instance of a Singleton
private static readonly CCryptography _instance = new CCryptography();

public static CCryptography CreatorInstance
get { return _instance; }

Now here is the sample code to use this class.

class Program
static void Main(string[] args)
CCryptography oCryptography = CCryptography.Instance;

string sEncrypt = "Test";
string sEncrypted ="";
string sDecrypted = "";
sEncrypted = oCryptography.EncryptString(sEncrypt);
sDecrypted = oCryptography.DecryptString(sEncrypted);

Console.WriteLine("Text to be Encrypted: " + sEncrypt + "\nEncrypted Text: " + sEncrypted + "\nDecrypted Text: " + sDecrypted);