Avoid Windows 10 forcing you to create a Microsoft account instead of a local one

28 Mar 2021 / ERap320

Microsoft has been really pushing online accounts to log in to a Windows installation, both when you are setting up a new machine and while you are using it, with endless notifications. While avoiding it during initial setup is easy (don't connect to any network until you have created your local account), stopping the latter is not easy.

Luckily, we can use Group Policies to solve it.

You can find "Edit group policy" by searching in the start menu, or by executing GPEdit.msc from the run menu.

  • Find Computer Configuration->Windows Settings->Security Settings->Local Policies->Security options
  • Locate the policy "Accounts: Block Microsoft Accounts"
  • Change the value to "Users can't add Microsoft accounts"

You probably also want to change this settings to be able to install Microsoft Store apps without an account

  • Computer Configuration->Administrative Templates->Windows Components->App runtime
  • Locate the setting "Allow Microsoft accounts to be optional"
  • Change the value to "Enabled". This will allow apps which support it to be installed without a Microsoft account attached

Git commit history graph in the terminal

27 Mar 2021 / ERap320

A history graph, like the ones provided by Github and Gitlab are very useful to check how your branches interact at a glance. However, git is always difficult to control and you may want to make sure you didn't break your precious repository before pushing. With this simple command, you can view the usual log visually organized as a graph:

git log --graph --pretty=oneline --abbrev-commit

Alias

If you also think this command may be very useful and you don't want to type it every time, you can create an alias for it. If you are using "Git Bash" for Windows like I do, you can do it this way:

cd && echo "alias git-graph='git log --graph --pretty=oneline --abbrev-commit'" >> .bash_profile

The next time you open Git Bash, git-graph will be available.

Removing files from commits after adding them to .gitignore

07 Mar 2021 / ERap320

It happens way too often. You just set up a brand new repository for the project, you push the initial commit, you check everything is ok and... damn, you forgot the .gitignore. Now you add the .gitignore but the useless files are already in the last commit, and they won't go away.

With this command you can untrack the newly ignored files, so they won't show up in any future commit:

git rm -r --cached .

After this you can go on with the usual

git add .

How to refresh Windows icons cache without rebooting

14 Feb 2020 / ERap320

Windows is very jealous of its saved icons, it doesn't want them to change in any way. This is the problem I face every time I'm developing a program and I try to change its icon. The operating system associates the executable to the old one, and it shows that instead of the one actually contained in the binary.

Luckily, there is a simple command to refresh the icon cache without having to delete system folders or rebooting, as some answers found online suggest.

Windows 10

ie4uinit.exe -show

Other Windows versions

ie4uinit.exe -ClearIconCache

System or closed process keeping a socket open on Windows

30 Oct 2019 / ERap320

While developing and debugging a C# program for a customer I noticed some very strange behaviour: when the program crashed or was terminated from the debugger (without having time to properly close a listening socket), the TCP port would stay open indefinitely. This was very annoying because after the crash I obviously couldn't restart the program without getting errors about the port being already open. Trying to close the socket from a handy program like cports didn't work at all: the process listening on the port was already dead, so it didn't know how to kill it! Not having much time to debug this I logged off and on my Windows account to force the socket to die every time the issue happened.

After implementation and testing of the most important features of the program I decided to look into the problem once more. At first I found some comments about how it's impossible to make Windows close listening sockets of a crashed application or that it closes itself after some minutes, but this sounded like complete nonsense to me. This is not the first network-involving program I write and I never stumbled in this problem before. The fact that it seemed to only happen "sometimes" made me skeptical this was a Windows problem. Didn't really know what to do with this information, so I went on googling.

I started looking into socket settings or flags, trying to set linger options and timeouts, but that did nothing. I also went so far as to set ReuseAddr to make the errors go away. This option makes the program always connect to the port even if it's occupied, but this didn't solve the problem because now I couldn't detect if there was a REAL other program already listening. Moreover, my users could open the program many times and only one instance would actually receive packets. This was unacceptable at best.

In the end, some post I found and some tries lead me to the right idea: child processes. Since the program had to work with an Oculus Quest (an Android based VR headset), some functions interfaced with it by opening adb and parsing its console output. I quickly figured out the problem appeared only when I used some feature involving adb, and closing its process also freed the TCP port!

When I programmatically launched adb, it became a child process. This would be fine if adb.exe ended after giving me its output but, as any Android developer knows, it stays open in the background waiting for other requests. By default, the child process which outlives its parent inherits almost all of its handles, including sockets. To obtain the desired behaviour, I "just" had to disable the inheritance on the process or on the handle. Too bad there is no way to do it in C# when calling the child process. The introduction of some way to set inheritance flags with pure C# was proposed but as far as I can tell everything stopped there.

I decided to just call kernel32.dll and use Windows APIs to do this. Luckily, the C# Socket class includes a Handle parameter exposing the handle we need.

//Windows kernel magic to avoid adb keeping inherited sockets alive when the program crashes

[Flags]
public enum HANDLE_FLAGS : uint
{
     NONE = 0,
     INHERIT = 1,
     PROTECT_FROM_CLOSE = 2
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetHandleInformation(IntPtr hObject, HANDLE_FLAGS dwMask, HANDLE_FLAGS dwFlags);

//End of Windows kernel magic

And when declaring the socket:

Socket socket = new Socket(IPAddress.Any.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
SetHandleInformation(socket.Handle, HANDLE_FLAGS.INHERIT, 0);

Lenovo Watch MOD: change chinese apps in the notification options

14 Jul 2018 / ERap320

I recently bought a Lenovo Watch 9, and I love its hardware, but I can't say the same about the software. The Lenovo Watch app didn't have the quality I expected: for example, nobody tells you that the search of the watch will always fail unless you have the GPS of your phone turned on.

After I solved this little problem I found the watch quite useful, but couldn't get over the absence of useful apps to get notifications, like Telegram, my favourite mobile messaging service. I decided to just mod the app to remove the chinese apps (arguably useless for anyone outside of China) and replaced it with the apps from which I need notifications the most.

These are the replacements I made:

  • WeChat -> Telegram
  • QQ -> Microsoft Outlook
  • Weibo -> Google Calendar
  • Line -> Clock

I also fixed Skype notifications, which didn't work in the original application.

Changes

I hope this work will be useful to you too!

Download the latest version of the mod from its GitHub repository

DOWNLOAD



1 2 next ... end