Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - VLS

#646
Connectors / [SOURCE] TextBox Connector 20140115
January 15, 2014, 07:24:06 PM
Comm.cs

Code (csharp) Select
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;

namespace TextBox_Connector
{
    public static class Comm
    {
        // Set module Type
        private static string type = "display";

        // Set namespace
        private static string nSpace = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.Substring(0, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.IndexOf('.'));

        // Declare process
        static Process p = new Process();

        // Declare target handle
        static IntPtr target = IntPtr.Zero;
       
        /// <summary>
        /// Delegate for thread enumeration
        /// </summary>
        delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);

        /// <summary>
        /// Delegate for child windows enumeration
        /// </summary>
        public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

        /// <summary>
        /// Sends custom message to target handle (hWnd)
        /// </summary>
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SendMessage(int hWnd, int Msg, int wparam, int lparam);
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);

        /// <summary>
        /// Enumerates child windows
        /// </summary>
        [System.Runtime.InteropServices.DllImport("user32")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

        // Constant for GETTEXT message
        private const uint WM_GETTEXT = 0x000D;

        // Constant for GETTEXTLENGTH message
        private const int WM_GETTEXTLENGTH = 0x000E;

        // Constant for SETTEXT message
        private const uint WM_SETTEXT = 0x000C;

        /// <summary>
        /// Enumerates windows in the thread
        /// </summary>
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);

        /// <summary>
        /// Enumerates Handles from a given process
        /// </summary>
        static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
        {
            var handles = new List<IntPtr>();

            foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
            {
                EnumThreadWindows(thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
            }

            return handles;
        }

        /// <summary>
        /// Callback for enumerating windows
        /// </summary>
        private static bool EnumWindow(IntPtr handle, IntPtr pointer)
        {
            // Get handle from garbage collector
            GCHandle gch = GCHandle.FromIntPtr(pointer);

            // Declare list
            List<IntPtr> list = gch.Target as List<IntPtr>;
           
            // Check if there's something
            if (list == null)
            {
                // Throw exception
                throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
            }
           
            // Add to list
            list.Add(handle);
           
            // Exit with true (null would cancel)
            return true;
        }
       
        /// <summary>
        /// Returns list of IntPtr with child windows
        /// </summary>
        public static List<IntPtr> GetChildWindows(IntPtr parent)
        {
            List<IntPtr> result = new List<IntPtr>();
            GCHandle listHandle = GCHandle.Alloc(result);
            try
            {
                EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
                EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }
            return result;
        }

        /// <summary>
        /// Gets text of control by handle
        /// </summary>
        public static string GetControlText(IntPtr hWnd)
        {
            // String builder for title           
            StringBuilder title = new StringBuilder();

            // Size of string for holding title.
            Int32 size = SendMessage((int)hWnd, WM_GETTEXTLENGTH, 0, 0).ToInt32();

            // 0 = no title.
            if (size > 0)
            {
                title = new StringBuilder(size + 1);

                SendMessage(hWnd, (int)WM_GETTEXT, title.Capacity, title);
            }

            // Give control text back
            return title.ToString();
        }

        /// <summary>
        /// Initialization routine
        /// </summary>
        public static string Init(Icon icon)
        {
            // Start process
            try
            {
                // Start
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.FileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location) + ".exe";
                p.Start();

                // Loop until it's loaded
                while (p.MainWindowHandle == IntPtr.Zero)
                {
                    Thread.Sleep(10);
                    p.Refresh();
                }

                // Check for input
                bool inputSolved = false;

                // Resolve input textbox
                while (!inputSolved)
                {
                    foreach (var handle in EnumerateProcessWindowHandles(p.Id))
                    {
                        // Get child windows
                        List<IntPtr> handles = GetChildWindows(handle);

                        // Hunt for "input"
                        foreach (IntPtr h in handles)
                        {
                            if (GetControlText(h).ToLower() == "input")
                            {
                                // Set target
                                target = h;

                                // Clear it
                                SendMessage(target, WM_SETTEXT, IntPtr.Zero, String.Empty);
                               
                                // Set flag
                                inputSolved = true;
                            }
                        }
                    }

                    // Pause
                    Thread.Sleep(25);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            // Exit
            return string.Empty;
        }

        /// <summary>
        /// Receives message from the framework
        /// </summary>
        public static string Receive(string sender, string type, string msg)
        {
            // Split by new line
            string[] input = msg.Split(Environment.NewLine.ToCharArray());

            /* Discriminate message */

            // Check if OK msg
            if (input[0] == "K")
            {
                // TODO: Process it
            }
            else
            {
                // Clear target
                SendMessage(target, WM_SETTEXT, IntPtr.Zero, String.Empty);

                // Send latest message
                SendMessage(target, WM_SETTEXT, IntPtr.Zero, input[0]);
            }

            // Exit
            return string.Empty;
        }

        /// <summary>
        /// Sends message to the framework
        /// </summary>
        public static string Send(string msg)
        {
            // Try to send message
            try
            {
                // Send
                object ret = Assembly.GetEntryAssembly().GetType("BetSoftware_Loader.Comm").GetMethod("Receive", BindingFlags.Static | BindingFlags.Public).Invoke(null, new object[] { nSpace, type, msg });

                // Return
                return (string)ret;
            }
            catch (Exception)
            {
                // Let it fall through           
            }

            return string.Empty;
        }
    }
}
#647
Community Software / Re: Help on VB.net
January 15, 2014, 07:17:36 PM
http://www.youtube.com/watch?v=7vftKmfP1Hg


