-
Notifications
You must be signed in to change notification settings - Fork 1
/
injlib.m
37 lines (33 loc) · 1015 Bytes
/
injlib.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Injection : NSObject
- (int) test:(int) x;
@end
@implementation Injection
+ (void) load
{
NSLog(@"------ [ injlib ] ------\n");
NSLog(@"injlib.dylib have already injected.");
// get class Test
Class clsTest = NSClassFromString(@"Test");
NSLog(@"clsTest: %p\n",clsTest);
// get methods
Method originalMethod = class_getInstanceMethod(clsTest,@selector(test:));
Method newMethod = class_getInstanceMethod(self,@selector(test:));
// get imps
originalImp = method_getImplementation(originalMethod);
IMP newImp = method_getImplementation(newMethod);
// set new imp to original method
method_setImplementation(originalMethod,newImp);
}
IMP originalImp;
int a=100;
- (int) test:(int) x
{
NSLog(@"[Injection test:]\n");
// call original method by original imp
int ret = (int)originalImp(self,@selector(test),x);
NSLog(@"original method returns: %i\n", ret);
return x+a;
}
@end