Help in C#

07/20/2012 07:20 TheChinStrap#1
Hello, all...
I've been working on a few tools, as some of the available tools either don't fit my needs or are outdated. (Just to clarify, I am in no way denoting anyone's work or making accusations of inadequacy. I simply needed something tailored a bit more to my needs and something that works a bit better with the 7.4 files.) Currently I am working on a gm tool and ran into a bit of an issue. I know there are a few very talented C# programmers in this community who have achieved this in the past and thought I might give it a shot. (please don't flame... I am at least trying which is more than you can say for most of the help threads in this forum.) Here is my issue:

The GM-Tool passes strings to Herlock using "sendmessage", as seen here:
Code:
Process[] herlock = Process.GetProcessesByName("CaptainHerlockServer");
if (herlock.Length == 0) return;
if (herlock[0] != null)
{
    IntPtr child = FindWindowEx(herlock[0].MainWindowHandle, new IntPtr(0), "Edit", null);
    SendMessage(child, 0x000C, 0, "#warp(" + xPosition.Text + ", " + yPosition.Text + ", \""+listBox1.Text+"\")");
}
This works fine, however, I tried using "sendkeys" to pass the enter keystroke which, as you know, will only effect the active window.
Code:
SendKeys.Send("{ENTER}");
Someone suggested looking into spy++ and using "WM_KEYDOWN". Unfortunately I cannot afford a legit version of Visual studio and was wondering if anyone would be willing to share their insights or methods used in the past.

I will, of course, share everything once I am finished. Thanks in advance. :D
07/20/2012 07:24 TheOnlyOneRaskim#2
Code:
[DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("User32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll")]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);
        public const uint WM_SETTEXT = 0x000C;




