如何使用Dllimport在C#中调用此Delphi方法? [英] How do I call this Delphi method in C# using Dllimport?

查看:96
本文介绍了如何使用Dllimport在C#中调用此Delphi方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助的新程序员!

已编译到DLL中的Delphi代码

The Delphi code that is compiled into the DLL

function SetCurrentSerial(Size : Integer; Msg : Pointer) : Integer stdcall;
var
  TempByte : PByte;
  TempStr : string;
  i: Integer;
begin
  Result := 0;
  TempByte := Msg;
  TempStr := '';
  for i := 0 to Size - 1 do
  begin
    TempStr := TempStr + ' ';
  end;

  for i := 0 to Size - 1 do
  begin
    TempStr[i+1] := Chr(TempByte^);
    Inc(TempByte);
  end;

  if not DLLClass.SelectDeviceSerial(TempStr) then
  begin
    Result := -1;
  end;
end;

C#代码

//Import a Function Pointer
[DllImport("Test.dll", CallingConvention= CallingConvention.StdCall, CharSet =  CharSet.Ansi)]
public unsafe static extern int SetCurrentSerial(int Size, byte[] Msg);

我需要将指针值,大小和消息存储在缓冲区中,然后在控制台窗口中打印该值.

I need to store the pointer value, Size and Msg in a buffer and print the value in a console window.

我将非常感谢完整构建的代码.预先谢谢你.

I will greatly appreciate a fully constructed code. Thank you in advance.

这是我到目前为止尝试过的代码. //C#代码

Here is the code I've so far tried. //C# Code

class Program
{
    [DllImport("Test.dll")]
    public unsafe static extern int SetCurrentSerial(int Size, void* Msg); 

    unsafe static void Main()
     {
        int Res;

        byte[] buffer = new byte[1024];

        Res = SetCurrentSerial(255, &buffer);

        Console.WriteLine("%s\n", buffer);
     }
}

推荐答案

您的DLL函数设计错误.您正在将字符串从调用代码传递到DLL.这真的很简单,您可以删除几乎所有代码. Delphi代码应如下所示:

Your DLL function is designed incorrectly. You are passing a string from the calling code to the DLL. That is really simple to do and you can remove almost all of your code. The Delphi code should be like this:

function SetCurrentSerial(Serial: PAnsiChar): LongBool; stdcall;
begin
  Result := DLLClass.SelectDeviceSerial(Serial);
end;

然后,C#代码应为:

[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, 
    CharSet = CharSet.Ansi)]
public static extern bool SetCurrentSerial(string Serial);

像这样调用函数:

bool succeeded = SetCurrentSerial(Serial);
if (!succeeded)
{
    // handle error
}

我已将您的整数返回值替换为指示成功或失败的布尔值.您是否应该还原为如下所示的整数:

I've replaced your integer return value with a boolean indicating success or failure. Should you prefer to revert to an integer that would look like this:

德尔福

function SetCurrentSerial(Serial: PAnsiChar): Integer; stdcall;
begin
  if DLLClass.SelectDeviceSerial(Serial) then begin
    Result := 0;
  end else begin
    Result := -1;
  end;
end;

C#

[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall, 
    CharSet = CharSet.Ansi)]
public static extern int SetCurrentSerial(string Serial);


更新

显然,您不能更改此DLL.真可惜,因为它的设计和实施确实非常糟糕.但是,要从C#调用该函数,您需要像这样声明p/invoke:

Apparently you cannot change this DLL. That is a shame because it is really very badly designed and implemented. However, to call that function from C# you need to declare the p/invoke like this:

[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SetCurrentSerial(int Size, byte[] Msg);

然后这样称呼它:

byte[] Msg = Encoding.Default.GetBytes(serial);
int retval = SetCurrentSerial(Msg.Length, Msg);
if (retval != 0)
{
    // handle error
}

这篇关于如何使用Dllimport在C#中调用此Delphi方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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