Delphi 程序生成不正确的 CRC16,想要在 C# 中复制行为

发布于 2025-01-05 21:19:23 字数 1045 浏览 3 评论 0原文

我继承了一个 Delphi 应用程序,我需要在工作中将其转换为 C# 应用程序。

其中一部分使用外部 dll 中的 CRC16 函数来为某些数据创建校验和。问题是生成的这个 CRC 与我在其他地方生成的任何 CRC 都不匹配。

我已经尝试了大约 10 个不同的 CRC 代码片段,这些代码片段是我在网上找到的,并且尝试了几个在线生成器。我生成的 CRC 与我在线生成的 CRC 相匹配,例如在 -

http:// www.lammertbies.nl/comm/info/crc-calculation.html

所以我现在唯一的结论是 Delphi 应用程序没有生成有效的 CRC16 校验和。这给我留下了两个选择

1] 尝试找出它生成的是哪种 CRC

2] 以某种方式从 C# 内部调用 DLL 中的函数

我已经尝试过这两种方法,但都失败了。

--

所有源代码都在 github 上 -

https://github。 com/delphi2sharp/delphisharp/blob/master/CRC16Test/CRC16Test/Program.cs

https://github.com/delphi2sharp/delphisharp/blob/master/DelphiCRC16Test/Unit1 .pas

和 CRC.DLL 也在那里。

--

任何帮助将不胜感激。

谢谢

I've inherited a Delphi application which I need to convert to a C# application at my job.

One part uses what is supposed to be a CRC16 function in an external dll to create a checksum for some data. The problem is that this CRC which gets generated doesn't match any of the CRCs ive managed to generate elsewhere.

I've tried about 10 different CRC code snippets which I found online, and have tried several online generators. The CRCs I generate match the ones I generate online, for example at -

http://www.lammertbies.nl/comm/info/crc-calculation.html

So my only conclusion now is that the Delphi application is not generating a valid CRC16 checksum. This leaves me with two options

1] Try to figure out which kind of CRC it is generating

2] Somehow call the functions in the DLL from inside C#

I've tried both and failed miserably.

--

All the source is on github -

https://github.com/delphi2sharp/delphisharp/blob/master/CRC16Test/CRC16Test/Program.cs

https://github.com/delphi2sharp/delphisharp/blob/master/DelphiCRC16Test/Unit1.pas

and the CRC.DLL is also there too.

--

Any help would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零崎曲识 2025-01-12 21:19:23

我认为你需要一个两阶段的方法。

1./ 目前编写一个“unsafe/pInvoke”包装器并调用本机 dll。

2./一旦更换了Delphi应用程序并且您可以控制编码和解码,请编写更标准的CRC函数。

调用本机 DLL 并不困难,请查看 pinvoke.net 上的一些示例。

I think that you need a 2 phase approach.

1./ For the moment write an "unsafe/pInvoke" wrapper and call the native dll.

2./ Once the Delphi app is replaced and you control the encoding as well as the decoding, write a more standard CRC function.

Calling a native DLL is not that difficult, have a look at some of the examples at pinvoke.net.

淡淡離愁欲言轉身 2025-01-12 21:19:23

如果您需要向后兼容性,为什么不使用原始本机 dll 的 pInvoke 包装器。

If you need backward compatibility why don't you use a pInvoke wrapper of the original native dll.

雅心素梦 2025-01-12 21:19:23

首先,我为将此作为答案发布表示歉意;发表评论太麻烦了。

那么你没有CRC.Dll的源代码吗?

看它,它看起来就像另一个基于Delphi的文件,它至少依赖于Kernel32.dll;除此之外我不能在这台 Mac 上说。
据观察,这种差异可能是由于 Delphi 和 .Net 之间的 Unicode 差异造成的。

如果您将其移植到 .Net,是否有任何原因不能用已知的良好解决方案替换该 CRC,或者是否必须保留一些向后兼容性?
如果是这样;你能不使用.Net中常见的CRC代码并对遗留的东西进行批量更新吗?

如果不;那么也许答案是放弃这个定制的 CRC 生成器并使用能够验证您的期望并提供可重复的准确结果的东西。

Firstly, I apologise for posting this as an answer; it's just too unwieldy to put into a comment.

Do you not have the source to CRC.Dll then ?

Looking at it, it looks like another Delphi based file, which has a reliance on atleast Kernel32.dll; other then that I can't say on this Mac.
An observation is that this difference may be due to Unicode differences between Delphi and .Net.

If you're porting this to .Net though, is there any reason why you can't just replace that CRC with a known good solution or do you have to retain some backwards compatibility ?
If so; can you not use the common CRC code in .Net and do a batch update on the legacy stuff?

If not; then perhaps the answer is to dump this bespoke CRC generator and use something that validates your expectations and provides repeatable accurate results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文