Skip to content

Instantly share code, notes, and snippets.

@Archie-Yang
Last active October 15, 2017 16:22
Show Gist options
  • Save Archie-Yang/f794873f1ec03c8abc92a59a0ab67133 to your computer and use it in GitHub Desktop.
Save Archie-Yang/f794873f1ec03c8abc92a59a0ab67133 to your computer and use it in GitHub Desktop.
sscanf (msvcrt40.dll)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
[DllImport("msvcrt40.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int sscanf(string str, string format, __arglist);
static void Main(string[] args)
{
var enc = Encoding.Default;
var buf1 = new byte[100];
var buf2 = new byte[100];
int int1, int2;
Console.WriteLine(sscanf("hello world! 123456789 987654321", "%s %s %d %d", __arglist(buf1, buf2, out int1, out int2)));
Console.WriteLine(enc.GetString(buf1, 0, Array.IndexOf(buf1, (byte)0))); // hello
Console.WriteLine(enc.GetString(buf2, 0, Array.IndexOf(buf2, (byte)0))); // world!
Console.WriteLine(int1); // 123456789
Console.WriteLine(int2); // 987654321
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment