.NET DLL needs to receive a Clarion callback procedure and then all it passing three ints?

冷暖自知 提交于 2019-12-04 12:38:51

Hopefully this example gives you somewhere to start, it is based on an example from the SoftVelocity newsgroups (cached plain text version here).

Note: I am using the RGiesecke DllExport package and a modified version of the Clarion LibMaker to create a compatible lib file. You mentioned that you are already calling into the C# DLL without issue so I am assuming that you are doing something similar. If you are interested, I go into this further on my blog.

Clarion Code

  PROGRAM
  MAP
    MODULE('ManagedCSharpDLL.dll')
CallbackProc                PROCEDURE(BSTRING PassedValue, *BSTRING ReturnValue),TYPE,PASCAL,DLL(TRUE)
SetCallback                 PROCEDURE(*CallbackProc pCallback),NAME('SetCallback'),PASCAL,RAW,DLL(TRUE)
TestCallback                PROCEDURE(*CString passedString),NAME('TestCallback'),PASCAL,RAW,DLL(TRUE)
    END
Callback                PROCEDURE(BSTRING PassedValue, *BSTRING ReturnValue),PASCAL
  END
a                   CSTRING(20)

CODE
  Message('Clarion: SetCallback(Callback)')
  SetCallback(Callback)

  a = 'Call Test Worked'
  Message('Clarion: Send message: ' & a)

  TestCallback(a)

  Message('Clarion: Made call and got back safely')

Callback      PROCEDURE(BSTRING PassedValue, *BSTRING ReturnValue)

  CODE
    MESSAGE('Clarion: Passed Value: ' & PassedValue)
    ReturnValue = 'Done'

C# Code

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using RGiesecke.DllExport;

namespace ManagedCSharpDLL
{
  public static class UnmanagedExports
  {

    private static CallbackProc _callback;

    [DllExport("SetCallback", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    public static void SetCallback(CallbackProc pCallback)
    {
      _callback = pCallback;
      MessageBox.Show("C#: SetCallback Completed");
    }

    [DllExport("TestCallback", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    public static void TestCallback(string passedString)
    {
      string displayValue = passedString;
      string returnValue = String.Empty;

      MessageBox.Show("C#: About to call the Callback. displayValue=" + displayValue + ", returnValue=" + returnValue);
      _callback(displayValue, ref returnValue);
      MessageBox.Show("C#: Back from the Callback. displayValue=" + displayValue + ", returnValue=" + returnValue);
    }

    public delegate void CallbackProc( [MarshalAs(UnmanagedType.BStr)] String PassedValue,  [MarshalAs(UnmanagedType.BStr)] ref String ReturnValue);

  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!