Add root certificate to Android system

2019-08-07 12:01:16 Using Computers Android

Starting from Android 7 Nougat, applications must explicitly trust user-installed certificates for them to be effective. To bypass this, we can install our own root certificate in the system certificate store.

Requirements: the phone must be rooted.

(more…)


Change when Windows turns the monitor off in lockscreen

2018-05-30 17:31:33 Using Computers Windows

Find the following registry key: HKEY_LOCAL_MACHINESYSTEM/CurrentControlSet/Control/Power/PowerSettings/7516b95f-f776-4464-8c53-06167f40cc998EC4B3A5-6868-48c2-BE75-4F3044BE88A7 and change Attributes to 2.

Now change the time in Power Options (advanced options).


VCmd script to compile current file and build a project

2018-04-08 09:27:12 Coding C# Visual Studio

Useful for CUDA development, since VS somehow does not monitor the changes in CU files.

(more…)


Set RDP certificate

2018-03-07 10:53:19 Using Computers Windows

After installing the certificate and grating read to NETWORK SERVICES, regedit:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp

Set value of SSLCertificateSHA1Hash


JavaScript event listener for history.pushState

2018-01-13 20:14:38 Coding JavaScript

In HTML there is an event onpopstate, but not onpushstate or onalterstate. How can we monitor such state changes? The answer is to alter the history.pushState function and manually dispatch an event inside.

More over, suppose we are writing a Chrome extension which has run_at: "document_start" that aims to capture all changes in states. Then we need to inject a <script> tag into the HTML, and we must do it as early as possible – right after the <head> tag is available. To achieve this we use a MutationObserver.

(more…)


Windows 10 “Open command window here”

2017-12-19 08:46:42 Using Computers Windows

Powershell is nice, but too slow.

Edit with regedit:

HKCR\Directory\shell\cmd
HKCR\Directory\Background\shell\cmd
  1. Change owner
  2. Add permission
  3. Rename HideBasedOnVelocityId to anything (e.g. ShowBasedOnVelocityId)

Ubuntu systemd start-up script

2017-12-07 13:39:31 Using Computers Ubuntu
  1. Create a script, e.g. /root/startup.sh.
  2. Create a service in `/etc/systemd/system’, e.g. ‘/etc/systemd/system/startup.service’
  3. Edit the service:
[Unit]
Description=Startup Script

[Service]
Type=oneshot
ExecStart=/root/startup.sh
WorkingDirectory=/root

[Install]
WantedBy=multi-user.target

Lastly, run systemctl enable startup.service

Reboot and test.

Tips:

  1. Before adding the service, run the script to confirm that it works
  2. Before reboot, use service [name] start to confirm that it works

Optimizing C# reflection method calls

2017-03-08 14:35:20 Coding C#

TL;DR: Func<> and Action<> instead of MethodInfo.Invoke runs 50 times faster

I can recall the excitement when I first learnt about reflection in C#. At that time my programming experience was limited to basic VB6 and C, so being able to browse type information was completely new to me. I was shocked and inspired by the possiblities that reflection can bring.

Reflection enables a lot of applications. For example, GameObject.SendMessage in Unity is fulfilled by finding the method with a name via reflection and then invoking it. Another important application is generic serializers. Without reflection, serializing objects will involve the pain of writing similar routines for every class[1]. Generic serializers like XmlSerializer and BinaryFormatter have saved a huge amount of development effort.

[1] Or some messy template and macro magic in C++ – see this StackOverflow answer for more.

However, we may overlook the performance cost as we enjoy the convenience. The rest of this post measures the reflection method calls in C#, and propose an optimization with delegate.

(more…)


Handling generic parameters in .Net dynamic code generation

2017-02-14 17:26:55 Coding C++

In C#, it is possible to write code that generate dynamic assemblies, classes and methods at run time. This is a very powerful metaprogramming trick as it allows developers to generate IL instructions according to runtime situations. It also helps to reduce the size of an assembly by creating classes and methods only when needed. I heard that this approach is used to create XmlSerializer instances for better performance.

This article is not an introduction of how to do the magic (though it is fairly simple and you can learn from the skeleton code below without any difficulity). It explores a very specific situation when generic typing meets dynamic IL emitting.

(more…)


Draw curves in WPF efficiently

2016-12-16 11:19:38 Coding C++ WPF

I have a customized canvas inheriting Canvas and overrides the OnRender(DrawingContext dc) method. And I need to draw some curves. I have precomputed the points on the curve and stored them in a List<Tuple<double, double>> because I am drawing different parts of the curve at different time. So, it is natural to come up with

for (int i = start; i <= end; ++i)
{
    dc.DrawLine(CurvePen, new Point(list[i].Item1, list[i].Item2), new Point(list[i+1].Item1, list[i+1].Item2));
}

However this approach is slow and CPU-intensive as hell. Guess what? I had a naive test and wrote

var gstr = new StringBuilder();
gstr.Append($"M {list[start].Item1},{list[start].Item2} L ");
for (int i = start + 1; i <= end; ++i)
{
    gstr.Append($"{t.Item1},{t.Item2} ");
}
dc.DrawGeometry(null, CurvePen, Geometry.Parse(gstr.ToString()));

Basically the code constructs a Path Markup, parse it to a Geometry object and draw it. It looks terrible – that’s a lot of string operations… but it is actually very, very fast. In fact, much faster than the multiple DrawLine calls.

One possible optimization is to, apparently, precompute all the Geometry objects, although it may result in worse performance because that’s a lot of objects. So I’ll just stick to this construct-on-the-fly thing at the moment 😛