Sunday, May 12, 2013

WTL::CRichEditCtrl

WTL::CRichEditCtrl needs the following line for initialization, add it in your DllMain or _tWinMain and it resolves the issue:
HINSTANCE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName());
Unfortunately I forgot that Rich Edit isn’t a common control and is not initialized with InitCommonControlsEx.

Tuesday, February 19, 2013

Some COM Books


Some books on COM. I just keep forgetting the name !!

  1. Inside OLE, 2nd Edition, by Kraig Brockschmidt (Microsoft Press)
  2. Understanding ActiveX and OLE, by David Chappell (Microsoft Press)
  3. Inside COM, by Dale Rogerson (Microsoft Press)

Wednesday, October 31, 2012

Print map of India using C

I don't know from where I got this code but its amazing ;)

#include <stdio.h> 
int main()
{
 int a,b,c;
 int count = 1;
 for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
      TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
      UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
      NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
      HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
      T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
      Hq!WFs XDt!" [b+++21]; )

      for(; a-- > 64 ; )
       putchar ( ++c=='Z' ? c = c/ 9:33^b&1); return 0; 

 return 0;
}

The Output

                    !!!!!!                                                     
                    !!!!!!!!!!                                                 
                     !!!!!!!!!!!!!!!                                           
                       !!!!!!!!!!!!!!                                          
                     !!!!!!!!!!!!!!!                                           
                      !!!!!!!!!!!!                                             
                      !!!!!!!!!!!!                                             
                        !!!!!!!!!!!!                                           
                        !!!!!!!!                                               
                        !!!!!!!!!!                                             
                       !!!!!!!!!!!!!!                                          
                     !!!!!!!!!!!!!!!!                                          
                    !!!!!!!!!!!!!!!!                                  !!!!!    
                  !!!!!!!!!!!!!!!!!!!                               !!!!!!!!!! 
                 !!!!!!!!!!!!!!!!!!!!!!!                 !         !!!!!!!!!!  
            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!              !!     !!!!!!!!!!!!    
           !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !!      !!!!!!!!       
            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!      
             !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!       
              !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  !!!!!!!!!!!!       
       !!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !!!!!!        
      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!      !!!!!         
          !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !!!          
        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        !          
          !!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!                       
           !!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!                         
                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!                          
                 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!                           
                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!                               
                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!                               
                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!                                 
                  !!!!!!!!!!!!!!!!!!!!!!!!!!                                   
                  !!!!!!!!!!!!!!!!!!!!!!!!!                                    
                   !!!!!!!!!!!!!!!!!!!!!!!!                                    
                    !!!!!!!!!!!!!!!!!!!!                                       
                    !!!!!!!!!!!!!!!!!!!                                        
                     !!!!!!!!!!!!!!!!                                          
                      !!!!!!!!!!!!!!!!                                         
                      !!!!!!!!!!!!!!!                                          
                       !!!!!!!!!!!!!!                                          
                        !!!!!!!!!!!!                                           
                        !!!!!!!!!!!!                                           
                        !!!!!!!!!!!!                                           
                          !!!!!!!!                                             
                          !!!!!!                                               
                           !!!!                                                
   

Query system for an environment variable value using C/C++

Today one of my colleague asked me how to query an environment variable using C++. The solution is simple, just use getenv() method. See the below code.

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
#pragma warning(disable: 4996) // disable warning for getenv()

int main()
{
 char *pointer = NULL;

 string str;
 cout<<"Enter the environment variable: ";

 while (cin>>str) {
  if(str.compare("exit") == 0) break;
  if(pointer = getenv(str.c_str())) {
   cout<<endl<<"Value of \""<<str<<"\" Variable: "<<pointer<<endl<<endl<<"Enter the environment variable: ";
  }else {
   cout<<"No such variables defined."<<endl<<"Enter the environment variable: ";
  }
 }
}

Tuesday, October 9, 2012

HttpServer - The Oracle JDK 1.6 Hidden Feature

