Function call from dll in python

I am trying to make a call from python to a dll, but I am getting an access violation. Perhaps some of them will tell me how to use ctypes correctly in the following code. GetItems is supposed to return a structure that looks like this:

struct ITEM
{
 unsigned short id;
 unsigned char i;
 unsigned int c;
 unsigned int f;
 unsigned int p;
 unsigned short e;
};

I'm really interested in just getting the identifier, no other fields are needed. I have my code below, what am I doing wrong? Thanks for the help.

import psutil
from ctypes import *

def _get_pid():
    pid = -1

    for p in psutil.process_iter():
        if p.name == 'myApp.exe':
            return p.pid

    return pid


class MyDLL(object):
    def __init__(self):
        self._dll = cdll.LoadLibrary('MYDLL.dll')
        self.instance = self._dll.CreateInstance(_get_pid())

    @property
    def access(self):
        return self._dll.Access(self.instance)

    def get_inventory_item(self, index):
        return self._dll.GetItem(self.instance, index)


if __name__ == '__main__':

    myDLL = MyDLL()
    myDll.get_item(5)
+3
source share
1 answer

First, you call get_item, and your class has only get_inventory_item, and you drop the result, and the capitalization of myDLL is incompatible.

You need to determine the type of Ctypes for your structure, for example:

class ITEM(ctypes.Structure):
    _fields_ = [("id", c_ushort),
                ("i", c_uchar),
                ("c", c_uint),
                ("f", c_uint),
                ("p", c_uint),
                ("e", c_ushort)]

(. http://docs.python.org/library/ctypes.html#structured-data-types)

, : ITEM:

myDLL.get_item.restype = ITEM

(. http://docs.python.org/library/ctypes.html#return-types)

, .

0

Source: https://habr.com/ru/post/1775826/


All Articles