Passing Array to DLL Function

Ok i'm trying to pass an array to a function inside a DLL. the following is the C++ DLL Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	double XPortBarArray(double inArray[][6], int Size)
	{
		/** 
		0 - time,
		1 - open,
		2 - low,
		3 - high,
		4 - close,
		5 - volume.
		**/

		double first = inArray[4][1];

		return first;
	}

any reason why this wouldn't work. I must be missing something because the returned double is not correct.
thanks
Last edited on
The DLL and main application are compiled separately from each other so your compile-time inArray[][6] will do no good. You must handle it as either a double* and do the width/height math yourself, or pass it as a double** pointer with a width and height and treat it as an array of pointers to doubles.
Thanks for information am I close

1
2
// the export
__declspec(dllexport) double* XPortBarArray(double* InArray[][6], int szRows, int szCols);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// the function

	double* XPortBarArray(double* inArray[][6], int szRow, int szCol=6)
	{
		/** 
		0 - time,
		1 - open,
		2 - low,
		3 - high,
		4 - close,
		5 - volume.
		**/

		double* first = inArray[0][4];

		return first;
	}

Ok, I guess i'm a little confused about the whole double* thing. I want to be able to cycle through the whole array If need be but I need to know that the array is being passed and extract the first double from inArray[0][4] at col 4

still not working
1
2
3
4
5
6
7
8
9
10
11
12
double GetAtRow0Col4(double **arr, unsigned rows, unsigned cols)
{
    return arr[0][4];
    //to iterate:
    for(unsigned r = 0; r < rows; ++r)
    {
        for(unsigned c = 0; c < cols; ++c)
        {
            //do stuff with arr[r][c]
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
//calling code:
const unsigned MyRows (3), MyCols (6);
double **MyArr = new double*[MyRows];
for(unsigned r = 0; r < MyRows; ++r)
{
    MyArr[r] = new double[MyCols];
}
//...
//fill array with data
//...
double first = TheDLLFunc(MyArr, MyRows, MyCols);

Last edited on
ok now im just confused

<DLL CODE>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

namespace MT4_Export
{
    class MyFunctions
    {
    public:
        __declspec(dllexport) int XPortCurBar(double HIGH, double LOW, double OPEN, double CLOSE, int VOLUME);
		__declspec(dllexport) double* XPortBarArray(double* InArr[][6], int szRows, int szCols);
		__declspec(dllexport) char* XPortMsg(string XMsg);
	};

	int XPortCurBar(double High, double Low, double Open, double Close, int Volume)
	{

		return 0;
	}

	double* XPortBarArray(double **inArray[], unsigned szRow, unsigned szCol=6)
	{
		/** 
		0 - time,
		1 - open,
		2 - low,
		3 - high,
		4 - close,
		5 - volume.
		**/
		double* first = inArray[0][4];

		return first; //first;
	}

	char* XPortMsg(char* inmsg)
	{
		char* mHeader = "DLL Received: ";
		// retrieve incoming string and assign to variable
		char* msgHeader = new char[strlen(inmsg)+1];
		memcpy(msgHeader,inmsg,strlen(inmsg)+1);
		msgHeader[strlen( inmsg )] = '\0';

		char* retVal = new char[strlen(mHeader)+strlen(msgHeader)+1];
		*retVal = '\0';
		
		// Assemble the string
		strcat(retVal,mHeader);
		strcat(retVal,msgHeader);

		return retVal;
	}

}

The calling routine is located outside of the DLL. The Function XPortMsg() works just fine and returns what is expected.
Last edited on
Topic archived. No new replies allowed.