Visual Studio 线程语句报错?

我反编译了一个 .NET 应用程序以编写自己的注入软件。一个线程错误无法被删除。我尝试了一切但它仍然告诉我有些问题...

代码(截图)

{
    using System;
    using System.IO;
    using System.IO.Pipes;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;

    internal class NamedPipes
    {
        public static string luapipename = "IDUNNOPIPE123";

        public static void LuaPipe(string script)
        {
            if (NamedPipeExist(luapipename))
            {
                new Thread (delegate {
                    try
                    {
                        using (NamedPipeClientStream stream = new NamedPipeClientStream(".", luapipename, PipeDirection.Out))
                        {
                            stream.Connect();
                            using (StreamWriter writer = new StreamWriter(stream, Encoding.Default, 0xf423f))
                            {
                                writer.Write(script);
                                writer.Dispose();
                            }
                            stream.Dispose();
                        }
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("连接到管道时发生错误。", "连接失败!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    catch (Exception exception1)
                    {
                        MessageBox.Show(exception1.Message.ToString());
                    }
                }).Start();
            }
            else
            {
                MessageBox.Show("请在使用此功能之前注入 " + Functions.exploitdllname + "!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public static bool NamedPipeExist(string pipeName)
        {
            bool flag;
            try
            {
                int timeout = 0;
                if (!WaitNamedPipe(Path.GetFullPath($"\\\\.\\pipe\\{pipeName}"), timeout))
                {
                    bool flag4;
                    int num2 = Marshal.GetLastWin32Error();
                    if (num2 != 0)
                    {
                        if (num2 != 2)
                        {
                            goto TR_0005;
                        }
                        else
                        {
                            flag4 = false;
                        }
                    }
                    else
                    {
                        flag4 = false;
                    }
                    return flag4;
                }
            TR_0005:
                flag = true;
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        private static extern bool WaitNamedPipe(string name, int timeout);
    }
}
点赞
用户4272085
用户4272085

我认为你在使用这段代码出现了以下错误 The call is ambiguous between the following methods or properties: 'Thread.Thread(ThreadStart)' and 'Thread.Thread(ParameterizedThreadStart)'

如你所说,编译器不能分清 Thread 构造函数的参数类型。如果在调用 Thread.Start 方法时传递了参数给你的委托,那么就需要使用 ParameterizedThreadStart。而你的代码并没有这么做。因此需要在这里使用 ThreadStart 委托。其语法如下:

public delegate void ThreadStart();

也就是说它不接受任何参数并且不返回任何值。所以你可以使用一个 lambda 表达式 来代替。

new Thread(() => {
    // ...
}).Start();
2019-03-04 19:25:56