Posted on 30-11-2012
Filed Under (Technology) by bigbang

There are a lot of articles about how to write Windows Services for managed C++ or C# code. But for native C++ coder, the situation is more Complicated. You have to install Windows Service by command line tool in you source code, and add the command line arguments in setup projects.

Here I try to explain the whole process in two steps.

1. Write native Win32 C++ code with service installation code

Unmanaged code service installations should have a command to install as a service and also uninstall. That means you need to add the service installation code in the project.

The key point of the code is the usage of the Service Control Manager ( SCM ). Please follow this example A basic Windows service in C++ (CppWindowsService) to build basic Windows Service application.

After Compilation, then you use argument -install to install the service,

CppWindowsService.exe -install

and use argument -remove to remove the service

CppWindowsService.exe -remove

. Once the service is installed you can edit the service settings in the Windows Service Manager. To access this, run the following from the run dialog or command prompt.




2. Add custom action in setup and deploy project to install the service while installing the application

All our application should be packaged to MSI installer to deploy to users. So they can use the setup file to install our application.

There are many third party solutions to provide this package function, like InstallShield. Visual Studio 2008 also provide Setup Projects to do this job.

The key point of unmanaged code service installations is that we should have a command to install as a service and also uninstall. In a Visual Studio setup you’d add a custom action to run your exe with that command line, probably -install and something like -remove as an uninstall custom action.

If you’re not using Visual Studio then just use the ServiceInstall / ServiceControl options in your setup tool.

Let’s use setup projects in Visual Studio as example. The key point is that you need to add custom action at the end of installation. With the above code, the argument of custom action should be -install.

By following steps, we can package the exe files for MSI installer:

To add a deployment project

  1. On the File menu, point to Add, and then click New Project.
  2. In the Add New Project dialog box’s Project Type pane, open the Other Project Types node, select Setup and Deployment Projects. In the Templates pane, choose Setup Project. In the Name box, type Custom Action Installer.

    The project is added to Solution Explorer and the File System Editor is displayed.

  3. In the File System Editor, select Application Folder in the left pane. On the Action menu, point to Add, and then choose Project Output.
  4. In the Add Project Output Group dialog box, CppWindowsService will be displayed in the Project list. Select Primary Output.

    Primary Output from CppWindowsService (Active) appears in the Application Folder.

To add the custom action

  1. Select the Custom Action Installer project in Solution Explorer. On the View menu, point to Editor, and then choose Custom Actions.

    The Custom Actions Editor is displayed.

  2. In the Custom Actions Editor, select the Commit node. On the Action menu, choose Add Custom Action.
  3. In the Select Item in Project dialog box, double-click the Application Folder. Then select Primary output from CppWindowsService.

    Primary output from CppWindowsService appears under the Commit node in the Custom Actions Editor.

  4. In the Properties window, make sure that the InstallerClass property is set to False (this is the default).
  5. In the Custom Actions Editor, select the Install node and add Primary output from CppWindowsService to this node as you did for the Commit node.
  6. On the Build menu, choose Build Custom Action Installer.

Then under the Debug or Release folder of setup project, you can find the MSI file. Just run the file, you will get the Windows service installed.

(1) Comment   
Posted on 16-11-2012
Filed Under (Technology) by bigbang

The basic usage of Emacs-w3m is the same as w3m itself. The host keys is the same as w3m. So it is very easy for the users who are familiar with w3m to use Emacs-w3m.

Although the manual of w3m has described the operation of w3m very clearly, I still want to outline some points confused me when I use Emacs-w3m at the beginning.

  1. How do I fill in forms with w3m?

    Form input fields are displayed in red (or reverse). Move the cursor to them and hit RETURN.

  2. How do I enter a URL?

    Type SHIFT-U, or use ‘U’ instead.

  3. How to get link URL?

    When the cursor is on a link, use ‘u’ to peek link URL. Emacs will get the link URL into system’s clipboard.

In which suitation should I use Emacs-w3m? Comparing with other browsers like Chrome, Firefox, is there any reason for you to use Emacs-w3m?

At first the reason I used Emacs-w3m was jsut for fun, just want to enjoy the cleanness provided by text browser. But right now I use w3m in three suitations:

  1. To read some articles I don’t want other colleagues know. By using w3m, I can make the website look ugly and not easy to identify.
  2. To copy formatted text from HTML to Emacs Org file. When I take notes or write blogs in Org Mode, I need to copy some content from internet and I don’t want to edit hyperlinks manually. So Emacs-w3m together with org-w3m is the thing for me.

    I will write a mode for Emacs which can copy the Chrome, Firefox region into the clipboard in org-mode format, so that pasting into org-mode buffers retains all html links and images. It will be more convinent.

  3. To find my blog article links. There are a lot of internal links in my blog articles, when I need them I use Emacs-w3m to find them and copy links directly.

Is it awsome? Let Emacs be your time killer at first, and Emacs will be your time saver at last.

(0) Comments   
Posted on 15-11-2012
Filed Under (Technology) by bigbang

When we need to copy rich content from HTML page while using Emacs Org Mode to take notes or write blogs, I used to copy the content directly and paste it to org file. Then all the rich text becomes plain text.

