0

Guys I'm stuck at the final part of a functional deskband implementation for my application,

For context, I'm currently running a visual studio solution with 2 projects in them, one is the c++ deskband dll and another is a simple c# console program. The DeskBand code is Microsoft's DeskBand sample

Currently, I can register the dll when the console app runs by calling the "DllRegisterServer" with GetProcAddress(). After this I'll simply right-click the taskbar and enable the registered deskband. My question comes from here on,

Once registered, let's say the OnPaint event in the Deskband implementation paints a text variable called "Hello world", What I want to do is simply send a string from c# to the running dll and make it paint "Sent from c#".

The register class:

public class Registrar
{
    private IntPtr hLib;
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
    internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern IntPtr LoadLibrary(string lpFileName);
    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern bool FreeLibrary(IntPtr hModule);
    internal delegate int PointerToMethodInvoker();
    public Registrar(string filePath)
    {
        hLib = LoadLibrary(filePath);
        if (IntPtr.Zero == hLib)
        {
            int errno = Marshal.GetLastWin32Error();
            throw new Win32Exception(errno, "Failed to load library.");
        }
    }
    public void RegisterComDLL()
    {
        CallPointerMethod("DllRegisterServer");
    }
    public void UnRegisterComDLL()
    {
        CallPointerMethod("DllUnregisterServer");
    }

    private void CallPointerMethod(string methodName)
    {
        IntPtr dllEntryPoint = GetProcAddress(hLib, methodName);
        if (IntPtr.Zero == dllEntryPoint)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        PointerToMethodInvoker drs =
               (PointerToMethodInvoker)Marshal.GetDelegateForFunctionPointer(dllEntryPoint,
                           typeof(PointerToMethodInvoker));
        drs();
    }

    public void FreeLib()
    {
        if (IntPtr.Zero != hLib)
        {
            FreeLibrary(hLib);
            hLib = IntPtr.Zero;
        }
    }
}

The simple console program:

static void Main(string[] args)
    {
        Console.WriteLine("Start");
        Registrar reg = new Registrar("DeskBandDLL.dll");
        if (reg != null)
        {
            reg.RegisterComDLL();
            //send data to the registered dll to start painting new text
            Console.ReadLine();
            reg.UnRegisterComDLL();
            reg.FreeLib();
        }
    }

Btw, I do know that Microsoft discontinued deskband in win11, but I really want to implement this feature for only the win10 platform and I feel like I'm close to finishing this.

And if you need the full code for clarification, I can host it on GitHub.

TBA
  • 1,921
  • 4
  • 13
  • 26
Ash
  • 131
  • 1
  • 7
  • 1
    Are you looking for PInvoke? This article should get you started https://learn.microsoft.com/en-us/dotnet/framework/interop/consuming-unmanaged-dll-functions – funatparties Feb 07 '22 at 16:28
  • 1
    First, IDK why you'd need to do anything other than a PInvoke on DllRegisterServer. However, some questions to ask... Just because you call DllRegisterServer does not mean it will succeed. Typically it should be run with elevated privileges so it registers with the system. If run with non-elevated privileges it might (probably) fail when called. It depends on whether it can detect that it should only register for the user when running non-elevated. Are you trying to register the classes for the machine, or for the user? Elevated or non-elevated? Why not write an installer using WiX toolkit? – Joseph Willcoxson Feb 07 '22 at 16:40
  • @JosephWillcoxson, yep im running this in admin mode and plan on running it always in admin mode. – Ash Feb 07 '22 at 16:49
  • Microsoft discontinued deskbands with Windows 7, not 11: "You should use thumbnail toolbars in new development in place of desk bands, which are not supported as of Windows 7." https://learn.microsoft.com/en-us/windows/win32/api/shobjidl/nn-shobjidl-ideskband2 – Simon Mourier Feb 07 '22 at 18:33

0 Answers0