Solved

How call win32 func using pointer?

Posted on 2002-05-23
9
227 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:mortonsw
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Ooops, and, of course, don't forget to 'FreeLibrary()' when you are done...
0
 
LVL 1

Author Comment

by:mortonsw
Comment Utility
thanks jkr, i edited the question though and you probably read the orig one.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, I was wondering already :o)

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
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
 
LVL 1

Author Comment

by:mortonsw
Comment Utility
-hiding the func call from a disassemble.
0
 
LVL 22

Accepted Solution

by:
ambience earned 100 total points
Comment Utility
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
 
LVL 22

Expert Comment

by:ambience
Comment Utility
BTW can you really stop a hacker that way ?????? Is this enough !!
0
 
LVL 1

Author Comment

by:mortonsw
Comment Utility
Hardly; Just one trick in a bag.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now