As you can see, there's no special modification required on the program to interact with the framework when using the TextBox connector.
#648
Community Software / Re: Help on VB.net
January 15, 2014, 07:11:24 PM
You can experience the connector in action by using this attached archive:

[attachmini=1]

(Video to illustrate usage to follow)
#649
Community Software / Re: Help on VB.net
January 15, 2014, 05:27:47 PM
Hi, just like you have it it's OK.

The idea being I do the ground work so you guys have it easy.




Look at this code:

Code (vbnet) Select
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

        ' TextBox1.Text = Latest framework input ...you can do as you please with it.
        Me.Text = TextBox1.Text

End Sub


You simply use the "TextChanged" event to obtain input.

You don't have to do anything else on your side.
#650
Community Software / Re: Help on VB.net
January 15, 2014, 04:30:41 PM
Thanks Stef, I meant attach the resulting .exe program (zipped  O:-) )

You'll notice connecting to the framework this way is really easy!
#651
Community Software / Re: Help on VB.net
January 15, 2014, 02:47:54 PM
Guys can you please replicate this form in your VB.NET?

[attachimg=1]

Just a textbox with the word "input" in it.

Kindly attach it to your post. Thank you.
#652
OK, testing it.
#653
Sure, I'm currently compiling it under VB.NET for Pockets and Stef.

File must be named:

TextBox_Connector.exe
#654
By all means let's move to VB6!




Can you please replicate this itsy-bitsy form in VB6?

Just a textbox with the word "input" in it.

[attachimg=1]




This is a most-generic connector, works in every programming language capable of using a Textbox (99.9% of all GUI-capable languages! :thumbsup: )
#655
Quote from: esoito on January 15, 2014, 04:23:15 AM
There's a problem right away.  No idea how to do that.

Reading from here:

http://justbasic.conforums.com/index.cgi?board=novice&action=display&num=1175250727

It seems that justbasic isn't too easy when dealing with GUI. I find the code a somewhat messy. Guess I'll have to make the GUI code myself first to try.

Hooking into console mode can prove a long winding road. Internally, a Textbox is just a window which can be referred to by handle and "messed with" more easily via standard windows messages.
#656

Quote from: esoito on January 15, 2014, 04:15:35 AMAnd what of 'bot mode'?

Yes, it's supported. The connector itself is NATIVE to the framework, which means it allows the foreign program to work as a tracker, tester, clicker and bot  :thumbsup:
#657
I have a very straightforward input-only connector ready. The output part is actually more cumbersome to work with (but we'll get there).

When you have time please make a GUI program with a TextBox with the word "input" in it.

You receive the last number from the framework in the TextBox and make as you wish with it.
#658
Yes, the whole idea of the connector module is to "fake" it as being just another native framework module, loading equally, doing the same input/output routines so it becomes part of it.


This way disparate technologies can work seamlessly.
#659
OK, starting right now, our official BetSoftware bundle releases are paid :nod:
#660
Well max, this is a risk I'm going to take for the sake of my own tranquility and productivity.

Operating closed-source, keeping technologies as secrets, treating your users like thieves and worsening their usage experience in the name of retaining exclusive rights is actually more work and burden for the programmer.

I'll just ditch it away to focus on what interests me: developing.

I know appreciative supportive users and fellow developers are going to be happy. That's enough for me.