Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Calling PB DLLs from other languages - Long Integer datatype

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calling PB DLLs from other languages - Long Integer datatype

    The following source code demonstrates at its simplest how to send and receive long integers from a DLL created with PB\DLL, called from various different languages.
    The DLL has just one function - PBFUNCTION, which accepts two parameters. The first parameter is the "sent" number, which you provide. When the function is invoked, the DLL will set the second parameter to 2 times the value of the first parameter, so this is the out or "receive" parameter. A better name for this function would be MultiplyByTwo instead of PBFUNCTION.
    I would like to specially thank Semen Matusovski and Tom Hanlin for steering me in the right direction with these other bizarre languages
    Also a special thanks to Sven Blumenstein for his excellent MASM32 version, cheers!
    Please feel free to post demo calling code from other languages!
    Code:
    [b]'PBDLL.BAS - The Powerbasic DLL we want to call from other languages[/b]
    #COMPILE DLL "pbdll.dll"
     
    FUNCTION LibMain(BYVAL hInstance   AS LONG, _
                     BYVAL fwdReason   AS LONG, _
                     BYVAL lpvReserved AS LONG) EXPORT AS LONG
             LibMain = 1   'success!
    END FUNCTION
     
    FUNCTION PBFUNCTION (BYREF NumberIn AS LONG, BYREF NumberOut AS LONG) EXPORT AS INTEGER
     MSGBOX "PBFUNCTION(" & TRIM$(STR$(NumberIn)) & ", " & TRIM$(STR$(NumberOut)) & ") called!"
     NumberOut = NumberIn * 2   'Return the number received multiplied by 2
     FUNCTION = 1               'Return 1 to indicate the functions success
    END FUNCTION

    [This message has been edited by Wayne Diamond (edited September 18, 2001).]
    -

  • #2
    Code:
    '[b]CALLPB.BAS - Call the DLL from Powerbasic[/b]
    #COMPILE EXE "callpb.exe"
     
    DECLARE FUNCTION PBFUNCTION Lib "pbdll.dll" ALIAS "PBFUNCTION" (BYREF NumberIn AS LONG, BYREF NumberOut AS LONG) AS LONG
     
    FUNCTION PBMAIN()
     Dim NumIn AS LONG, NumOut AS LONG, CallFunction AS INTEGER
     NumIn = 10
     CallFunction = PBFUNCTION(NumIn, NumOut)
     IF CallFunction = 1 THEN
        MSGBOX "NumIn = " & STR$(NumIn) & ", NumOut is now " & TRIM$(STR$(NumOut)) & " (should be twice the value of NumIn)"
     ELSE
        MSGBOX "Call failed"
     END IF
    END FUNCTION
    ------------------
    -

    Comment


    • #3
      Code:
      '[b]CALLVB.FRM - Call the DLL from Visual Basic[/b]
      Private Declare Function PBFUNCTION Lib "pbdll.dll" (ByRef NumberIn As Long, ByRef NumberOut As Long) As Long
        
      Private Sub Form_Load()
       Dim NumIn As Long, NumOut As Long, CallFunction As Integer
       NumIn = 10
       CallFunction = PBFUNCTION(NumIn, NumOut)
       If CallFunction = 1 Then
          MsgBox "NumIn = " & Str$(NumIn) & ", NumOut is now " & Trim$(Str$(NumOut)) & " (should be twice the value of NumIn)"
       Else
          MsgBox "Call failed"
       End If
      End Sub

      [This message has been edited by Wayne Diamond (edited September 16, 2001).]
      -

      Comment


      • #4
        Code:
        [b]// CALLDELPHI.DPR - Call the DLL from Borland Delphi[/b]
        {$APPTYPE CONSOLE}
        {$X+}
        program CallCRC32;
        uses
          SysUtils,
          Windows;
         
        Type
          longptr = ^longint;
         
        Var
          xRet: Integer;
          xNumIn, xNumOut: longptr;
          xResult: LongInt;
         
        Function PBFUNCTION(  NumIn: longptr; NumOut: longptr): Integer;
          stdcall; external 'PBDLL.DLL';
         
        begin
          xNumIn^ := 10;
          xRet := PBFUNCTION(xNumIn, xNumOut);
          xResult := xNumOut^;
          writeln('xResult = ', xResult, ' (should be 20)');
        end.
        ------------------
        -

        Comment


        • #5
          Code:
          [b]// CALLC.C - Call the DLL from C[/b]
          #include "windows.h"
          #include "stdio.h"
           
          typedef int __stdcall LPFNCALLDLL( long &NumberIn, long &NumberOut );
          int main(int argc, char* argv[])
          {
           HMODULE hLib;
           LPFNCALLDLL* lpCallDll;
           long NumberIn, NumberOut;
           hLib = LoadLibrary( "PBDLL.DLL" );
           if ( hLib == NULL )
           {
                  printf("Could not load PBDLL.DLL");
                  return FALSE;
           }
           lpCallDll = (LPFNCALLDLL*)GetProcAddress(hLib, "PBFUNCTION");
           if ( lpCallDll == NULL )
           {
                  printf("Could not find function in PBDLL.DLL");
                  FreeLibrary( hLib );
                  return FALSE;
           }
           int Result;
           NumberIn = 10;
           NumberOut = 0;
           Result = lpCallDll( NumberIn, NumberOut );
           FreeLibrary(hLib);
           printf("NumberIn is %d, NumberOut is now %d (should be twice the value of NumberIn)", NumberIn, NumberOut);
           return TRUE;
          }

          ------------------
          -

          Comment


          • #6
            Code:
            [b]; callasm.asm - Call the DLL from ASM (MASM)[/b]
                    .386
                    .model flat, stdcall
                    option casemap :none
             
                    include \masm32\include\windows.inc
                    include \masm32\include\user32.inc
                    include \masm32\include\kernel32.inc
             
                    includelib \masm32\lib\user32.lib
                    includelib \masm32\lib\kernel32.lib
              
                .data
                    szMsg           db "NumIn = %lu, NumOut is now %lu (should be twice the value of NumIn)", 0
                    szCap           db "Call PBDLL from ASM", 0
                    szLib           db "pbdll.dll", 0
                    szProc          db "PBFUNCTION", 0
             
                .data?
                    NumIn           dd ?
                    NumOut          dd ?
                    szBuffer        db 512 dup(?)
             
                .code
             
            callasm:
                mov NumIn, 10
                push offset szLib
                call LoadLibrary
                invoke LoadLibrary, addr szLib
                push eax
                invoke GetProcAddress, eax, addr szProc
                push offset NumOut
                push offset NumIn
                call eax
                call FreeLibrary
             
                invoke wsprintf, addr szBuffer, addr szMsg, NumIn, NumOut
                invoke MessageBox, 0, addr szBuffer, addr szCap, MB_OK
             
                invoke ExitProcess, eax
             
            
            end callasm
            ------------------
            Sven Blumenstein
            IT-Trainee at DECOMA Exterior Systems, Germany
            E-Mail (work): mailto:[email protected][email protected]</A>
            E-Mail (home): mailto:[email protected][email protected]</A>
            Programming with: PB/Dll, MASM, VB, VC++, LCC, VBA, WML

            [This message has been edited by Sven Blumenstein (edited September 18, 2001).]

            Comment

            Working...
            X