Did you check the logs?
you can find the logs here (replace username, CompanyName, ProductName): C:\Users\username\AppData\LocalLow\CompanyName\ProductName\Player.log
It is likely that the process cannot be found and throws an exception. But you are trying to access the currentProcess anyway I guess, so you could simply use Process.GetCurrentProcess().
For the actual interop and logic I am not an expert and cannot tell you if that code would work or not.
Thanks a lot, got it working. Part of the problem was indeed that the process/window could not be found, but with @StarManta ‘s suggestion, using GetActiveWindow() the window can be found and then to hide the taskbar you have to make the window a tool window using WS_EX_TOOLWINDOW. It’s important that you DON’T use the "~’" character because that is I assume for removing attributes. So anyway thanks a lot for the help and here is the final code to remove the taskbar icon of a Unity application:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
public class HideTaskbarIcon : MonoBehaviour
{
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("User32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("User32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
private const int GWL_EXSTYLE = -0x14;
private const int WS_EX_TOOLWINDOW = 0x0080;
void Start()
{
IntPtr pMainWindow = GetActiveWindow();
SetWindowLong(pMainWindow, GWL_EXSTYLE, GetWindowLong(pMainWindow, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
}
}
Be careful that your game is indeed the Active Window. Not always true if some other window is active (like running your game through another process). Also, ~ is a bitwise inversion, ~WS_EX_TOOLWINDOW means ‘all bits EXCEPT WS_EX_TOOLWINDOW’.