Sunday, March 28, 2010

Open mailto links in GMail with Google Chrome, Firefox and maybe Safari.

Here is a simple bookmarklet that when clicked will turn all mailto: links on a webpage into javascript pop-up windows for a gmail compose message with the to, subject and body filled out. The main thing is that it works with craigslist. :)

For those looking for a simple solution, just drag this link onto your bookmark bar:
GMail Mailto

To use it, open a webpage with a mailto link on it and then click the bookmarklet. Now when you mouse-over a mailto link you will notice that it is now a javascript function instead. Clicking it will open up a GMail compose window.

For those interested in the code:

If anyone knows how GMail handles other fields like CC, BCC, etc. let me know and I'll add the code to the bookmarklet. I can make some educated guesses, but without a webpage to test against i don't want a broken script posted on this blog.

Tuesday, July 14, 2009

Java Runtime Dispatch of a Deserialized Class

So I have a class that contains some data. I want to be able to serialize the object and send it over to another thread/process/computer and have the receiver execute a Process function overloaded to accept the class as an argument. The problem is that the receiver may not have an overload implemented, in which case I want the message to just be dropped.
Before I get into the details on the solution I came up with to deal with this scenario I should probably explain why I want this. The goal is to have a fairly generalize broadcaster / listener setup where a thread can subscribe to another thread and anytime anything interesting happens in the subscribed thread it will broadcast a message out to all it's listeners. Sometimes, the listener will care about the particular message and want to enter some handling function when it receives the message but other times the listener doesn't care about a particular method. Also, if a new method is implemented in the broadcaster I don't want all listeners to break until I get around to implementing a processing function.
Enter Java hacks.
The code is written for Android, but those of you working with Vanilla Java can think of a Bundle as a Serialized object. The same code could be used for serialized objects instead of bundled objects with minimal changes. We start off by figuring out the name of the class that was serialized. In the case of my bundled object the class name is just a string inside the bundle. For a serialized object you would deserialize it and call object.getClass().getClassName(). Once we have the class name we then want to instantiate one of those classes on the receiving side. For a bundle, this means calling a custom constructor that takes the bundle as a parameter and rebuilds the class from that. For a serialized object you don't have to do anything as the serialization system takes care of reconstruction for you.
Now for the interesting part, taking an unknown object (as far as the source code is concerned) and entering into some custom handling for whatever type of object it really is. To do this the receiver has an overloaded Process function for each of the messages that he wants to handle. We then search the receiving class for a Process function that takes our message object as a parameter. If it doesn't find it an exception will be thrown which can be caught and the packet dropped if you like, or perhaps some kind of error to the developer. If it is found that Process function is then called with the object passed in as a parameter which now takes us back into compile-time checked land where we no longer have to deal with generic "objects".
And here is the code for using Serializables.

Friday, February 13, 2009

Hooking Direct3D9 with C#.

So I wanted to hook Direct3D9 and override some of the calls with my own so I could either track the call itself or even change the incoming parameters or the returned results.  There are a number of uses for this type of thing ranging from profiling, cheating in 3D games to full screen monitor spanning.
This sort of thing has been done before in C++ many times and there are a number of sources of information and samples on how to do it such as over at GameDev.net.  However, I wanted to be able to write my code in C# both because I felt it was time to learn the language a little bit and I have worked with C++ enough to know that I don't really want to deal with all the headaches associated with such a low level language.
First off, I needed to be able to hook a Direct3D9 application with C#.  Luckily, there is a free (and extremely well documented) library available that does just that called EasyHook.  Getting it up and running was quite easy since it came with a sample application that did almost exactly what I wanted, it just hooked CreateFile in kernel32.dll instead of Direct3DCreate9 in d3d9.dll which was a simple change.
So now anytime Direct3DCreate9 was called by the hooked application instead of calling the real Direct3DCreate9 it would instead call my C# function, MyDirect3DCreate9.  Initially, MyDirect3DCreate9 would call the real Direct3DCreate9 (in d3d9.dll) and return whatever result the real one returned (as an IntPtr).
Of course , Direct3DCreate9 returns an IDirect3D9 interface which then does all the work.  At this point things get tricky because I wanted to be able to override the member funtions of IDirect3D9, not just the exported functions in d3d9.dll (of which there are only a few).  My initial thought was to use the COM interop services provided in the .NET framework and implement IDirect3D9 on my own.  My IDirect3D9 object could then instantiate a real IDirect3D9 object (from d3d9.dll) and call those functions as necessary.
Unfortunately, after much trial and error I was unable to get such a system working.  I think the main problem stems from the fact that Direct3DCreate9 needed to return an IDirect3D9 object to the calling native code (the hooked game) and it wasn't too happy getting a managed IDirect3D9 implementation instead.
Then I came across this site which, although a total hack, allowed me to do exactly what I wanted.  Basically, we know what the IDirect3D9 virtual function table has in it because we have the C++ header file d3d9.h, which defines exactly what the functions look like.  In order to fool the calling application into thinking it has a real IDirect3D9 object all we have to do is provide it with an object containing a virtual function table containing 17 members (as seen in d3d9.h) all of which have signitures that it is familiar with.
First things first we create a real IDirect3D9 object and take a look at it's virtual function table.  We can't modify the original table because it's protected by the OS but we can point the IDirect3D9 at a different virtual function table.  So what we do is create our own virtual function table in unprotected memory and copy all the function pointers from the original into it.  Then we tell IDirect3D9 to point at our custom table instead of the real one.
Now that we have a writable virtual function table we can then change some of those functions to point at our own C# functions instead.  Those C# functions can then call into the original function table when we want to call the real IDirect3D9 functions!
Here are the DllImport statements for calling into kernel32 (for memory management) and d3d9.  You will notice that I have also created an IDirect3D9 structure in C# that has a signature that matches the native IDirect3D9 signature (which only has an array of function pointers).  This will allow me to receive an IDirect3D9 object from native code and get to the virtual functions from managed code.
Here is my custom IDirect3D9 class.  It's not really an IDirect3D9 implementation but instead it's a container where I keep all my IDirect3D function overrides, store the native IDirect3D9 object and manage the virtual function table.
And here is my injection code, (some of the remoting stuff removed for brevity).