| | |||||||
| Technology & Technical Skills Computer skills, hardware, software, internet topics, gadgets, programming |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Senior Member Join Date: Nov 2006
Posts: 336
|
Something I'm screwing around with. Use this to extract a function: Code: //Broke some rules here...
#include <iostream.h>
#include <fstream.h>
char *blah="Blahhahah";
void fc() { cout << blah << blah[0] << blah[3] << blah[5]; }
typedef void (*fctype)();
fctype funcp;
int main() {
funcp=fc;
unsigned __int16 *fdat=(unsigned __int16*)funcp;
//Not sure how to determine this exactly but it's close to the file size of the exe.
void *nfunc=malloc(/*587370*/400000);
for (int x=0;x</*587370*/400000;x++) *(((char*)nfunc)+x)=*(((char*)funcp)+x);
ofstream exc("exc.exf", ios::out|ios::binary|ios::trunc);
exc.write((char*)nfunc, 400000);
exc.close();
while(1);
} Code: #include <iostream.h>
#include <fstream.h>
char *blah="Blahhahah";
void fc() { cout << "Orange"; }
typedef void (*fctype)();
fctype funcp;
int main() {
ifstream exc("exc.exf", ios::in|ios::binary|ios::ate);
int excs=exc.tellg(); exc.seekg(0);
void *fdat=(void*)(new __int8[excs]);
exc.read((char*)fdat, excs);
funcp=(fctype)fdat;
//funcp=(fctype)nfunc;
(*funcp)();
while(1);
} -String constants aren't stored -The second code's fc has to do something. -I can't get cin to work for strings, just cout. (although you could probably write another function to cin stuff and use that) -Calling other functions doesn't work but it can call itself. -I suppose a lot of this depends on the compiler. -I've never made a DLL and never directly used one in C++ so I don't really know if this is like one or not. -You can use function pointers to call functions. -Keep everything above the function declaration about the same... Last edited by Minsc; 11-07-2006 at 07:07 AM. |
| | |
| | #2 (permalink) |
| Member Join Date: Nov 2006 Location: Brisbane, Queensland, Australia
Posts: 70
|
Holy cow! That has to be the most horrific hack I have ever, ever seen Not only is it ludicrously compiler dependent, it's most likely operating system dependent and hardware dependent. Could easily be day-of-the-week dependent, for that matter. I sure hope it's a silly experiment and not an excuse to avoid DLLs - if your code looks like that, then it really is time to learn how to use DLLs. It's not really quite how DLLs work. You can have DLL functions dynamically bound in your own code using function pointers (this is how things like plugins work). But you need the OS to load the DLL into the right place, in the right way, and then you need to use the OS's function in order to extract the function pointer you need. You can also have them dynamically bound by the OS based on instructions inserted by the compiler (this is how things like user32.dll work), so that the functions themselves will look statically bound within your own code. |
| | |
| Bookmarks |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| Display Modes | |
| |
All times are GMT. The time now is 08:08 PM.




