我正在尝试通过 c# 将结构传递给 delphi,我已经完成以下操作来传递消息,我按照 pinvoke 的格式从https://www.pinvoke.net/default.aspx/Structures.COPYDATASTRUCT复制 datat 结构,但在德尔福我没有收到任何消息。在某种程度上,我相信这是因为我没有以正确的方式对结构进行编码。当我只传递一个字符串消息时,我会收到它,但是当我尝试传递结构时,什么都没有
这是我到目前为止所做的。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ccTestForm2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SendFingerPrintResult();
}
const int WM_COPYDATA = 0x004A;
//include SendMessage
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpszClass, string
lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Ansi, EntryPoint = "SendMessage", SetLastError = false)]
public static extern int SendMessageCopyData(IntPtr hWnd, int uMsg, UIntPtr wParam, ref COPYDATASTRUCT lParam);
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
public struct ReturnStruct
{
public int i;
public string card;
public string name;
public string responsecode;
public string responsetext;
public string approval;
public string tranid;
public string reference;
public double d;
public string transactionType;
public string creditCardType;
public int EMVContact;
public string applicationName;
public string applicationIdentifier;
public string reserved;
public IntPtr ToPtr()
{
IntPtr ret = Marshal.AllocHGlobal(473);
IntPtr ptr = ret;
Marshal.WriteInt32(ptr, i); ptr = IntPtr.Add(ptr, 4);
DelphiShortStringHelper.WriteToPtr(card, ref ptr, 50);
DelphiShortStringHelper.WriteToPtr(name, ref ptr, 100);
DelphiShortStringHelper.WriteToPtr(responsecode, ref ptr, 5);
DelphiShortStringHelper.WriteToPtr(responsetext, ref ptr, 100);
DelphiShortStringHelper.WriteToPtr(approval, ref ptr, 15);
DelphiShortStringHelper.WriteToPtr(tranid, ref ptr, 50);
DelphiShortStringHelper.WriteToPtr(reference, ref ptr, 16);
Marshal.Copy(new double[] { d }, 0, ptr, 1); ptr = IntPtr.Add(ptr, 8);
DelphiShortStringHelper.WriteToPtr(transactionType, ref ptr, 24);
DelphiShortStringHelper.WriteToPtr(creditCardType, ref ptr, 10);
Marshal.WriteInt32(ptr, EMVContact); ptr = IntPtr.Add(ptr, 4);
DelphiShortStringHelper.WriteToPtr(applicationName, ref ptr, 50);
DelphiShortStringHelper.WriteToPtr(applicationIdentifier, ref ptr, 15);
DelphiShortStringHelper.WriteToPtr(reserved, ref ptr, 10);
return ret;
}
}
public ReturnStruct GetReturnStruct()
{
var ret = new ReturnStruct();
ret.i = 2;
ret.card = "1234";
ret.name = "test";
ret.responsecode = "mese";
ret.responsetext = "dork";
ret.approval = "Plerk";
ret.tranid = "pse";
ret.reference = "Ig";
ret.d = DateTime.UtcNow.ToOADate();
ret.transactionType = "cit";
ret.creditCardType = "t2";
ret.EMVContact = 0;
ret.applicationName = "mpp";
ret.applicationIdentifier = "nne";
ret.reserved = "12";
return ret;
}
public void SendFingerPrintResult()
{
// get the window to send struct
IntPtr receiverHandle = FindWindow("TReceiverMainForm", "ReceiverMainForm");
if (receiverHandle == IntPtr.Zero) return;
// Get the struct
ReturnStruct ret = GetReturnStruct();
IntPtr ptr = ret.ToPtr();
try
{
var cds = new COPYDATASTRUCT
{
dwData = IntPtr(2), // to identify the message contents
cbData = Marshal.SizeOf(ret),
lpData = ptr
};
SendMessageCopyData(receiverHandle, WM_COPYDATA, UIntPtr.Zero, ref cds);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
}
}
class DelphiShortStringHelper
{
public static void WriteToPtr(string s, ref IntPtr ptr, byte maxChars = 255)
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(s);
int strLen = Math.Min(bytes.Length, (int)maxChars);
Marshal.WriteByte(ptr, (byte)strLen);
ptr = IntPtr.Add(ptr, 1);
Marshal.Copy(bytes, 0, ptr, strLen);
ptr = IntPtr.Add(ptr, (int)maxChars);
}
}
}