Personal Development for Smart People Forums

Personal Development for Smart PeopleTM Forums


Go Back   Personal Development for Smart People Forums > Personal Development > Technology & Technical Skills
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Technology & Technical Skills Computer skills, hardware, software, internet topics, gadgets, programming


Welcome to the Personal Development for Smart People Forums, the place for lively, intelligent discussion of all personal growth issues -- physical, mental, financial, social, emotional, spiritual, and more.

You're currently viewing as a guest, which gives you limited read-only access. By joining our free community, you'll be able to post your own messages, access many members-only features, see the new messages posted since your last visit, and of course remove this header message. Registration is fast, simple, and free, so please join today.

If you arrived here from a search engine, you may want to explore the main site first, which includes hundreds of deep and insightful articles on a variety of personal development topics.

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2006, 06:29 AM
Senior Member
 
Join Date: Nov 2006
Posts: 305
Minsc is on a distinguished road
Default Function pointers instead of dlls in C++

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);
}
And this will read the file and execute the function:
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);
}
Some restrictions that I know of:

-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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-07-2006, 08:01 AM
Moderator
 
Join Date: Nov 2006
Location: Brisbane, Queensland, Australia
Posts: 58
Andrew Russell is on a distinguished road
Default

Holy cow!

That has to be the most horrific hack I have ever, ever seen Nicely done

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


All times are GMT. The time now is 10:10 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 by Pavlina LLC