3

I am creating a C# DLL that will be used as a plug-in for the PLSQL Developer IDE, which is designed in C++.

My C# DLL needs to accept a C++ pointer, then assign that pointer to a function or method, to be called later on.

The IDE provides a specifications document for building these plug-in's, but it only provides samples for C++ and Delphi. The specifications document provides more information I included in this screenshot.

Provided C++ Example:

void (*IDE_MenuState)(int ID, int Index, BOOL Enabled);
BOOL (*IDE_Connected)();
void (*IDE_GetConnectionInfo)(char **Username, char **Password, char **Database);
void (*IDE_GetBrowserInfo)(char **ObjectType, char **ObjectOwner, char **ObjectName);

void RegisterCallback(int Index, void *Addr)
{
    switch (Index)
    {
        case 10 :
        (void *)IDE_MenuState = Addr;
        break;
        case 11 :
        (void *)IDE_Connected = Addr;
        break;
        case 12 :
        (void *)IDE_GetConnectionInfo = Addr;
        break;
        case 13 :
        (void *)IDE_GetBrowserInfo = Addr;
        break;
    }
}

C# I Have So Far:

I should note that I am using Robert Gieseckes Unmanaged Exports NuGet Package for exporting functions. I can change this if necessary.

public bool IDE_Connected()
{
    return false;
}

public void IDE_MenuState(int ID, int Index, bool Enabled)
{

}

[DllExport("add", CallingConvention = CallingConvention.Cdecl, ExportName= "RegisterCallback")]
public static void RegisterCallback(int Index, IntPtr Addr)
{
    if (Index == 10)
    {
       // Assign IntPtr Addr to IDE_MenuState()
       // Please help :)
    }
    if (Index == 11)
    {
       // Assign IntPtr Addr to IDE_Connected()
       // Please help :)
    }

}

How can I assign the C++ pointer argument to my C# methods?

Josh
  • 51
  • 6

1 Answers1

2

For practical purposes, compiled methods cannot be changed at run-time. So changing the functionality of IDE_Connected() (from your code example) is not possible.

You can, however, declare your methods as delegates (see here), and create a static instance of each one. Try this:

public unsafe class MyClass
{
    delegate void IDE_MenuState(int ID, int Index, bool Enabled);
    delegate bool IDE_Connected();
    delegate void IDE_GetConnectionInfo(char** Username, char** Password, char** Database);
    delegate void IDE_GetBrowserInfo(char** ObjectType, char** ObjectOwner, char** ObjectName);

    static IDE_MenuState method_IDE_MenuState;
    static IDE_Connected method_IDE_Connected;
    static IDE_GetConnectionInfo method_IDE_GetConnectionInfo;
    static IDE_GetBrowserInfo method_IDE_GetBrowserInfo;

    public static void RegisterCallback(int Index, IntPtr Addr)
    {
        switch (Index)
        {
            case 10:
                method_IDE_MenuState = (IDE_MenuState)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_MenuState));
                break;
            case 11:
                method_IDE_Connected = (IDE_Connected)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_Connected));
                break;
            case 12:
                method_IDE_GetConnectionInfo = (IDE_GetConnectionInfo)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_GetConnectionInfo));
                break;
            case 13:
                method_IDE_GetBrowserInfo = (IDE_GetBrowserInfo)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_GetBrowserInfo));
                break;
        }
    }

    public static void IDE_MenuState(int ID, int Index, bool Enabled)
    {
        if (method_IDE_MenuState == null)
        {
            throw new MissingMethodException("IDE_MenuState has not been assigned pointer yet.");
        }
        method_IDE_MenuState(ID, Index, Enabled);
    }

    public static bool IDE_Connected()
    {
        if (method_IDE_Connected == null)
        {
            throw new MissingMethodException("IDE_Connected has not been assigned pointer yet.");
        }
        return method_IDE_Connected();
    }

    public static void IDE_GetConnectionInfo(char** Username, char** Password, char** Database)
    {
        if (method_IDE_GetConnectionInfo == null)
        {
            throw new MissingMethodException("IDE_GetConnectionInfo has not been assigned pointer yet.");
        }
        method_IDE_GetConnectionInfo(Username, Password, Database);
    }

    public static void IDE_GetBrowserInfo(char** ObjectType, char** ObjectOwner, char** ObjectName)
    {
        if (method_IDE_GetBrowserInfo == null)
        {
            throw new MissingMethodException("IDE_GetBrowserInfo has not been assigned pointer yet.");
        }
        method_IDE_GetBrowserInfo(ObjectType, ObjectOwner, ObjectName);
    }
}

Note: I have not tested this, but try changing the method signatures of IDE_GetConnectionInfo and IDE_GetBrowserInfo to use out char[] or even better, out string, instead of char**. This will make your API more useful in C#.

Mr Anderson
  • 2,200
  • 13
  • 23