isGadget(gadget)
BlitzPlus Forums/BlitzPlus Programming/isGadget(gadget)
| ||
Is there anyway of detecting if a gadget handle is valid with out crashing the computer. I'm allowing users to create scripts and I want to prevent a crash if they use the wronge value for a handle.... something like: bool isGadget(int gadHandle) Thanks! |
| ||
You could keep a list of valid gadget handles in a bank (for example) and compare the values to the value set in a script when the script runs, unless you have tens of thousands of gadgets the speed impact will be negligible although for a gui app it most definitely will not be noticeable. EDIT : kind of like this: when a new gadget is created add its handle to a type list , and when it is removed/deleted/freed remove the handle from the list. This list can be used to compare any script references to gadget handles with. |
| ||
Hmmmm... then I would need to wrap all the gui functions to do the tracking and checking. Right now the script language gives the user direct access to the gui functions.... Well, its one possible solutions. Is there anything available in the user32.dll do check gadgets.... Maybe I could create a function in C++ to check the x value of a gadget. Then using a try/catch I could detect the error and return false or true if ther is no error |
| ||
you could use the api IsWindow() found within user32.dll pass IsWindow() the HWND pointer returned from QueryObject() |
| ||
function isGadget(gadgetHndl) if isWindow(QueryObject(gadgetHndl, 1)) then return true return false end function testing this now.... |
| ||
function isGadget(gadgetHndl) if isWindow(QueryObject(gadgetHndl, 1)) then return true return false end function This crashes in blitz when an invalid gadgetHndle is used. Ill try porting it to a C++ DLL. Just not tonight. I need some zees. :) |
| ||
use FindWindow. decls API_FindWindow%(lpClassName%,lpWindowName$):"FindWindowA" API_IsWindow%(hwnd%):"IsWindow" blitz+ code window_test = CreateWindow("Test Window - Default (15)",0,500,400,100,0) Notify isWindow("Test Window - Default (15)") ; wait until the user closes one of the windows Repeat If WaitEvent()=$803 Then Exit Forever End ; bye! Function isWindow(window_title$) hwnd = API_FindWindow(0,window_title$) Return API_IsWindow(hwnd) End Function |
| ||
OK, created the dll..... it works fine.... but I need to use queryObject and THAT is what crashes it! arg! Heres the code: (C++ DLL) BBDECL bool BBCALL isGadget(HWND hWnd) { try{ return IsWindow(hWnd); } catch(...) { return false; } } in B+ if isGadget(QueryObject(handle, 1)) then ; blah, blah, blah endif Any ideas? |
| ||
Hmmmmm..... I guessed that gadget handles were actually pointers so when I pass the pointer to c++ and check the value I get an int.... if the gadget handle is for a valid gadget I get some value. If the gadget is freed then I get 0. I know that there is still a chance that the memory maybe written over so this is not perfect. More over if I pass a value less then 9764864 the dll will causes a crash. I guess 9764864 is either not a valid or is a protected memory address. And Im sure this too may not be the same on every computer..... C++ BBDECL bool BBCALL isGadget(int* ptr) { if (int(&ptr) < 9764864){ return false; } // int x = *ptr; // will still crash comp if &ptr > 9764864... even in try block try{ int x = *ptr; if (*ptr == 0){return false;} return true; } catch(...) { return false; } return false; } b+ win = createwindow(.....) print isGadget(win) ; prints 1 freegadget win print isGadget(win) ; prints 0 print isGadget(123) ; prints 0 So it works, NO CRASH. I just don't know how portable or reliable it is. |
| ||
Moore If i recall correct, when b+ creates windows or gadgets it first create's an internal struct to store these values. im unsure of the offset within the pointer returned where the windows HWND is stored. it should be possable to scan the passed value and check IsWindow on each pointer until a pre-defined offset is reached. kev |
| ||
Kev, Good idea! I did in fact look into that and EACH gadget has a slightly different structure. The offset for a window is not the same as an offset for a panel. Looks like my best choice is to limit what the user can do with the script language and wrap the GUI. Thanks all! |