foreach (Process pProc in Process.GetProcessesByName("CaptainHerlockServer))
                    {
                        string text = "#your_text_here";
                        IntPtr p = Marshal.StringToHGlobalAuto(text);
                        IntPtr tmp = FindWindowEx(pProc.MainWindowHandle, IntPtr.Zero, "Edit", null);
                        SendMessage(tmp, WM_SETTEXT, 0, p);
                        SendMessage(tmp, 258, 13, IntPtr.Zero);
                        Application.DoEvents();
                    }
This Code will send the message to each Gameserver wich is running in Backround/Foreground.
07/20/2012 07:38 Xijezu#3
Quote:
Originally Posted by TheOnlyOneRaskim View Post
Code:
[DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("User32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll")]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);
        public const uint WM_SETTEXT = 0x000C;




foreach (Process pProc in Process.GetProcessesByName("CaptainHerlockServer))
                    {
                        string text = "#your_text_here";
                        IntPtr p = Marshal.StringToHGlobalAuto(text);
                        IntPtr tmp = FindWindowEx(pProc.MainWindowHandle, IntPtr.Zero, "Edit", null);
                        SendMessage(tmp, WM_SETTEXT, 0, p);
                        SendMessage(tmp, 258, 13, IntPtr.Zero);
                    }
This Code will send the message to each Gameserver wich is running in Backround/Foreground.
If you remove the Application.DoEvent():
THATS MY CODE <.< xD
07/20/2012 07:45 TheChinStrap#4
Quote:
Originally Posted by TheOnlyOneRaskim View Post
Code:
[DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("User32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll")]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);
        public const uint WM_SETTEXT = 0x000C;




foreach (Process pProc in Process.GetProcessesByName("CaptainHerlockServer))
                    {
                        string text = "#your_text_here";
                        IntPtr p = Marshal.StringToHGlobalAuto(text);
                        IntPtr tmp = FindWindowEx(pProc.MainWindowHandle, IntPtr.Zero, "Edit", null);
                        SendMessage(tmp, WM_SETTEXT, 0, p);
                        SendMessage(tmp, 258, 13, IntPtr.Zero);
                        Application.DoEvents();
                    }
This Code will send the message to each Gameserver wich is running in Backround/Foreground.
Worked perfectly... Thanks a ton, Raskim. In the end I don't think i will use "foreach", as I may use an xml file to define the process, so that people with more than one server can simply customize the xml file and use the tool independently for each server. Thanks again for your help and quick reply!
I'll be sure to include your name in the credits when I release my work.

Also, for anyone else that may have been looking for this solution, you will also need the following:
Code:
using System.Runtime.InteropServices;

PS:
@ Raskim and Xijezu
You're both brilliant. My GM tool's layout is based on your's, Xijezu. Not trying to copy you or "jock your style", but it was layed out brilliantly. Hope you don't mind but if you do shoot me a pm.

___________________________

Raskim Rocks ;)
07/20/2012 14:34 M>M#5
Quote:
Originally Posted by TheOnlyOneRaskim View Post
Code:
[DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("User32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll")]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);
        public const uint WM_SETTEXT = 0x000C;




foreach (Process pProc in Process.GetProcessesByName("CaptainHerlockServer))
                    {
                        string text = "#your_text_here";
                        IntPtr p = Marshal.StringToHGlobalAuto(text);
                        IntPtr tmp = FindWindowEx(pProc.MainWindowHandle, IntPtr.Zero, "Edit", null);
                        SendMessage(tmp, WM_SETTEXT, 0, p);
                        SendMessage(tmp, 258, 13, IntPtr.Zero);
                        Application.DoEvents();
                    }
This Code will send the message to each Gameserver wich is running in Backround/Foreground.
it's like my code but I am using vb ;)
07/20/2012 18:35 TheChinStrap#6
Quote:
Originally Posted by M>M View Post
it's like my code but I am using vb ;)
Careful... Xijezu will tear your balls off for mentioning vb. XD

Edit: Ha ha ha! I can't believe he hit thanks for that one...
07/21/2012 15:59 M>M#7
Quote:
Originally Posted by TheChinStrap View Post
Careful... Xijezu will tear your balls off for mentioning vb. XD

Edit: Ha ha ha! I can't believe he hit thanks for that one...
alahm any saam :mad:

no one can say that vb is a noob programming language even Xijezu because it's not !

but yes i am with Xijezu that vb i not like C# or C++ :rolleyes: but it's still it's a great programming language to me ;)
07/21/2012 18:55 SilentBill#8
Yes it is. It's in the name. Visual Basic.
07/22/2012 10:37 TheChinStrap#9
I have to agree. Atleast in comparison to c#, vb falls a bit short. Easier to learn but limited as far as how extensible. It's not a bad language, but doesn't compare with C#
07/22/2012 19:34 Xijezu#10
Well, for some reason I'm always discussing about C# and vb... xD

Err, basically VB & C# is the same, they just have a different syntax.
Both of them need the .NET Framework, which has, orly?, the same content in vb aswell in c#.
The only difference is at a standard-"include": Microsoft.CSharp & Microsoft.VisualBasic.
For sure you can add both into your project, but usually just the necessary one is included.

Why I hate vb? Because this is not a good "Syntax" <.<
07/23/2012 03:12 SilentBill#11
I wouldn't say they're the same. VB has some pretty severe limitations and while technically you can do the same with VB what you can do with C#, it will take jumping through some hoops to get some functionality in VB that you can get from C# out of the box.
Also, VB will let you code "wrong" and not worry about it, which is why novice and beginner types flock to it.
And yes, I do dislike VB for its syntax too.
07/23/2012 12:36 TheChinStrap#12
Quote:
Originally Posted by SilentBill View Post
I wouldn't say they're the same. VB has some pretty severe limitations and while technically you can do the same with VB what you can do with C#, it will take jumping through some hoops to get some functionality in VB that you can get from C# out of the box.
Also, VB will let you code "wrong" and not worry about it, which is why novice and beginner types flock to it.
And yes, I do dislike VB for its syntax too.
Yeah... You have to reinvent the wheel to get anything worthwhile working. C# is definitely worth the little extra time to learn... Though I'm probably not a great example. XD