无法从delpi转换为C#

时间:2018-04-05 17:16:55

标签: c# delphi

您好我正在尝试在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

希望有人可以帮助我 感谢Anwsering和Best Regards

1 个答案:

答案 0 :(得分:0)

感谢所有帮助我最终解决问题的人

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)