I have seen this class many a times, but never encountered a scenario to use it. Recently in one of my application I needed to embedded a HTTP server. So I thought, Why not give this class a try !!

HTTP server infrastructure/framework is very simple to implement and is bundled only with the Oracle JDK 1.6. Following are the main classes:
  1. HttpServer
  2. HttpContext
  3. HttpExchange
See this link for sun.* package discussions.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.List;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class JavaServer 
{
 public static final String nl = System.getProperty("line.separator");

 public static void main(String[] args) throws Exception 
 {
  if(args.length != 2)
  {
   printUsage();
   System.exit(-1);
  }
  String strPort = null;
  String strAddress = null;

  for (int i = 0; i < args.length; i++) 
  {
   if(args[i].startsWith("-p")){
    strPort = args[i].substring(2, args[i].length()).trim();
   }else if(args[i].startsWith("-a")){
    strAddress = args[i].substring(2, args[i].length()).trim();
   }else
    throw new IllegalArgumentException("Unknown command '" + args[i]+ "'");
  }

  if(strAddress.length() == 0 || strPort.length() == 0)
   throw new IllegalArgumentException("Port = " + strPort + ", IP = " + strAddress);

  int port = Integer.parseInt(strPort);
  InetSocketAddress address = new InetSocketAddress(strAddress, port);

  System.out.println("Going to run server on '" + address + "'");

  new JavaServer().start(address);
 }

 private static void printUsage() {
  System.out.println("$JavaServer -p<Port> -a<IP Address>");
 }

 private HttpServer server;

 public void start(InetSocketAddress address) throws IOException
 {
  server = HttpServer.create(address, 0); 
  server.setExecutor(Executors.newCachedThreadPool());
  server.createContext("/", new RootHandler());
  server.start();
 }
}

class RootHandler implements HttpHandler 
{
 private static final int BUFFER_SIZE = 1024;

 public void handle(HttpExchange exchange) throws IOException
 {
  String method = exchange.getRequestMethod();

  if(method.equalsIgnoreCase("get"))
  {
   String request = "";
   ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);
   InputStream is = exchange.getRequestBody();

   byte[] buff = new byte[BUFFER_SIZE];
   while (true) 
   {
    int out = is.read(buff);
    if(out == -1)
     break;
    baos.write(buff, 0, out);
   }

   is.close();

   if (baos.size() > 0) {
    request = URLDecoder.decode(baos.toByteArray().toString(), "UTF-8");
   } else {
    request = null;
   }

   StringBuilder buf = new StringBuilder();
   buf.append("<html><head><title>Simple HTTP Server !!</title></head><body>");
   buf.append("<p><pre>");
   buf.append(exchange.getRequestMethod() + " " + exchange.getRequestURI() + " " + exchange.getProtocol() + JavaServer.nl);

   Headers headers = exchange.getRequestHeaders();

   for (String name : headers.keySet()) {
    List<String> values = headers.get(name);
    for (String value : values) {
     buf.append(name + " --> " + value + JavaServer.nl);
    }
   }
   if (request != null) {
    buf.append(JavaServer.nl);
    buf.append(request);
   }

   buf.append("</pre></p>");
   buf.append("</body></html>\n");

   String response = buf.toString();

   Headers responseHeaders = exchange.getResponseHeaders();
   responseHeaders.set("Content-Type", "text/html");
   exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length());

   OutputStream res = exchange.getResponseBody();
   res.write(response.getBytes());
   res.close();
  }else
  {
   String response = "Bad request method !!";
   exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_METHOD, response.length());
   OutputStream res = exchange.getResponseBody();
   res.write(response.getBytes());

   res.close();
  }
  exchange.close();
 }
}

Steps to run the above code

  1. Compile it using Oracle JDK 1.6
  2. Then execute this command on console (without quotes) 'Java JavaServer -p<port number> -a<ipaddress or localhost>' . For example, java JavaServer  -p5463 -a127.0.0.1
  3. Open your browser and type the URL. For example, http://127.0.0.1:5463/

You should see something like this (depends on the browser):