Bonjour tout le monde,

Je me mets au Python et en particulier aux callback entre Python et une bibliothèque dynamique en C. Je travaille avec Python 3.4 et Visual Studio 2015 pour mon code C.

Voici ce que je cherche à faire:
  • Charger la DLL en Python
  • Creer une callBack avec la signature attendue
  • Creer la function python qui servira de callback
  • Appeler la méthode C depuis python
  • La méthode C appelle la callBack Python


Voici mon code C:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
extern "C" _declspec(dllexport)
void printValues(int * buffer, int length, void (*func)(int *, int))
{
	for (int i = 0; i < length; ++i)
	{
		buffer[i] = i;
	}
	func(buffer, length);
}
Voici mon code Python:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
from ctypes import *
 
FuncType = WINFUNCTYPE(None, POINTER(c_int), c_int)
 
def copyValueCallBack(buffer, length):
    msg="python: values:"
    for i in xrange(length):
        msg += " {0}".format(buffer[i])
    msg +=" length: {0}".format(length) 
    print(msg)
    return
 
def main():
 
    dll = windll.LoadLibrary("CallBack.dll")
    if not dll:
        print("No dll loaded")
        return
    try:
        printValuesFunc = getattr(dll, "printValues")
    except AttributeError:
        print("No printValues function loaded from the dll")
        return
 
    callBack = FuncType(copyValueCallBack)
    length = 5
    buffer = (c_int * length)()
    printValuesFunc(byref(buffer), length, callBack)
 
if __name__ == "__main__":
    main()
J'obtiens la callBack Python est bien appelé mais j'obtiens toujours une erreur: Run-Time Check Failure #0 - T.

Je ne vois pas du tout comment corriger ce genre d'erreur. Merci de vos retours