.NET的DLL需要接收号角回调过程,然后所有它传递3个整数? [英] .NET DLL needs to receive a Clarion callback procedure and then all it passing three ints?

查看:276
本文介绍了.NET的DLL需要接收号角回调过程,然后所有它传递3个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个C#.NET的DLL的号角(号角是C ++类编程语言)计划。

I'm writing a C# .NET DLL for a Clarion (Clarion is C++ like programming language) program.

我打电话到C#.NET的DLL就好了,一切是伟大的工作。但是,我需要一个C#.NET的DLL接收号角程序回调的目的,然后可以调用该程序通过3个int参数。

I call into the C# .NET DLL just fine, all is working great. However, I need that C# .NET DLL to receive a Clarion Procedure for callback purposes and then be able to call that procedure passing three int parameters.

号角过程是这样的(一个号角长是C#INT):

The Clarion Procedure looks like this (a Clarion long is a C# int):

MyCallBack procedure(long p1, long p2, long p3)
... Data ...
    code
    ... Code ...

我如何通过abvoe程序到C#.NET的DLL和如何做的C#.NET的DLL调用过程传递3个int参数?

How do I pass the abvoe procedure to the C# .NET DLL and how does the C# .NET DLL call that procedure passing three int parameters?

在此先感谢。

推荐答案

希望这个例子给你地方开始,它是基于从SoftVelocity新闻组为例(缓存的纯文本版的这里)。

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

注意:我使用的是 RGiesecke DLLEXPORT 包和号角LibMaker的修改版本来创建兼容库文件。你刚才提到你已经调用到C#DLL没有问题,所以我假设你正在做类似的事情。如果你有兴趣,我进入这个对我的博客进一步

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.

  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);

  }
}

这篇关于.NET的DLL需要接收号角回调过程,然后所有它传递3个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