Replace internal function
-
- Enthusiast
- Posts: 224
- Joined: Sat Jul 07, 2018 6:50 pm
Replace internal function
Some PB functions call the standard functions from msvcrt.dll (wcslen, wcsstr, memset etc).
I know that it's possible to replace them with an external library (for example asmlib can do this).
Is it possible to do this without a static library, by reassigning the internal msvcrt function to the inline assembler analog in the code?
Maybe with a macro or some other way...
I know that it's possible to replace them with an external library (for example asmlib can do this).
Is it possible to do this without a static library, by reassigning the internal msvcrt function to the inline assembler analog in the code?
Maybe with a macro or some other way...
Re: Replace internal function
Yes, it is:
This example automatically adds the HighQuality constant whenever a font is loaded. Like this you can redirect any function as you wish. But be careful...
Code: Select all
Procedure LoadFontHQ(ID.i,Name.s,Height.l,Style.l=0)
ProcedureReturn LoadFont(ID,Name,Height,Style|#PB_Font_HighQuality)
EndProcedure
Macro LoadFont(ID,Name,Height,Style=0)
LoadFontHQ(ID,Name,Height,Style)
EndMacro
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
-
- Enthusiast
- Posts: 224
- Joined: Sat Jul 07, 2018 6:50 pm
Re: Replace internal function
jacdelad, thank you.
Maybe I wasn't clear about what I wanted.
PeekS internally call wcslen here.
I want replace internal call to wcslen with call to my inline asm code (not only when using PeekS, but also for any other functions calling wcslen within itself).
Asm lib can redefine such calls (strlen, strcmp, memcpy, memset etc) if ASMLIB_OVERRIDE_STANDARD_LIBRARY is set.
But this implemented inside .lib code (did not check how this is implemented, maybe just importing functions with the same name).
I suspect that this can be implemented directly in the application code.
Maybe I wasn't clear about what I wanted.
Code: Select all
a$="abc"
b$ = PeekS(@a$, -1)
I want replace internal call to wcslen with call to my inline asm code (not only when using PeekS, but also for any other functions calling wcslen within itself).
Asm lib can redefine such calls (strlen, strcmp, memcpy, memset etc) if ASMLIB_OVERRIDE_STANDARD_LIBRARY is set.
But this implemented inside .lib code (did not check how this is implemented, maybe just importing functions with the same name).
I suspect that this can be implemented directly in the application code.
Re: Replace internal function
Sorry for my confusion. is that what you want even possible?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: Replace internal function
You could try API Hook Engine to redirect API calls to your own functions...
Et cetera is my worst enemy
-
- Enthusiast
- Posts: 224
- Joined: Sat Jul 07, 2018 6:50 pm
Re: Replace internal function
From docs:did not check how this is implemented, maybe just importing functions with the same name
If two function libraries contain the same function name then the linker will take the function from the library that is linked first.
The override method will replace not only the function calls you write in the source code, but also function calls generated implicitly by the compiler as well as calls from other libraries.
The override method may fail if the standard library has multiple functions in the same module. If the standard library has two functions in the same module, and your program uses both functions, then you cannot replace one without replacing the other. If asmlib replaces one, but not the other, then the linker will then generate an error message saying that there are two definitions of the replaced function.
So it's a little bit tricky. Selfhooking can be one of the workarounds It's just not the most convenient way.
Re: Replace internal function
Isn't this Ok?
In Test() you can place your inline asm.
Code: Select all
Procedure.s Test(Memory.i, Length.i=0, Format.i=0)
ProcedureReturn "test"
EndProcedure
Macro PeekS(Memory, Length=0, Format=0)
Test(Memory, Length, Format)
EndMacro
a$="abc"
b$ = PeekS(@a$, -1)
Debug b$
-
- Enthusiast
- Posts: 224
- Joined: Sat Jul 07, 2018 6:50 pm
Re: Replace internal function
Yes it's ok. It's just a "local" solution for one particular function. NoteIsn't this Ok?
I want replace internal call to wcslen with call to my inline asm code (not only when using PeekS, but also for any other functions calling wcslen within itself)
For example this code also call wcslenThe override method will replace not only the function calls you write in the source code, but also function calls generated implicitly by the compiler as well as calls from other libraries
Code: Select all
a$ = "12345"
- holzhacker
- Enthusiast
- Posts: 123
- Joined: Mon Mar 08, 2010 9:14 pm
- Location: "Mens sana in corpore sano"
- Contact:
Re: Replace internal function
Hi, take a look at mk-soft's ScaleGadgets module, just fantastic https://www.purebasic.fr/english/viewtopic.php?t=71823, I changed the CanvasGadget to add the #PB_Canvas_Container flag and also the OpenWindows flag so when in the Form Design I set the #PB_Windows_Borderless flag if I create a ContainerGadget instead of a window, with this I can use several forms created in different windows in a single initial form.
Another customization I made was to create the HideGadget function after loading the module, and with SetGadgetData to signal if the Gadget is hidden, so I know which container is active at a given time when the software is being used.
The version I adapted is v0.27
Another customization I made was to create the HideGadget function after loading the module, and with SetGadgetData to signal if the Gadget is hidden, so I know which container is active at a given time when the software is being used.
The version I adapted is v0.27
Re: Replace internal function
@holz
Wrong thread? All nice and well, but fail to see what that has to do with this topic.
Wrong thread? All nice and well, but fail to see what that has to do with this topic.
- holzhacker
- Enthusiast
- Posts: 123
- Joined: Mon Mar 08, 2010 9:14 pm
- Location: "Mens sana in corpore sano"
- Contact:
Re: Replace internal function
Hello @Rinzwind, how is everything?
As I said STUDY the fantastic module ScaleGadgets, IT REPLACES THE INTERNAL FUNCTIONS that create the gadgets... if this has nothing to do with the topic I don't know what does.
In the rest I explained briefly how I used it to optimize the form designer, just to give an example of using the module and the techniques it contains.
Code: Select all
Wrong thread? All nice and well, but fail to see what that has to do with this topic.
In the rest I explained briefly how I used it to optimize the form designer, just to give an example of using the module and the techniques it contains.