sscanf 在 CIL 中运行时返回 0

问题描述 投票:0回答:1

我有来自 Serge Lidin 的 Expert .NET 2.0 IL Assembler 的 IL 代码:

这位于名为 CilTest.il 的文件中:

//----------- Program header
.assembly extern  mscorlib { auto }
.assembly OddOrEven  { }
.module OddOrEven.exe
//----------- Class declaration
.namespace Odd.or {
    .class public auto ansi Even extends [mscorlib]System.Object {
//----------- Field declaration
        .field public static int32 val
//----------- Method declaration
        .method public static void check( ) cil managed {
            .entrypoint
            .locals init (int32 Retval)
        AskForNumber:
            ldstr "Enter a number"
            call void [mscorlib]System.Console::WriteLine (string)
            call string [mscorlib]System.Console::ReadLine ()
            ldsflda valuetype CharArray8 Format
            ldsflda int32 Odd.or.Even::val
            call vararg int32 sscanf(string,int8*,...,int32*)
            stloc Retval
            ldloc Retval
            brfalse Error
            ldsfld int32 Odd.or.Even::val
            ldc.i4 1
            and
            brfalse ItsEven
            ldstr "odd!"
            br PrintAndReturn
        ItsEven:
            ldstr "even!"
            br PrintAndReturn
        Error:
            ldstr "How rude!"
        PrintAndReturn:
            call void [mscorlib]System.Console::WriteLine (string)
            ldloc Retval
            brtrue AskForNumber
            ret
        } // End of method
    } // End of class
} // End of namespace
//----------- Global items
.field public static valuetype CharArray8 Format at FormatData
//----------- Data declaration
.data FormatData = bytearray(25 64 00 00 00 00 00 00) // % d . . . . . .
//----------- Value type as placeholder
.class public explicit CharArray8 
              extends [mscorlib]System.ValueType { .size 8  }
//----------- Calling unmanaged code
.method public static pinvokeimpl("msvcrt.dll" cdecl) 
    vararg int32 sscanf(string,int8*) cil managed { }

以下文件位于同一文件夹中,名为 CilTest.ilproj:

<Project Sdk="Microsoft.Net.Sdk.il/7.0.0">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
</Project>

我正在使用以下方式编译代码:

dotnet build -r win-x64 --self-contained -c release

当我运行代码时,出现此错误:

enter image description here

似乎调用

sscanf
返回 0 个成功的格式化字符串:

call vararg int32 sscanf(string,int8*,...,int32*)

为什么

sscanf()
不起作用?

有没有办法调试代码,以便我可以了解为什么

sscanf()
不起作用?

c .net-core scanf cil
1个回答
0
投票

我的经验有限,但在以这种方式调用 sscanf 时总是使用 PInvoke 方法。我们使用了类似的东西:

.method private pinvokeimpl("msvcrt.dll" cdec1)
    int32 sscanf _pinvoke(string, int8*)
cil; managed { }

您也可以编写自己版本的 sscanf(MSVC)(GLIBC),因为它是一个相对简单的函数,并将其合并到您的程序中。

© www.soinside.com 2019 - 2024. All rights reserved.