Page 1 of 1

Replace internal function

Posted: Tue Mar 22, 2022 4:11 am
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...

Re: Replace internal function

Posted: Tue Mar 22, 2022 5:17 am
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...

Re: Replace internal function

Posted: Tue Mar 22, 2022 3:49 pm
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.

Re: Replace internal function

Posted: Tue Mar 22, 2022 10:24 pm
by jacdelad
Sorry for my confusion. is that what you want even possible?

Re: Replace internal function

Posted: Tue Mar 22, 2022 11:04 pm
by chi
You could try API Hook Engine to redirect API calls to your own functions...

Re: Replace internal function

Posted: Wed Mar 23, 2022 12:54 am
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.

Re: Replace internal function

Posted: Thu Mar 31, 2022 12:55 pm
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.

Re: Replace internal function

Posted: Thu Mar 31, 2022 1:12 pm
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!

Re: Replace internal function

Posted: Wed Apr 06, 2022 12:58 pm
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

Re: Replace internal function

Posted: Wed Apr 06, 2022 1:19 pm
by Rinzwind
@holz
Wrong thread? All nice and well, but fail to see what that has to do with this topic.

Re: Replace internal function

Posted: Wed Apr 06, 2022 1:34 pm
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.