1

I'm trying to get out an array in a pythonic format [numpy or list] form a DLLwritten in c#/c++. This DLL gives out the pointer to the memory and not the evaluated array. How can I access the memory in a FAST way (I wrote the DLL to speedup the code)?

I have used the pythonnet module and i managed to get the memory pointer as a but I'm failing in extracting the memory pointer as an integer or binary and then to get the data of the output vector.

Here the python code:

import numpy as np
import clr
clr.AddReference(R'...\PyDLL.dll')  
from PyDLL import StringLibrary

my_instance = StringLibrary()

x = my_instance.arrayShift(np.array([1,2,3,4,5,6,7,8,9,0]).reshape(-1))

And then the C# code

namespace PyDLL
{
    public class StringLibrary
    {
        public static double[] arrayShift(double[] array)
        {
            int b = array.Length;
            double[] newArray = new double[b];

            for(int i=0; i<b; i++)
            {
                newArray[i] = array[b-i-1];
            }

            return newArray;
        }
    }
}

I expect that the output x in python is a list or a np array, while now it is a System.Double[] object.

the expected output of x is [0,9,8,7,6,5,4,3,2,1]

Thank you for any help

1 Answer 1

1

After many tries and researches, I managed to solve the problem in this way:

Cpp part:

Open Visual Studio, then create a new project (Dynamic-Link Library). Name the project and add a new file in the project (Header file) and name it with the same project name. Insert the following code in the header:

// headerName.h - Contains declarations of math functions
#pragma once
#ifdef HEADERNAME_EXPORTS
#define HEADERNAME_API __declspec(dllexport)
#else
#define HEADERNAME_API __declspec(dllimport)
#endif

// Define the functions that will be called using the DLL
extern "C" HEADERNAME_API void funName1(...);
extern "C" HEADERNAME_API void funName2(...);
extern "C" HEADERNAME_API void funName3(...);
...;

In the folder 'Source Files' add a new file with extension .cpp (call it with the same name as the project), then write the function in this .cpp file. Here an example code:

#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier  COMPULSORY TO ADD
#include "HEADERNAME.h" // COMPULSORY TO ADD
#include lib#1
#include lib#2
#include lib#3
...
using namespace std;

void funName1(...)
{
    ...
}
void funName2(...)
{
    ...
}
void funName3(...)
{
    ...
}

To be able to pass the arrays between python and c++, in my solution, the use of pointers is needed and no return must be used. The output variable is passed as an input variable, initialized in python, using a pointer. In the DLL the memory cells of the output variables are re-wrote and, doing so, when the DLL completes the function, the output is computed and already usable by python without return. Here an example of how to write a function using pointers for input and output arrays:

void funName(double* inputArray_pointer, double* outputArray_pointer, int extraVariable)
{
    double inner_var = 100;
    for (int i = 0; i < extraVariable; i++)
    {
        inner_var = min(inner_var, *(inputArray_pointer+i));
        *(outputArray_pointer + i) = inner_var;
    }
}

Then rebuild the DLL.

Python part

To use correctly a DLL in the python code, the modules 'ctypes' and 'numpy' must be imported. Here follows an example of the python code (This will use the example DLL of point (9)):

import numpy as np
import ctypes

cpp_fun = ctypes.CDLL(R'pathToDll/DLLname.dll')  

# Define the types of the arguments of the DLL (pointer, int, double, ecc)
cpp_fun.funName.argtypes = [np.ctypeslib.ndpointer(),
                            np.ctypeslib.ndpointer(),
                            ctypes.c_int]

# Allocate or define the input variables using numpy or the standard scalar of python (int, float, ecc..)
inputArray = np.array([0,1,2,3,4,5,6,7,8,9,...,1000]).reshape(-1).astype('f8')
outputArray = np.zeros([inputArray.shape[0]])astype('f8')
extraVariable = inputArray.shape[0]
cpp_fun.funName(inputArray, outputArray, extraVariable)

Doing so, in the variable 'outputArray' we will find the result of the calculations performed inside the DLL as a np array structure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.