|
Post by DJLinux on Dec 10, 2007 12:29:03 GMT -5
I know how i can return from Basic4GL plugin an array as result
dim Array(1024) Array=DoAnythingWithArray()
but how looks the Basic4GL plugin code for ? DoAnythingWithArray(TheArray)
Thanx Joshy
|
|
|
Post by Tom Mulgrew on Dec 10, 2007 14:38:37 GMT -5
I wrote these quickly, before work. Haven't tested them yet. This is an example of how it's done. void DLLFUNC Func_AddUpArray(IDLL_Basic4GL_Runtime &basic4gl) { // Get array size int count = basic4gl.GetArrayParamDimension(1, 0);
// Allocate array int *array = new int[count];
// Read array from parameter basic4gl.SetType(DLL_BASIC4GL_EXT_INT); basic4gl.ModTypeArray(1, count); //basic4gl.ModTypeReference(); // Not required, because ModTypeArray does this automatically basic4gl.GetParam(1, array); // Sum up array int sum = 0; for (int i = 0; i < count; i++) sum += array; // Return sum as integer basic4gl.SetType(DLL_BASIC4GL_EXT_INT); basic4gl.SetReturnValue(& sum);
// Free array space delete[] array; }
void DLLFUNC Func_ReverseArray(IDLL_Basic4GL_Runtime &basic4gl) {
// Get array size int count = basic4gl.GetArrayParamDimension(1, 0);
// Allocate array int *array = new int[count]; int *dest = new int[count]; // Read array from parameter basic4gl.SetType(DLL_BASIC4GL_EXT_INT); basic4gl.ModTypeArray(1, count); //basic4gl.ModTypeReference(); // Not required, because ModTypeArray does this automatically basic4gl.GetParam(1, array);
// Reverse array into dest for (int i = 0; i < count; i++) dest = array[count - i - 1]; // Copy array back to parameter basic4gl.SetParam(1, dest); // Free array space delete[] array; delete[] dest; } (I had to put a space between & and "sum", otherwise it displays as &sum on this board)
|
|
|
Post by DJLinux on Dec 10, 2007 17:37:28 GMT -5
thanx Joshy
|
|
|
Post by Empyrion Martyr on Apr 7, 2008 14:14:25 GMT -5
i found out that b4gel crashes if you declare an array inside on your plugin functions and do not delete[] inside the same function. I needed to allocate a matrix for the whole life of an instance of a program but apparently returning from a function will give an exception if memory is not cleaned. One solution I can come up with is declaring the matrix inside b4gel and using the plugin to access the stack and the individual elements of the matrix/array, and work directly with them instead of creating a copy. (oops, gotta rewrite my plugin funcs then  )
|
|