Sorry about the link, this one is correct. To send keys and what not there are a few things you need to know about the SendMessage() function of User32.dll.
SendMessage() can be imported using this code:
PHP Code:
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
You need to pass it the window handle of your Tibia client, which you can get (in VS2012, C#) using:
PHP Code:
Convert.ToUInt32(Process.GetProcessesByName("Tibia")[0].MainModule.BaseAddress.ToInt32())
Msg will probably be one of these for a key press:
PHP Code:
public const Int32 WM_CHAR = 0x0102;
public const Int32 WM_KEYDOWN = 0x0100;
public const Int32 WM_KEYUP = 0x0101;
public const Int32 VK_RETURN = 0x0D;
Note that if you use WM_KEYDOWN you should
always follow up with WM_KEYUP on the same keycode. The keycode should be inserted into wParam, and can be chosen from a list of
virtual key codes. I use a list like this, and select one as I need to:
PHP Code:
enum ArrowKeys : uint
{
left = 0x25,
up = 0x26,
right = 0x27,
down = 0x28
}
lParam is something I don't use, nor know how to, but I just declare it like this:
PHP Code:
IntPtr lParam = new IntPtr();
You can get the memory addresses of items in the inventory by having an empty backpack, adding a gold coin to it, then searching for 3031 in Cheat Engine or something, then you'll have a list, now change it to another item, and do "Next" search for the new item id, and the list should decrease, change back to the gp, search again, try another item and keep doing Next searches in this way until you've just got one address, now you've something to work from. If it was the first item in your inventory, you can probably work out from TibiaAPI source code the structure of the inventory at the time, and calculate what item you've found, then work your way backwards until you've got the start of the inventory structure, then you can iterate through it to get a list of items, their stacksize, and a few flags like movable, iscontainer, canuse, usetype etc... You'll need to know the length of each item in the inventory, and a few other things. I often find a good way to find out structures is to dump the output of the memory to a multi line textbox, and examine it in a text editor for \0 (terminator key, it tells you the end of a string), some integers which may be of relevance, e.g 60 (stack size, if you have 60 items in a stack), and so on... This may be difficult though, and time consuming, so you might be better off to pilfer the addresses and offsets from TibiaAPI.
An important piece of advice I can give you now is that using TibiaAPI is not necessarily a bad thing. It's not really challenging to read memory addresses and acquire information from the Tibia client at all, what's challenging is using the information in a way which is thread safe, efficient, and actually useful to the end user (even if it is yourself). The beauty of TibiaAPI is that it's not something you will just want to abuse, you may reach one of its many limitations, and decide that you want to take your work a step further. When you do so, you'll probably end up rewriting a large portion of TibiaAPI itself, in order to do more of what you want. The difficult things which you most likely won't be able to do at first are those such as writing your own A* search algorithm, for pathfinding. The A* search algorithm, if you don't know, is a pathfinding function which will take 2 locations, and find the shortest, or quickest path between the two points, considering a whole range of variables depending on your requirements. Those variables could be things as simple as a wall, blocking a certain path, or as complex as the amount of time in ms it takes you to walk over a certain tile. If your algorithm is well written, you'll be able to find the best path every time, without fail. The problem is most of the examples are also written in higher level codes such as C++, which makes them difficult to translate into a low level language like C#, and even more difficult to understand by someone who is new to programming, as they will use a whole variety of functions which don't exist in C#, and they will do different things in some very different ways. Perhaps the most difficult thing you'll find is actually making something which you can work on yourself. The biggest problem I face with all of my projects is that they reach a certain size and I just lose interest, say I've got 1000 lines of code in my reader class, I will need to modify it to read another value, I won't want to add the stuff because it might break the rest of the code. People say that annotation (comments) will help with this, but I often find that the best way to do it is using #region and #endregion, to declare regions of code which are grouped together based on what they do. I also find that creating multiple classes may at first seem rather pointless and time wasting, but having a class for reading, one for controlling, one for the GUI, one for storing object information, one for storing addresses, one for doing pathfinding, and one for anything which doesn't fall into any of the above categories will really help later on. Like at the minute my objects class only has 6 objects, 3 of which are completely empty, 1 of which contains 1 address which might even be incorrect. The thing is, in a months time I will be able to pick it up and look at it, and restart working on it, even if I've forgotten everything about it, it will all make sense still because of how it is written.
Another piece of advice, and please don't take it the wrong way that I'm throwing advice everywhere, is to get yourself a copy of C# 5.0 in a nutshell. I've got the 4.0 book in front of me and it's helped me countless times, I've actually used a function I hadn't used before, which is the switch function, really useful for testing variables against multiple values (especially strings). I saw the function in an example I found while googling the SendMessage() function, and didn't quite understand how it was being used, so I flicked to the index of C# 4.0 in a nutshell, found "switch", and looked at the page about it, it had a page and a half or so of information and example code which demonstrated perfectly clearly how to use the function according to the C# 4.0 standards. I then mentally high-fived myself and carried on working through the program.