Solved

How call win32 func using pointer?

Posted on 2002-05-23
9
230 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 230
C++ assignment question 7 237
How to link to a static lib in Visual Studio C++ 2010 Express/ Console applications 2 106
max float value 3 53
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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