Calling C# from C++

Most of the post on the internet about interoperability is the other way around (Calling C++ from C#) since C# is apparently the superior language in terms of developer friendliness. But a part of research work is to make weird combinations of system work regardless of the reason.

I succeeded in doing just that thank to this Visual C++ example (documentation is available in part 1 of the tutorial).

One thing to add is, I needed a tool to export .NET 4.0 to .tlb files so I can use the same assemblies in C++ (pure C++ and not C++.NET). The tool is called tlbgen, and the syntax is quite simple:

tlbgen System.Net.dll

Management task: Remote shutdown utility

I run a simple web server to serve as a storage medium for my mobile devices (I don’t like using dropbox or other online storage solutions because they don’t offer much space and they hogs up bandwidth unnecessarily). Before I go to sleep I usually shut down the server to save energy, but in order to do so, I have to have another computer turned on to perform remote access into the server and shut it down via command line. Occasionally, I would turn off my work computer before I remember that I want to shut down the server and I’m too lazy by then to turn on my work computer again.

That’s why I wanted an application that is capable of shutting down Windows on my behalf. The scenario is like this: because I can access FTP service from my phone, I will create a file, namely ‘shutdown’; when the server sees this file, it will delete the file and initiate shutdown. Quite simple.

To shut down, I use Windows management interface (System.Management namespace in C#)

        static void Shutdown()
        {
            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();

            // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams =
                     mcWin32.GetMethodParameters("Win32Shutdown");

            // Flag 1 means we want to shut down the system. Use "2" to reboot.
            mboShutdownParams["Flags"] = "1";
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown",
                                               mboShutdownParams, null);
            }
        }

From here

And the rest is just a loop to check if the file exists

        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            while (true)
            {
                if (!System.IO.File.Exists("D:\Shutdown"))
                {
                    Thread.Sleep(500);
                }
                else
                {
                    System.IO.File.Delete("D:\Shutdown");
                    Shutdown();
                    return;
                }
            }
        }

That’s it, I run the application and it will wait until I create a file named ‘shutdown’ in D:, it will perform shutdown and quit

Here’s the sample code and binary

Invisible folder for Windows

Recently there have been quite some posts about how to create an invisible folder, as the process is quite complicated, I thought “why not prepackage it?”. And here it goes, your invisible folder (extract and enjoy).

Why is it in rar format you ask? Because Windows’ default zip still can’t handle Unicode properly a decade later.

Typing Japanese with Dvorak keyboard

From StackOverflow

I did this for chinese. I just did it for japanese as well. Heres what you do:

Using the following link by Michael Kaplan, please read it before you change your continue.

http://superuser.com/questions/38781/how-can-i-use-the-chinese-ime-with-a-dvorak-layout

Start > Run > type regedit > press enter (registry editor will open)

Expand HKEY_LOCAL_MACHINE > Expand SYSTEM > Expand CurrentControlSet > Expand Control > Expand Keyboard Layouts > Select E0010411(japanese IME) > Export a copy for backup > Double-click Layout file > type kbddv.dll in the value data box.

You may need to restart your computer. note that E0010411 is for japanese, E00E0804 is for chinese.

Good luck!

Best setup to conveniently type English Dvorak, Vietnamese, and Korean (or any other East Asian language)

At first I thought I have to define three languages, because Unikey automatically made Windows switch to Vietnamese when I type, the default keyboard switching shortcut also conflict with Unikey’s (Ctrl + Shift); so every time I want to switch between QWERTY and DVORAK, I have to check whether I had accidentally turned Unikey off, and I have to press the language switching shortcut two times to switch between the two most used language (English and Korean). Believe me, that’s a lot of hassle right there.

So, after a lot of fiddling, I figured out I could just have it like this

My setup

(To add Dvorak to Korean, select “show more…”)

This way, I just need to switch between korean mode and English mode by pressing Ctrl + Shift (Microsoft IME still uses the QWERTY layout, which I don’t want to use, so I left it in 영어 mode). Conveniently, when I switch to Korean, Unikey is also turned off so I won’t accidentally put accent on Korean characters.

So, to summary:

  • To Type English, I can just type in Dvorak
  • I can also type Vietnamese, since VNI typing does fit really nice with English
  • Press Ctrl + Shift to switch to Korean, which also turns Vietnamese off