Replace internal function

Windows specific forum
Everything
Enthusiast
Enthusiast
Posts: 224
Joined: Sat Jul 07, 2018 6:50 pm

Replace internal function

Post by Everything »

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...
User avatar
jacdelad
Addict
Addict
Posts: 1478
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Replace internal function

Post by jacdelad »

Yes, it is:

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
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...
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
Everything
Enthusiast
Enthusiast
Posts: 224
Joined: Sat Jul 07, 2018 6:50 pm

Re: Replace internal function

Post by Everything »

jacdelad, thank you.

Maybe I wasn't clear about what I wanted.

Code: Select all

a$="abc"
b$ = PeekS(@a$, -1)
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.
User avatar
jacdelad
Addict
Addict
Posts: 1478
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Replace internal function

Post by jacdelad »

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
User avatar
chi
Addict
Addict
Posts: 1034
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Replace internal function

Post by chi »

You could try API Hook Engine to redirect API calls to your own functions...
Et cetera is my worst enemy
Everything
Enthusiast
Enthusiast
Posts: 224
Joined: Sat Jul 07, 2018 6:50 pm

Re: Replace internal function

Post by Everything »

did not check how this is implemented, maybe just importing functions with the same name
From docs:
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.
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Replace internal function

Post by infratec »

Isn't this Ok?

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$
In Test() you can place your inline asm.
Everything
Enthusiast
Enthusiast
Posts: 224
Joined: Sat Jul 07, 2018 6:50 pm

Re: Replace internal function

Post by Everything »

Isn't this Ok?
Yes it's ok. It's just a "local" solution for one particular function. Note
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)
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
For example this code also call wcslen

Code: Select all

a$ = "12345"
But thanks anyway!
User avatar
holzhacker
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Mar 08, 2010 9:14 pm
Location: "Mens sana in corpore sano"
Contact:

Re: Replace internal function

Post by holzhacker »

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
Greetings and thanks!

Romerio Medeiros
romerio@gmail.com
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Replace internal function

Post by Rinzwind »

@holz
Wrong thread? All nice and well, but fail to see what that has to do with this topic.
User avatar
holzhacker
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Mar 08, 2010 9:14 pm
Location: "Mens sana in corpore sano"
Contact:

Re: Replace internal function

Post by holzhacker »

Hello @Rinzwind, how is everything?

Code: Select all

Wrong thread? All nice and well, but fail to see what that has to do with this topic.
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.
Greetings and thanks!

Romerio Medeiros
romerio@gmail.com
Post Reply