Announcement

Collapse
No announcement yet.

Passing Arrays to DLLs

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

  • Passing Arrays to DLLs

    How can you pass an array to a DLL? I've tried using various methods, but each time it crashes ESignal. I've created a short test program to test the DLL and everything works correctly, but once I call the DLL from ESignal it crashes. I can pass integers and doubles and it works fine, but once I pass an array---crash!!! Below are the programs I've writen in PowerBasic for creating and testing the DLL, and the EFS Script:

    Code:
    '==============================================
    '  Generic DLL Template for PowerBASIC for Windows
    '==============================================
    
    #COMPILER PBWIN 9
    #COMPILE DLL "C:\Program Files\eSignal\MyTestArray"
    
    %USEMACROS = 1
    #INCLUDE "Win32API.inc"
    
    GLOBAL ghInstance AS DWORD
    
    '-------------------------------------------------------------------------------
    ' 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
            ghInstance = hInstance
            FUNCTION = 1   'success!
    
        CASE %DLL_PROCESS_DETACH
            FUNCTION = 1   'success!
    
        CASE %DLL_THREAD_ATTACH
             FUNCTION = 1   'success!
    
        CASE %DLL_THREAD_DETACH
            FUNCTION = 1   'success!
    
        END SELECT
    
    END FUNCTION
    
    '-------------------------------------------------------------------------------
    ' Exported function...
    '
    FUNCTION MyFunction1 ALIAS "MyFunction1" (BYREF myParam1() AS DOUBLE) EXPORT AS DOUBLE
                              
        ' test code
        FUNCTION = myParam1(2)  ' return value to calling program
    
    END FUNCTION
    Code:
    '=============================================='
    '  Test DLL Program
    '==============================================
    
    #COMPILER PBWIN 9
    #COMPILE EXE
    '--------------------------------------------------------------------
    DECLARE FUNCTION MyFunction1 LIB "C:\Program Files\eSignal\MyTestArray.dll" _
              ALIAS "MyFunction1" (BYREF Param1() AS DOUBLE) AS DOUBLE
    '--------------------------------------------------------------------
    
    FUNCTION PBMAIN () AS LONG
        
        LOCAL lRes AS SINGLE
        DIM Param1(5) AS DOUBLE
        
        Param1(0)=10
        Param1(1)=12
        Param1(2)=14
        
        lRes = MyFunction1(Param1())  ' call MyFunction1 in MyTestArray.dll
    
        MSGBOX "Exe result from MYTESTDLL.DLL:" + STR$(lRes)
    
    END FUNCTION
    Code:
    //EFS Script
    debugClear();
    
    var d = new DLL("MyTestArray.dll");
    var fpArray = new Array();
    var myArray = new Array();
    var bInit = false;
    
    function preMain() {
        setPriceStudy(false);
        setShowCursorLabel(false);
        setShowTitleParameters( false );
        
        setStudyTitle("DLL Test");
        setCursorLabelName("Test Value", 0);
    
        setDefaultBarFgColor(Color.red, 0);
        setPlotType(PLOTTYPE_LINE, 0); 
        setDefaultBarThickness(2, 0);
        setComputeOnClose();
        
        askForInput();
        
        var x=0;   
        
        fpArray[x] = new FunctionParameter("LineColor", FunctionParameter.COLOR);
        with(fpArray[x++]){
            setName("Line Color");
            setDefault(Color.green);
        }    
    
        fpArray[x] = new FunctionParameter("ViewValue", FunctionParameter.BOOLEAN);
        with(fpArray[x++]){
            setName("Display Cursor Labels");
            setDefault(true);
        }    
        
        fpArray[x] = new FunctionParameter("Thickness", FunctionParameter.NUMBER);
    	with(fpArray[x++]){
            setName("Line Thickness");
            setLowerLimit(1);		
            setDefault(2);
        }
    }
    
    d.addFunction("MyEFSCallName", DLL.DOUBLE, DLL.STDCALL, "MyFunction1", DLL.DOUBLEARRAY);
    
    var xVal = 0.0;
    
    function main(Thickness, LineColor, ViewValue) {
        if ( bInit == false ) { 
            setDefaultBarFgColor(LineColor, 0);
            setDefaultBarThickness(Thickness, 0);
            setShowCursorLabel(ViewValue);        
            bInit = true; 
        } 
        
        if (getBarState()==BARSTATE_NEWBAR){
        
            for (x=0; x<20; x++ ) {   
             myArray[x] = close(-x);
            }
            
            xVal = (d.call("MyEFSCallName", myArray));
                    
            return xVal; 
        }
    }
    Any help would be greatly appreciated....

    Darrell

  • #2
    Re: Passing Arrays to DLLs

    Hi Darrell,


    Originally posted by DarrellL
    How can you pass an array to a DLL? I've tried using various methods, but each time it crashes ESignal. I've created a short test program to test the DLL and everything works correctly, but once I call the DLL from ESignal it crashes. I can pass integers and doubles and it works fine, but once I pass an array---crash!!! Below are the programs I've writen in PowerBasic for creating and testing the DLL, and the EFS Script:

    Code:
    .....
    Any help would be greatly appreciated....

    Darrell
    I haven't pursued the use of DLL's much, but I think you have to pass Arrays as a String. There may also be limitations to the length of the String. Once the String is passed, parse the String (in the DLL) back into an Array.

    The best way to convert an Array into a String is to use the toString() method.

    Here is a small code snippet I had that demonstrates the toString() method (and some other tests) that can be run as-is if interested.

    PHP Code:
    var myArray=[4/5,5/6,5.4,8/9];
    debugPrintln("var myArray=[4/5,5/6,5.4,8/9];");

    debugPrintln("myArray is an Array ("+(myArray instanceof Array)+") which is also an "+typeof(myArray));
    debugPrintln("myArray[0] represents "+myArray[0]+", which is a "+typeof(myArray[0]));
    debugPrintln("myArray has these values ==> "+myArray+" and myArray.length =  "+myArray.length);

    debugPrintln("");
    debugPrintln("the toString() method converts myArray into a string, adding commas between the variable index values");
    debugPrintln("var myString=myArray.toString(); ");
    var 
    myString=myArray.toString();

    debugPrintln("myString is now a "+typeof(myString)+" ("+(myString instanceof String)+")");
    debugPrintln("myString represents ==>> "+myString+" and myString.length =  "+myString.length);

    //~ here is the output...

    //~ var myArray=[4/5,5/6,5.4,8/9];
    //~ myArray is an Array (true) which is also an object
    //~ myArray[0] represents 0.8, which is a number
    //~ myArray has these values ==> 0.8,0.8333333333333334,5.4,0.8888888888888888 and myArray.length =  4

    //~ the toString() method converts myArray into a string, adding commas between the variable index values
    //~ var myString=myArray.toString(); 
    //~ myString is now a string (false)
    //~ myString represents ==>> 0.8,0.8333333333333334,5.4,0.8888888888888888 and myString.length =  45 

    Comment


    • #3
      Steve,

      Thanks for taking the time to respond to my post. Unfortuantely I tried passing the array in the form of a string, but it still causes ESignal to crash. Although I'm not a programming or EFS script expert, my suspicion is that you can't pass Arrays or Strings to a DLL in Basic. You probably need to pass some type of pointer to the information, like in C, but I'm not a C programmer.

      Thanks again, Darrell

      Comment


      • #4
        Hi Darrell,

        You are most welcome. I performed a search of the forum by using the Search tool and this search string DLL* AND array*. Here is a link to the resultant Search.

        There is a Chris Kryza post where he passed an Array of length=1024, a potential trick in passing Arrays.

        http://forum.esignalcentral.com/show...ray*#post77383


        There are also some links to working DLLs. Perhaps something in these references will help.


        Originally posted by DarrellL
        Steve,

        Thanks for taking the time to respond to my post. Unfortuantely I tried passing the array in the form of a string, but it still causes ESignal to crash. Although I'm not a programming or EFS script expert, my suspicion is that you can't pass Arrays or Strings to a DLL in Basic. You probably need to pass some type of pointer to the information, like in C, but I'm not a C programmer.

        Thanks again, Darrell

        Comment

        Working...
        X