Author Topic: dll function not firing...  (Read 289 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Sr. Member
  • ****
  • Posts: 375
    • PluriBASIC
dll function not firing...
« on: June 15, 2019, 02:55:24 PM »

 I have this function in a dll (dllexample.dll):

Code: [Select]
extern
FUNCTION DLLFUNC() AS string, export

    RETURN "Hello world, from an exported DLL function."

END FUNCTION
end extern

And i have this code in the caller (caller.dll):

Code: [Select]
DECLARE FUNCTION DLLFUNC LIB "dllexample.dll" ALIAS "DLLFUNC" () AS STRING

FUNCTION PBMAIN() AS INT
   print DLLFUNC()
END FUNCTION

PBMAIN() ' invoke entry point

When i compile both (no errors during compilation) and invoke caller.exe, all i get is
a messagebox with the title O2H, and the message: dllexample.dll

What can i be doing wrong?






Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4097
    • Oxygen Basic
Re: dll function not firing...
« Reply #1 on: June 15, 2019, 04:28:12 PM »
Hi Brian,

Your code works provided you have $dll, and it is locatable from the app's launch path.
You must also have matching 32bit / 64bit.

Code: [Select]
$dll
$filename "t.dll"
uses rtl32

extern
FUNCTION DLLFUNC() AS string, export

    RETURN "Hello world, from an exported DLL function."

END FUNCTION
end extern

Note that unlike .exe files, DLLs do not have to be called .dll, plugins may use different file extension names. For instance, Steinberg VST plugins use .vst
« Last Edit: June 15, 2019, 04:39:07 PM by Charles Pegge »

Brian Alvarez

  • Sr. Member
  • ****
  • Posts: 375
    • PluriBASIC
Re: dll function not firing...
« Reply #2 on: June 15, 2019, 08:19:00 PM »
 Hello Charles, i got it working. I think i was interfering with the natural course of execution for Oxygen DLLs.

I had this code before "uses rtl32""

Code: [Select]
int ¤DLLREASON = 1
mov edx, [esp+8]
¤DLLREASON = edx

The reason i had for this is to send it to my own LIBMAIN procedure... But i am noticing this was useless, because Oxygen will
have taken action before the variable gets to my LIBMAN function...

 I can re-write that part of the code, but then it will be re-written once you release an update. I may forget to update that part of your code
once you do that, or it will be impossible for me to do so if user updates the rtl manually...

 Do you think something can be done about this? I would like to be able to override your code... but, how?

Brian Alvarez

  • Sr. Member
  • ****
  • Posts: 375
    • PluriBASIC
Re: dll function not firing...
« Reply #3 on: June 15, 2019, 08:21:25 PM »
Do you think it would be safe if i omit the $dll and i place new code for #ifdef dll ?

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4097
    • Oxygen Basic
Re: dll function not firing...
« Reply #4 on: June 16, 2019, 06:19:35 AM »
$dll is used to set up both the DLL entry point, and the DLL epilog (with o2 cleanup). Do you need to handle this stuff? You can call a libmain like main or winmain, but it's optional.

Brian Alvarez

  • Sr. Member
  • ****
  • Posts: 375
    • PluriBASIC
Re: dll function not firing...
« Reply #5 on: June 17, 2019, 03:30:27 PM »
can you please give me an example for that Charles? :)
Thanks!

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4097
    • Oxygen Basic
Re: dll function not firing...
« Reply #6 on: June 17, 2019, 09:02:10 PM »
Brian,

I'm reworking the RTLs and this is how The entry and exit code will be defined for DLLs (and EXEs.

Code: [Select]
  #file FileName independent

  %mode32bit

  '=========
  #ifdef dll
  '=========
  '
  'DLL EQUATES
  '
  % DLL_PROCESS_DETACH   0
  % DLL_PROCESS_ATTACH   1
  % DLL_THREAD_ATTACH    2
  % DLL_THREAD_DETACH    3
  % DLL_PROCESS_VERIFIER 4
  '
  mov edx,[esp+8] 'second param of DLLmain
  '
  select edx
    case DLL_PROCESS_ATTACH
      jmp fwd prolog
    case DLL_PROCESS_DETACH
      ! finish()
      finish()
      mov eax,0
    case DLL_THREAD_ATTACH
      mov eax,1
    case DLL_THREAD_DETACH
      mov eax,0
  end select
  ret 12

  def _epilog 'FOR DLL
  ====================
   
    ._end_

    lea edi,[ebp-8]   'temp list
    call delbuf
    mov eax,1
    mov esp,ebp : pop ebp : add esp,4 : pop edi : pop esi : pop ebx
    ret 12

    ._error_

    push 0x30
    "MISSING OR UNLOADABLE"
    push eax
    push ecx
    push 0
    call [ebx+472]
    lea edi,[ebp-8]   'temp list
    call delbuf
    mov esp,ebp : pop ebp : add esp,4 : pop edi : pop esi : pop ebx
    mov eax,0
    ret 12

  end def '_epilog


  '=========
  #else 'EXE
  '=========

    def _epilog 'FOR EXE
    ====================

    ._end_

    mov edi,eax 'hold exit value in edi
    push 0 'place for exit code
    mov eax,esp
    push eax
    call getModuleHandle
    push eax
    call GetExitCodeProcess
    mov eax,edi 'return exit value in eax
    call ExitProcess

    ._error_

    push 0x30
    "MISSING OR UNLOADABLE"
    push eax
    push ecx
    push 0
    call [ebx+472]
    mov eax,0
    jmp _end_

    end def


  '==============
  #endif 'DLL/EXE
  '==============


  '------
  Prolog:
  '======
...

After completing the prolog, setting up the Run-time library, it goes directly to your source code.

At the end of your source code, the finish procedure is inserted to cleardown o2's allocations. Then the epilog is invoked.
« Last Edit: June 17, 2019, 09:09:56 PM by Charles Pegge »

Brian Alvarez

  • Sr. Member
  • ****
  • Posts: 375
    • PluriBASIC
Re: dll function not firing...
« Reply #7 on: June 18, 2019, 09:44:01 AM »
Thanks Charles, I think i get it, but im still not sure if i will be able to capture DLL_PROCESS_DETACH, for customized dll cleanup...

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4097
    • Oxygen Basic
Re: dll function not firing...
« Reply #8 on: June 18, 2019, 10:44:49 AM »
You can add your own customised finish() procedure to the source code. It must invoke the terminate macro to cleardown o2. The compiler will then recognise it, and not insert the default finish().

sub finish()
...
...
terminate
end sub

Brian Alvarez

  • Sr. Member
  • ****
  • Posts: 375
    • PluriBASIC
Re: dll function not firing...
« Reply #9 on: June 19, 2019, 12:15:27 PM »
Okay, i will give it a try. :) Thanks!