C# Dll调用字符串编码问题

发布于 2024-10-31 21:00:33 字数 1795 浏览 0 评论 0原文

    StringBuilder codeline = new StringBuilder(100);


    protected virtual int OnCodeLine(int code, int docId, ref StringBuilder codeline)
            {
////
}

这就是我得到的*ref StringBuilder*

<块引用>

大吉大利×大吉大利×大吉大利éĞêĞëĞìĞíĞîĞïĞğĞñĞòĞóĞôĞõĞöĞ÷ĞøĞùĞúĞûĞüĞıĞşĞÿĞÑÑÑÑÑÑÑ Ñ 呵呵

呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵了

仅使用 StringBuilder 我只得到 3

这就是它应该返回

300 078 9059431

顺便说一下,检查中的 MICR 代码

 [DllImport("mflib.dll")]
    public static extern int mfScanFeeder(int mode, int font, int timeout);

 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
 public delegate int MFS100_CodeLineDelegate(int code, int docId, ref StringBuilder codeline);
 public event MFS100_CodeLineDelegate MFS100_CodeLine;
 private static  MFS100_CodeLineDelegate cache_CodeLine;

更新:这里是适用于的原始代码vb6

    Public Function MFS100_OnCodeline(ByVal code As Long, ByVal docId As Long, ByVal codeline As String) As Long
  Dim i As Integer

  WriteEvent "OnCodeline:"
  WriteEvent "  code = " & code
  WriteEvent "  docId = " & docId
  WriteEvent "  codeline = " & codeline

  MFS100_OnCodeline = -1        ' -1 means: sorting will be done by mfSort()

  g_codeline = codeline

  CScannerForm.TmrSort.enabled = True
End Function

更新 2

  mfSetEvent((int)EventEnum.E_EVENT_CODELINE, cache_CodeLine);



[DllImport("mflib.dll")]
        private static extern int mfSetEvent(int eventID, Delegate callsback);

当我将 StringBuilder 与 ref 一起使用时,我得到一个字符串,其中包含 32361长度。没有参考我得到 仅第一个值 细绳。

OnCodeLine 用于回调 从扫描仪设备。什么是 问题 ?

    StringBuilder codeline = new StringBuilder(100);


    protected virtual int OnCodeLine(int code, int docId, ref StringBuilder codeline)
            {
////
}

This is what i get with *ref StringBuilder*

ĞĞÑĞÒĞÓĞÔĞÕĞÖĞ×ĞØĞÙĞÚĞÛĞÜĞİĞŞĞßĞàĞáĞâĞãĞäĞåĞæĞçĞèĞéĞêĞëĞìĞíĞîĞïĞğĞñĞòĞóĞôĞõĞöĞ÷ĞøĞùĞúĞûĞüĞıĞşĞÿĞÑÑÑÑÑÑÑ Ñ
ÑÑÑ

ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ

and only with StringBuilder i only get 3

This is what its suppose to return

300 078 9059431

By the way this the MICR Code from Cheques

 [DllImport("mflib.dll")]
    public static extern int mfScanFeeder(int mode, int font, int timeout);

 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
 public delegate int MFS100_CodeLineDelegate(int code, int docId, ref StringBuilder codeline);
 public event MFS100_CodeLineDelegate MFS100_CodeLine;
 private static  MFS100_CodeLineDelegate cache_CodeLine;

Update : Here is the Original Code that works in vb6

    Public Function MFS100_OnCodeline(ByVal code As Long, ByVal docId As Long, ByVal codeline As String) As Long
  Dim i As Integer

  WriteEvent "OnCodeline:"
  WriteEvent "  code = " & code
  WriteEvent "  docId = " & docId
  WriteEvent "  codeline = " & codeline

  MFS100_OnCodeline = -1        ' -1 means: sorting will be done by mfSort()

  g_codeline = codeline

  CScannerForm.TmrSort.enabled = True
End Function

Update 2

  mfSetEvent((int)EventEnum.E_EVENT_CODELINE, cache_CodeLine);



[DllImport("mflib.dll")]
        private static extern int mfSetEvent(int eventID, Delegate callsback);

When i use StringBuilder with ref i get a string that have
32361 length. Without ref i get
only the first value of the
string.

OnCodeLine is for the callback
from the scanner device. What is the
problem ?

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

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

发布评论

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

评论(2

∞觅青森が 2024-11-07 21:00:33

您不应该通过引用传递 StringBuilder。当你这样做时,你会说你只得到一个角色。这是期望 ANSI 编码但实际接收 Unicode (UTF-16) 的标志。通过指定字符集修复它:

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode)]
public delegate int MFS100_CodeLineDelegate(int code, int docId, StringBuilder codeline);

You should not pass the StringBuilder by ref. When you do that you say you only get one character back. That's the hallmark of expecting ANSI encoding but actually receiving Unicode (UTF-16). Fix it by specifying the character set:

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode)]
public delegate int MFS100_CodeLineDelegate(int code, int docId, StringBuilder codeline);
生来就爱笑 2024-11-07 21:00:33

我不明白你想要实现什么,但是如果你想收集 OnCodeLine 内部生成的字符串,则不需要通过引用传递 StringBuilder,因为它是引用类型

只需传递不带引用的 StringBuilder,填充它,当您返回时,您将在其中拥有所需的字符串。

关于调用 OnCodeLine 后得到的结果,您能否提供一些有关实现的信息?

抱歉,我没有注意到涉及到 PInvoke! :(

I don't understand what are you trying to achieve, but if you want to collect a string generated inside OnCodeLine, you don't need to pass the StringBuilder by reference, as it is a reference type.

Just pass the StringBuilder without ref, populate it, and when you return you'll have the desired string in it.

Regarding what you get after calling the OnCodeLine, can you provide some info regarding the implementation?

Sorry, I didn't notice the PInvoke was involved!! :(

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