How call win32 func using pointer?

I want to store the address of a win32 function (for example GetLocalTime) in a pointer var and then use the pointer var to call GetLocalTime. While assigning the offset of GetLocalTime to pointer var, I would like to add in an additional value like a random number, say 37, which I'll subtract back off before calling. My C/C++ is pretty weak but in ASM it looks like:

RNDNUM    = 37
mov       [ptrGLT],offset GetLocalTime + RNDNUM
sub       [ptrGLT],RNDNUM
call      [ptrGLT],offset SystemTime
LVL 1
mortonswAsked:
Who is Participating?
 
ambienceConnect With a Mentor Commented:
typedef void  (__stdcall *FPTR)( LPSYSTEMTIME);


int main()
{
     SYSTEMTIME ti;
     
     FPTR ptr = GetLocalTime;
     ptr = (FPTR)((DWORD)ptr + (DWORD)37);
     ((FPTR) (((DWORD)ptr) - (DWORD)37 ))(&ti);

     cout << ti.wYear << endl;

     return 0;
}

Hope this helps
0
 
jkrCommented:
It is pretty straight foward.

First you need a 'typed function pointer', so the compiler knows about the function signature:

typedef VOID ( *FPTR_GetLocalTime)( LPSYSTEMTIME);

// Load dll

HINSTANCE hDll = LoadLibrary ( "kernel32.dll");

if ( !hDll) {

 // error, though _very_ unlikely
}

// get the entry point
FPTR_GetLocalTime pGetLocalTime = ( FPTR_GetLocalTime) GetProcAddress ( hDll, "GetLocalTime");

if ( !pGetLocalTime) {

 // error, though _very_ unlikely
}

// make the call

SYSTEMTIME st;

pGetLocalTime ( &st);
0
 
jkrCommented:
Ooops, and, of course, don't forget to 'FreeLibrary()' when you are done...
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
mortonswAuthor Commented:
thanks jkr, i edited the question though and you probably read the orig one.
0
 
jkrCommented:
Yes, I was wondering already :o)

0
 
fl0ydCommented:
Just out of curiostiy: what exactly are you trying to do? You got me a bit confused with this adding/subtracting of a random offset. Some sort of copy protection, maybe?
0
 
mortonswAuthor Commented:
-hiding the func call from a disassemble.
0
 
ambienceCommented:
BTW can you really stop a hacker that way ?????? Is this enough !!
0
 
mortonswAuthor Commented:
Hardly; Just one trick in a bag.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.