Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Creating a handle for an object? How? (http://www.go4expert.com/forums/creating-handle-object-t25685/)

alirezan 3May2011 02:10

Creating a handle for an object? How?
 
Hi guys

I have these function definitions and I'm supposed to write the functions. I have trouble understanding a part of it. I will explain.
Quote:

Opentimer
1 (vt_Time) Expiry time, relative to now, or VMS_UNSET to leave timer unset
return (vMSHnd) Handle to the new timer
Closetimer
1 (vMSHnd) Handle to the timer
return (vt_bool) TRUE = operation succeeded
IstimerExpired
1 (vMSHnd) Handle to the timer
return (vt_bool) TRUE = expired
Unsettimer
1 (vMSHnd) Handle to the timer
return (vt_bool) TRUE = operation succeeded
Resettimer
1 (vMSHnd) Handle to the timer
2 (vt_Time) see arg#1 for timerOpen(), if VMS_UNSET then behaves same as timerUnSet()
return (vt_bool) TRUE = operation succeeded
The create function is supposed to return a handle to a timer. I am not sure how I'm supposed to do that.

I am planning on creating a class and in the implementation of the create function, I will set a variable to current time (using gettickcount () ) and set the expiry variable to what's passed in. In the isExpired function, I will compare the values against current time (again using gettickcount () ) and determine if timer is expired or not.

But I don't know how to create a handle for each timer the way the function definitions require me to. Any ideas?

Thank you

xpi0t0s 3May2011 15:30

Re: Creating a handle for an object? How?
 
Not enough info. What OS? Are the timers implemented by the OS or have ou implemented them?

A handle is just something the computer uses to be able to identify an object. It could be a pointer, or it could just be a number that indexes into a pointer or object table. For example:
1 "hello"
2 "world"
3 "foo"
4 "bar"

1-4 could be considered "handles" in this respect. The exact meaning of handle in your situation will depend on the situation, for example if these are Windows handles as implemented by the WinAPI, then the HANDLEs returned by CreateTimer() et al would count as valid handles that could be returned by your functions, unless for any reason you wanted to obfuscate the actual Windows HANDLE values themselves, in which case you'd want to create a lookup table so that your program could deduce the Windows HANDLE from the handle passed in, e.g.
1 Windows-HANDLE-1
2 Windows-HANDLE-2
3 Windows-HANDLE-3

then your program would get a Windows HANDLE and bung it in this table, return as a handle the lookup value, then on each entry to your library you would take the given handle (1-3) and lookup the actual Windows HANDLE which you would then use to call the Windows function.

Bit rambly but I hope that's clear ;-)


All times are GMT +5.5. The time now is 01:30.