Solved

How call win32 func using pointer?

Posted on 2002-05-23
9
229 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TTimer no longer functioning 7 91
Fast, free, private online code repository / editor 7 97
ASP.net build a IF/Then Walkthrough Guide 1 211
Android development question 2 37
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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