Forum » Programiranje » [Python] ctypes
[Python] ctypes

twom ::
Dll funkcija spisana v C
Poizkusil sem malo morje kombinacij, pa sem dobival neke "pointerje" ali pa errorje...
Eden od mojih Python poizkusov
Vprašanje... kako dobim vrednost?
function GetValue(RecNr: Integer; out Value: PAnsiChar): Integer;V "out Value" naj bi se vpisala vrednost (tudi iz 0x00 znakov), se pravi mora bit Value nek pointer.
Poizkusil sem malo morje kombinacij, pa sem dobival neke "pointerje" ali pa errorje...
Eden od mojih Python poizkusov
def GetPozicijaFromHistory1(RecNr): obj = self.dll.GetValue obj.argtypes = [c_int, POINTER(c_char_p)] obj.restype = c_long field_value = c_char_p() obj(RecNr, byref(field_value)))in rezultat
<cparam 'P' (0x01A7CE18)>
Vprašanje... kako dobim vrednost?
- spremenil: twom ()

socialec ::
baje da moraš uporabiti vrnjeno dolžino za branje vsebine iz pointerja z morebitnimi 0x00 vmes s string_at()

LiquidAI ::
@twom: Vaša deklaracija funkcije GetValue ne ustreza C sintaksi. Primer za Pascal DLL lib, C++ DLL lib in Python ctypes.
Bonus: V C++ se poljubno spremeni vrednost po referenci , ki jo deklarirate v Pythonu.
Feature: V Pascalu se ne spremeni deklarirana vrednostv Pythonu.
// FreePascal
// Python
// Visual C++
Bonus: V C++ se poljubno spremeni vrednost po referenci , ki jo deklarirate v Pythonu.
Feature: V Pascalu se ne spremeni deklarirana vrednostv Pythonu.
// FreePascal
library PascalDllLib; {$mode objfpc}{$H+} function GetValue(RecNr: Integer; out Value : PAnsiChar): Integer; stdcall; begin writeln('Pascal RecNr: ', RecNr); Value:= PAnsiChar('thisisalongtext'); writeln('Pascal Value: ', Value); Result:= Length(Value); writeln('Pascal Result: ', Result); end; exports GetValue; begin end.
// Python
from ctypes import CDLL, POINTER, addressof, byref, c_char_p, c_int, c_long, create_string_buffer class CDLLExample: def __init__(self) -> None: self.dll = CDLL("X:\\PascalDllLib.dll") # DLL file (absolute path) def GetPozicijaFromHistory1(self, RecNr) -> int: obj = self.dll.GetValue obj.argtypes = [c_int, POINTER(c_char_p)] obj.restype = c_long str_buffer = create_string_buffer(b'slotech', 256) field_value = c_char_p(addressof(str_buffer)) return_value = obj(RecNr, byref(field_value)) # call Pascal function GetValue print('Pascal str buffer value: %s' % str_buffer.value) # get Value return return_value if __name__ == '__main__': print("Python main") m = CDLLExample() return_value = m.GetPozicijaFromHistory1(1) print('Python return value: %s' % value)
// Visual C++
// dllmain.cpp : Defines the entry point for the DLL application. #include "pch.h" #include <stdio.h> BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } #ifdef __cplusplus extern "C" { #endif __declspec(dllexport) int GetValue(INT recNr, CHAR** value) { printf("C++ recNr: %d\n", recNr); strcpy_s(*value, 5, "C++!\0"); printf("C++ value: %s\n", *value); return strlen(*value); } #ifdef __cplusplus } #endif
Zgodovina sprememb…
- spremenilo: LiquidAI ()
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | c++ rabim pomoč konstuktorjiOddelek: Programiranje | 1178 (873) | Snowflake2 |
» | Predstavitev dvojiškega drevesa z seznamomOddelek: Programiranje | 2091 (1691) | ktka |
» | Kontakt obrazecOddelek: Izdelava spletišč | 2163 (2008) | betmen |
» | [c#] loopanje po fieldih classaOddelek: Programiranje | 1219 (1046) | vojko20 |
» | [C++]Instanca razredaOddelek: Programiranje | 1649 (1540) | Senitel |