Thanks for the replies everyone, after doing a bit more research I have finally solved the access violation error and it functions properly. The problem was with the stack, when the code passes to my function various stuff is pushed onto the stack by the compiler (it pushes more onto the stack...