This way is simple but has a defect: if you are copy rich text with hyperlinks or formatted text, you need to add hyperlinks and formats after you pasting the text. The business is too troublesome to miss anything important.

Today I find a tool which can copy w3m rich text to Org file and keep the format. The steps to do it are:

First use Emacs-w3m to explore internet, then install org-w3m mode, and finally you can use command M-x org-w3m-copy-for-org-mode or keybindings C-c C-x M-w to copy rich text and convert HTML content to Org tags automatically.

It seems that org-w3m is a good toy, haha~~

PS: it will be better if we can copy images from HTML and cache them under folder which we are editing org files. Maybe someday I will make one Emacs mode for this.

(1) Comment   
Posted on 14-11-2012
Filed Under (Technology) by bigbang

There are a lot of advantages to use FastCGI as web server solution, and there is also an easy way to construct such kind of server by using Apache and mod_fcgi module.

Let’s use Ubuntu 11.04 and Apache2.2 to develop a FastCGI Web Server.

1. Configure Apache and mod_fcgid module

First, install Apache from command line under Ubuntu:

$ sudo apt-get install apache2

Then we can also install mod_fcgid module in the same way:

$ sudo apt-get install libfcgi-dev

After the installation of Apache and mod_fcgid module, then we can edit configure file


to modify options about mod_fcgid. BTW: according to Apache edition and installation path, the Apache configure file is under different path, so please check Apache manual for the details.

The example of configuration is:

<Directory /var/www/>
SetHandler fcgid-script
Options +ExecCGI

# Customize the next two directives for your requirements.
Order allow,deny
Allow from all

The above configuration is trying to put FastCGI binary code under path /var/www/ . Or you can choose another path in which Apache can execute the binary code.

2. Install fcig development package

Under Linux operating system, we need to install libfcgi library to use C, C++, Java, Perl to develop FastCGI program. The fcgi library encapsulate FastCGI protocol, so we don’t need to pay attentation to FastCGI protocol details. We can use following command to install it:

$ sudo apt-get install libfcgi-dev

Then create a .c source code file in your work folder, input following C code and compile it:

#include "fcgi_stdio.h"
#include <stdlib.h>

void main(void)
    int count = 0;
    while(FCGI_Accept() >= 0)
        printf("Content-type: text/html\r\n"
               "<title>FastCGI Hello!</title>"
               "<h1>FastCGI Hello!</h1>"
               "Request number %d running on host <i>%s</i>\n",
                ++count, getenv("SERVER_NAME"));

The command to compile the example code is:

$ gcc tiny-fcgi.c -o tiny-fcgi -lfcgi

The binary file of FastCGI program can execute directly (yes, trust me, it can run as normally program as well) and output related result.

Then copy the compiled binary file to Apache FastCGI path.

$ cp tiny-fcgi /var/www/

3. Ping your FastCGI services

Restart Apache service:

$ sudo /usr/sbin/apachectl restart

Then input following address in your favorite web browser, and you can reach your FastCGI service:

4. Ending

The configuration of FastCGI is not complex and I will introduce some advanced techniques of FastCGI later. FastCGI is some kind of old things in IT world, and I think it is suitable for distributed computation using C as computing engine.

The separation of web server and computing engine is good for web developers and computing program developers to work seperatly and enjoy their work alone.

(0) Comments   
Posted on 13-11-2012
Filed Under (Technology) by bigbang

Emacs Marmalade provides the way to install Emacs packages automatically, just like Ubuntu installing softwares.

It is very convenient to install Emacs Addons for Emacs 24.

Emacs 24 has embeded Marmalade and can turn it on by following code:

(require 'package)
(add-to-list 'package-archives 
    '("marmalade" .

Then we can use M-x list-packages to list all packages, choose one and install it.

If you know the package name, you can also install it directly by using command M-x package-install [RET] [Package-name] to install the specific package.

(0) Comments   
Posted on 13-11-2012
Filed Under (Technology) by bigbang

We always need to indent spaces for code block or text block in Emacs, especially in Org files.

For example, when you inserts a code block in Emacs Org file, you always need to insert blocks similar to following:

#+begin_src cpp
#include <iostream>

int main()
    return 0;

But when you copy the code from your source code file, the code is always aligned to the left of the page. When you export your org file to html file, the # mark will damage all the code block content.

We need to indent the code block for 2 spaces or 4 spaces, then the block mark will work.

It’s easy! There are two ways to do the indention.

The first way, Use the indent-rigidly command with a numeric prefix.

For example, after selecting the block, C-u 4 M-x indent-rigidly to indent the region by four spaces, C-u -4 M-x indent-rigidly to remove four spaces.

You can also use C-x <TAB> key bindings to replace M-x indent-rigidly command.

The second way, we could also use column mode.

We can select the lines in column mode, then add four spaces to all of them at once by following steps:

  1. Jump to column 1 of the first line you want to indent.
  2. C-<space> to set mark.
  3. move the point down to the last lines (stay in column one).
  4. C-x r t to enter column mode (or C-<RET> if you’ve got CUA mode), the input <space><space><space><space><RET> to indent four spaces.
(0) Comments    Read More