PDA

View Full Version : need help for compiling these calc_methods



largo_winch
29-12-2011, 16:22
many thanks for trying to help eros.

re-organized the problem and show result with solution !

a) "calculatorDLL.bas":

#COMPILE DLL
#DIM ALL

%USEMACROS = 1
#INCLUDE "Win32API.inc"
#INCLUDE "CalculatorDLL.Inc

GLOBAL ghInstance AS DWORD

CLASS cTestCalculator AS COM
INTERFACE ImyTestCalculator : INHERIT IUNKNOWN

METHOD tAdd (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG)
tc = ta + tb
END METHOD

METHOD tSub (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG)
tc = ta - tb
END METHOD

METHOD tMul (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG)
tc = ta * tb
END METHOD

METHOD tDiv (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG) AS LONG
IF tb <> 0 THEN
tc = ta / tb
METHOD = %S_OK
ELSE
METHOD = %E_FAIL
END IF
END METHOD

END INTERFACE
END CLASS

SUB CreateInstance ALIAS "CreateInstance" (myJunk AS ImyTestCalculator) EXPORT
LOCAL obj AS ImyTestCalculator
SET obj = CLASS "cTestCalculator"
IF OBJPTR(obj) <> 0 THEN
SET myJunk = obj
END IF
END SUB

'-------------------------------------------------------------------------------
' Main DLL entry point called by Windows...
'
FUNCTION LIBMAIN (BYVAL hInstance AS LONG, _
BYVAL fwdReason AS LONG, _
BYVAL lpvReserved AS LONG) AS LONG

SELECT CASE fwdReason
CASE %DLL_PROCESS_ATTACH
'Indicates that the DLL is being loaded by another process (a DLL
'or EXE is loading the DLL). DLLs can use this opportunity to
'initialize any instance or global data, such as arrays.
ghInstance = hInstance
FUNCTION = 1 'success!
'FUNCTION = 0 'failure! This will prevent the EXE from running.
CASE %DLL_PROCESS_DETACH
'Indicates that the DLL is being unloaded or detached from the
'calling application. DLLs can take this opportunity to clean
'up all resources for all threads attached and known to the DLL.

FUNCTION = 1 'success!
'FUNCTION = 0 'failure!

CASE %DLL_THREAD_ATTACH
'Indicates that the DLL is being loaded by a new thread in the
'calling application. DLLs can use this opportunity to
'initialize any thread local storage (TLS).
FUNCTION = 1 'success!
'FUNCTION = 0 'failure!

CASE %DLL_THREAD_DETACH
'Indicates that the thread is exiting cleanly. If the DLL has
'allocated any thread local storage, it should be released.
FUNCTION = 1 'success!
'FUNCTION = 0 'failure!
END SELECT
END FUNCTION


b) "calculatorDLL.inc":

#IF NOT %DEF(%CalculatorDLLInc)
%CalculatorDLLInc = 1

INTERFACE ImyTestCalculator : INHERIT IUNKNOWN
METHOD tAdd (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG)
METHOD tSub (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG)
METHOD tMul (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG)
METHOD tDiv (BYVAL ta AS INTEGER, BYVAL tb AS INTEGER, BYREF tc AS LONG) AS LONG
END INTERFACE

#ENDIF


c) "test-myCalculator.bas":

#COMPILE EXE
#DIM ALL

#INCLUDE "Win32Api.inc"
#INCLUDE "CalculatorDLL.inc"

DECLARE SUB CreateInstance LIB "CalculatorDLL.dll" ALIAS "CreateInstance" (myJunk AS ImytestCalculator)

FUNCTION PBMAIN () AS LONG
DIM testC AS ImytestCalculator
DIM result AS LONG
CreateInstance testC
IF OBJPTR(testC) = 0 THEN EXIT FUNCTION
testC.tAdd(2,3,result) ' 5 ' result ok :)
MSGBOX FORMAT$(result)
END FUNCTION


my uncle frank have sent to me some minutes before solution he got from josé he said there was ... "AS COM" in "cTestCalculator" missing. he tried to help from his job at school too;) I was very frustrated last night about that little example, but that's a problem to myself not reading enough about methods and classes theme. sorry!

all correct examples I send as attachement.

bye, largo

ErosOlmi
30-12-2011, 13:57
Hi largo,

I was replying just now and you have deleted your post :cry:

Anyway, no problem.
I've found this interesting post at PB forum: http://www.powerbasic.com/support/pbforums/showthread.php?t=48630&highlight=NEWCOM+CLSID+LIB
Check it and I'm sure you will see how to implement your class/interface into a DLL.

The trick is to use: LET objvar = NEWCOM CLSID ClassID$ LIB DLLPath$
and use a temporary IDispatch variable as stated by José Roca

If you will re-post your code we can try together

Ciao
Eros

largo_winch
30-12-2011, 16:44
Check it and I'm sure you will see how to implement your class/interface into a DLL.

The trick is to use: LET objvar = NEWCOM CLSID ClassID$ LIB DLLPath$
and use a temporary IDispatch variable as stated by José Roca

thank you eros for link and help. I will check these new example too. See my first post with solution from josé's site, there was only .. "AS COM" in cTestCalculator missing.

addendum:


The biggest change was assigning NewCom to an IDispatch variable and then assigning it to an IInherited.

how to use GUID's and implement for this class-method example in my first post this will be another part for next time to study!

bye, largo