Send data from c# program to c++ dll (DeskBand)

148 Views Asked by At

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.

0

There are 0 best solutions below