如何使用Pchar函数来使用c#
how to use Pchar function to use c#
如何在 C# 中使用此函数?
function CheckCard (pPortID:LongInt;pReaderID:LongInt;pTimeout:LongInt): PChar;
此函数包含dll。
我可以这样试试:
[DllImport("..\RFID_107_485.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.ThisCall)]
public static extern char CheckCard(int pccPortID, int pccdReaderID, int pccTimeout);
char pccCheckCard = CheckCard(3, 129, 1000);
Console.WriteLine(pccCheckCard);
但我没有得到真正的答案...
请帮帮我? :)
这里有很多问题。这是我能看到的:
- 编写的Delphi 代码使用Delphi
register
调用约定。这只能从 Delphi 代码访问,不能通过 p/invoke 方法调用。但是,您可能从代码中省略了调用约定,实际上是 stdcall
.
- 您的 p/invoke 使用
CallingConvention.ThisCall
,这肯定不匹配任何 Delphi 函数。 Delphi. 不支持该调用约定
- 您将指向以 null 结尾的字符数组的指针
PChar
错误翻译为 char
单个 UTF-16 字符。
- Delphi 代码看起来很可疑。函数 returns
PChar
。那么,谁负责释放返回的字符串。如果 Delphi 代码返回一个指向在函数 returns 时被破坏的字符串变量的指针,我不会感到惊讶,这是一个非常常见的错误。
- 您使用相对路径引用 DLL。这是非常危险的,因为您无法轻易控制是否会找到 DLL。将 DLL 放在与可执行文件相同的目录中,并仅指定 DLL 的文件名。
- 没有看到任何错误检查。
可能有效的变体如下所示:
Delphi
function CheckCard(pPortID: LongInt; pReaderID: LongInt; pTimeout: LongInt): PChar;
stdcall;
C#
[DllImport("RFID_107_485.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CheckCard(int pccPortID, int pccdReaderID, int pccTimeout);
....
IntPtr pccCheckCard = CheckCard(3, 129, 1000);
// check pccCheckCard for errors, presumably IntPtr.Zero indicates an error
// assuming ANSI text
string strCheckCard = Marshal.PtrToStringAnsi(pccCheckCard);
// or if the Delphi code returns UTF-16 text
string strCheckCard = Marshal.PtrToStringUni(pccCheckCard);
这留下了未解决的问题,即如何释放返回的指针。您必须查阅该功能的文档才能找到它。该问题包含的信息不足。
如何在 C# 中使用此函数?
function CheckCard (pPortID:LongInt;pReaderID:LongInt;pTimeout:LongInt): PChar;
此函数包含dll。
我可以这样试试:
[DllImport("..\RFID_107_485.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.ThisCall)]
public static extern char CheckCard(int pccPortID, int pccdReaderID, int pccTimeout);
char pccCheckCard = CheckCard(3, 129, 1000);
Console.WriteLine(pccCheckCard);
但我没有得到真正的答案...
请帮帮我? :)
这里有很多问题。这是我能看到的:
- 编写的Delphi 代码使用Delphi
register
调用约定。这只能从 Delphi 代码访问,不能通过 p/invoke 方法调用。但是,您可能从代码中省略了调用约定,实际上是stdcall
. - 您的 p/invoke 使用
CallingConvention.ThisCall
,这肯定不匹配任何 Delphi 函数。 Delphi. 不支持该调用约定
- 您将指向以 null 结尾的字符数组的指针
PChar
错误翻译为char
单个 UTF-16 字符。 - Delphi 代码看起来很可疑。函数 returns
PChar
。那么,谁负责释放返回的字符串。如果 Delphi 代码返回一个指向在函数 returns 时被破坏的字符串变量的指针,我不会感到惊讶,这是一个非常常见的错误。 - 您使用相对路径引用 DLL。这是非常危险的,因为您无法轻易控制是否会找到 DLL。将 DLL 放在与可执行文件相同的目录中,并仅指定 DLL 的文件名。
- 没有看到任何错误检查。
可能有效的变体如下所示:
Delphi
function CheckCard(pPortID: LongInt; pReaderID: LongInt; pTimeout: LongInt): PChar;
stdcall;
C#
[DllImport("RFID_107_485.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CheckCard(int pccPortID, int pccdReaderID, int pccTimeout);
....
IntPtr pccCheckCard = CheckCard(3, 129, 1000);
// check pccCheckCard for errors, presumably IntPtr.Zero indicates an error
// assuming ANSI text
string strCheckCard = Marshal.PtrToStringAnsi(pccCheckCard);
// or if the Delphi code returns UTF-16 text
string strCheckCard = Marshal.PtrToStringUni(pccCheckCard);
这留下了未解决的问题,即如何释放返回的指针。您必须查阅该功能的文档才能找到它。该问题包含的信息不足。