I am having a serious problem with my DLL development. I am developing DLL's for another
application to call and can not get them to work properly if called multiple times with differing
parameter values. Here is an example of the calling of the DLL function:
Code:
{---------- WORKING EL Indicator Code BEGIN ----------} VARS: Avg1(0); DefineDLLFunc: "C:\MyAvg.DLL", INT, "AvgMAIN", FLOAT, FLOAT, LPFLOAT; Value1 = AvgMAIN( Close, 5, &Avg1 ); Plot1( Avg1,"DLL_1"); {where the & indicates passing a pointer} {---------- WORKING EL Indicator Code END ----------}
call, nothing works properly:
Code:
{---------- NOT WORKING EL Indicator Code BEGIN ----------} VARS: Avg1(0), Avg2(0); DefineDLLFunc: "C:\MyAvg.DLL", INT, "AvgMAIN", FLOAT, FLOAT, LPFLOAT; Value1 = AvgMAIN( Close, 5, &Avg1 ); Value2 = AvgMAIN( Close, 15, &Avg2 ); Plot1( Avg1,"DLL_1"); Plot2( Avg2,"DLL_2"); {---------- NOT WORKING EL Indicator Code END ----------}
be a static array, as the function is called iteratively and passed a new data record. I think
that since this array is static, it is corrupting the second, third, etc. calls. I tried to
correct this, by breaking the function up into a main portion which handles all of the data
maintenance and a Calc portion which handles all of the computations. This did not work. Below
is the actual code from the DLL. If anything jumps out at you as to where I am going wrong, I
would be extremely appreciative if you could point out the problem to me.
Thanks in advance.
Regards,
Andrew Peskin
[email protected]
Code:
'---------- DLL CODE BEGIN ---------- #COMPILE DLL "C:\MyAvg.DLL" ' Compile to DLL rather than EXE. #DIM ALL ' Declare variables before use. #DEBUG ERROR OFF ' Use to Toggle Error DECLARE FUNCTION AvgCALC( PriceDAT() AS SINGLE, Length AS SINGLE) AS SINGLE FUNCTION LibMain( BYVAL hInstance AS LONG, _ BYVAL Reason AS LONG, _ BYVAL Reserved AS LONG) EXPORT AS LONG %TRUE = 1 ' Set Constant True %FALSE = 0 ' Set Constant False LibMain = 1 ' 1 tells WIN that load ' was a success. END FUNCTION FUNCTION AvgMAIN( BYVAL Price AS SINGLE,_ BYVAL Length AS SINGLE,_ BYVAL ReturnADR AS DWORD)_ EXPORT AS INTEGER STATIC PriceBUFF() AS SINGLE STATIC PriceDAT() AS SINGLE STATIC Newest AS LONG STATIC InitComplete AS LONG STATIC PassNum AS DWORD LOCAL ReturnVAL AS SINGLE LOCAL Count AS DWORD LOCAL SINPTR AS SINGLE PTR INCR PassNum IF ( InitComplete = 0 ) THEN REDIM PriceBUFF(Length-1) FOR Count = 0 TO ( Length - 1 ) PriceBUFF(Count) = 0 NEXT Count REDIM PriceDAT(Length-1) FOR Count = 0 TO ( Length - 1 ) PriceDAT(Count) = 0 NEXT Count InitComplete = 1 Newest = -1 PassNum = 1 END IF Newest = ( Newest + 1 ) MOD Length PriceBUFF(Newest) = Price IF ( PassNum >= ( Length + 1 ) ) THEN FOR Count = 0 TO ( Length - 1 ) PriceDAT(Count) = PriceBUFF( ((Newest + Length + Count) MOD Length ) ) NEXT Count ReturnVal = AvgCALC( PriceDAT(), Length ) AvgMAIN = 1 END IF SINPTR = ReturnADR @SINPTR = ReturnVAL END FUNCTION FUNCTION AvgCALC( PriceDAT() AS SINGLE,_ Length AS SINGLE)_ AS SINGLE LOCAL Count AS DWORD LOCAL Sum AS DOUBLE LOCAL ReturnVAL AS SINGLE Sum = 0# FOR Count = 0 TO ( Length - 1 ) Sum = ( Sum + PriceDAT( Count ) ) NEXT Count ReturnVAL = ( Sum / Length ) AvgCALC = ReturnVAL END FUNCTION ' Function WEP tells Windows to unload this DLL. This function ' definition is the same for any DLL. Certain other run-once ' code can be put here as well, like de-allocating objects. FUNCTION Wep (BYVAL nParameter AS INTEGER) EXPORT AS WORD Wep = 1 ' Tells WIN that unload ' was a success. END FUNCTION '---------- DLL CODE END ----------
Comment