【问题标题】:can't convert from delpi to C#无法从 delpi 转换为 C#
【发布时间】:2018-04-05 17:16:55
【问题描述】:

您好,我正在尝试用 C# 为 Delphi 应用程序编写一个插件,我正在使用 UnmanagedExports 库(NuGet 上的第一个库)导出我的函数

但现在我在将用 Delpi 编写的示例插件代码转换为 C# 时遇到问题

library myplugin;

uses
    SysUtils,
    Classes,
    Dialogs;

type
    P_RDSGroup=^TRDSGroup;
    TRDSGroup = record
        Year: word;
        Month: byte;
        Day: byte;
        Hour: byte;
        Minute: byte;
        Second: byte;
        Centisecond: byte;
        RFU: word;
        Blk1: integer;
        Blk2: integer;
        Blk3: integer;
        Blk4: integer;
end;

var
    PI: integer;
    Group: TRDSGroup;

{$R *.res}

procedure RDSGroup(PRDSGroup: P_RDSGroup); stdcall;
    begin
    Group:=PRDSGroup^;
    if (Group.Blk1>=0) then
        begin
    if (PI<>Group.Blk1) then
    begin
    PI:=Group.Blk1;
    ShowMessage('New PI has been detected: '+IntToHex(PI,4));
    end;
  end;
end;

procedure Command(Cmd, Param: PChar); stdcall;
    var w: string;
    begin
    w:=UpperCase(string(Cmd));
    if (w='CONFIGURE') then
        ShowMessage('Nothing to configure in this simple plugin.');
    if (w='RESETDATA') then PI:=-1;
    end;

function PluginName: PChar; stdcall;
begin
    Result:='My First Plugin';
end;

Exports
    RDSGroup, Command, PluginName;

begin
PI:=-1;
end

我正在尝试用 C# 翻译它

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;

namespace MyFirstRDSSpy_Plugin
{

    public class Class1
    {


        //P_RDSGroup = TRDSGroup; //Don't know what =^ does
        public struct TRDSGroup
        {
            public ushort Year;
            public byte Month;
            public byte Day;
            public byte Hour;
            public byte Minutes;
            public byte Second;
            public byte Centisecond;
            public ushort RFU;
            public int Blk1;
            public int Blk2;
            public int Blk3;
            public int Blk4;

        }
        static int PI; 
        static TRDSGroup Group;

        static void RDSGroup(TRDSGroup *PRDSGroup);
        {
            Group = *PRDSGroup; //not sure if fixed right?
            if (Group.Blk1 >= 0)
                {
                if (PI != Group.Blk1) {
                    PI = Group.Blk1;
                    System.Windows.Forms.MessageBox.Show("New PI has been detected:" + PI.ToString("X4"));
                }
            }

         }
    static void Command(string Cmd, string Param)
    {
        var w = "";
        w = Cmd.ToUpper();
        if (w == "CONFIGURE")
        {
            System.Windows.Forms.MessageBox.Show("Nothing to configure in this simple plugin.");

        }
        if (w == "RESETDATA")
        {
            PI = -1;
        }
    }
    static void PluginName()
    {
        return "My First Plugin";
    }
    //EXPORTS GO HERE
    [DllExport("RDSGroup", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    [DllExport("Command", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    [DllExport("PluginName", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    //EXPORTS GO HERE
}
}

但它不会编译 1. 我需要显示消息框,但我不知道如何从 C# 中的类库中做到这一点 2. 我需要做 P_RDSGroup=^TRDSGroup;但我不知道如何在 C# 中执行此操作 3. 不知道如何翻译 PChar(我认为是 Char 但我不确定)

对于任何想知道我想为其编写插件的应用程序的插件文档的人都在这里:http://rdsspy.com/download/pdf/spyapi.pdf

希望有人可以在这里帮助我 感谢您的回答和最诚挚的问候

【问题讨论】:

  • 在delphi中,word等价于C#ushort,而不是string
  • 顺便说一句,这里有很多不是有效的 C# 代码(这就是它无法编译的原因),例如我不确定这里的意图是什么:@ 987654327@?看起来您正在用 C# 复制/粘贴一些 Delphi 代码,但这是行不通的。
  • 我正在尝试转换 Delpi 过程 RDSGroup 并且由于它的过程它不会返回任何相当于 C# 中的 void 我认为还有一些像 P_RDSGroup=^TRDSGroup 这样的东西我不确定=^ 它的 C# exuivalent 不确定我必须为其参数输入什么,因为 PRDSGroup: P_RDSGroup 不是字符串也不是 int 所以我不知道在这里做什么
  • 如果你不知道基本的delphi语法,你怎么能尝试这个任务?人们有如此不切实际的期望。
  • @David:我在不了解(完整)基本语法的情况下翻译甚至修改了 Python 和 Ruby 代码,但我能够找出所用语法的确切含义并修改或翻译代码我需要(例如 Jekyll -- Ruby -- 或 Pygments -- Python)。花了一些时间学习我需要知道的那部分语言。我认为 PersonalNonGrata 也可以这样做。

标签: c# delphi


【解决方案1】:

感谢所有帮助我终于能够修复它的人

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;

namespace MyFirstRDSSpy_Plugin
{
    public class Class1
    {
        static unsafe TRDSGroup* P_RDSGroup;
    public struct TRDSGroup
{
        public ushort Year;
        public byte Month;
        public byte Day;
        public byte Hour;
        public byte Minutes;
        public byte Second;
        public byte Centisecond;
        public ushort RFU;
        public int Blk1;
        public int Blk2;
        public int Blk3;
        public int Blk4;

    }
    static int PI;
    static TRDSGroup Group;

    [DllExport("RDSGroup", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    unsafe static void RDSGroup(TRDSGroup* PRDSGroup)
    {
        Group = *PRDSGroup;
        if (Group.Blk1 >= 0)
        {
            if (PI != Group.Blk1) {
                PI = Group.Blk1;
                System.Windows.Forms.MessageBox.Show("New PI has been detected:" + PI.ToString("X4"));
            }
        }

    }
    [DllExport("Command", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    static void Command(string Cmd, string Param)
    {
        var w = "";
        w = Cmd.ToUpper();
        if (w == "CONFIGURE")
        {
            System.Windows.Forms.MessageBox.Show("Nothing to configure in this simple plugin.");

        }
        if (w == "RESETDATA")
        {
            PI = -1;
        }
    }
    [DllExport("PluginName", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    static string PluginName()
        {
        return "My First Plugin";
        }
    }
}

如果您想使用 WinForms,只需将其添加为参考 (System.Windows.Forms)

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2010-10-15
    • 2019-07-05
    • 2019-04-13
    • 2020-07-21
    相关资源
    最近更新 更多