Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How call win32 func using pointer?

Posted on 2002-05-23
9
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7030532
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
ID: 7030533
Ooops, and, of course, don't forget to 'FreeLibrary()' when you are done...
0
 
LVL 1

Author Comment

by:mortonsw
ID: 7030546
thanks jkr, i edited the question though and you probably read the orig one.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 7030559
Yes, I was wondering already :o)

0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7030576
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
ID: 7030887
-hiding the func call from a disassemble.
0
 
LVL 22

Accepted Solution

by:
ambience earned 400 total points
ID: 7031165
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
ID: 7031168
BTW can you really stop a hacker that way ?????? Is this enough !!
0
 
LVL 1

Author Comment

by:mortonsw
ID: 7031287
Hardly; Just one trick in a bag.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 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