hi all,

i am pretty new to Python(a very new to this forum). have been programming in C & C++.

i have a DLL written in C which i need to use. i would like to access it through Python instead of using Visual C++.

i am having a problem with this though. i hope someone here can help.

lemme start with my DLL definition:

#ifndef DLL_ABC_H
#define DLL_ABC_H

//Exported functions
#ifndef DLL_ABC
   #ifdef __BORLANDC__
      #define DLL_ABC   _export 
   #else
      #define DLL_ABC __declspec(dllexport) __stdcall 
   #endif
#endif

extern "C"
{
   //Version of the DLL
   extern void DLL_ABC Info_Version_ABC(char *sVersion);

   extern BYTE DLL_ABC B_ABC_Function01(BYTE *pbParam1);

   extern BYTE DLL_ABC B_ABC_Function02(BYTE * pbParam2,BYTE *pbParam3);

   extern BYTE DLL_ABC ABC_Function03(char * sFileNameIn, char * sFileNameOut,BYTE * pbParam4);

}
#endif

basically, its the byte pointers that are causing the problem. i need to work in hexadecimal here.
(param 4 is to be read in. Funtions 01 and 02 set params that wld be used by function 3 to calculate param4.)
example of use of DLL is as such:

BYTE pbParam4[16];
BYTE pbParam1[16]={0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F};
BYTE pbParam2[16]={0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F};
BYTE pbParam3[16]={0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F};

B_ABC_Function02(pbParam2,pbParam3);
B_ABC_Function01(pbParam1);

ABC_Function03("FileIn.txt", "FileOut.txt",pbParam4);

in my Python script, i have the following:

abc = ctypes.windll.DLL_ABC

p2 = 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F
p3 = 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F
byte = ctypes.c_byte()
byte = abc.B_ABC_Function02(ctypes.byref(ctypes.c_void_p(p2)), ctypes.byref(ctypes.c_void_p(p3)))

p1 = 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F
byte = abc.B_ABC_Function01(ctypes.byref(ctypes.c_void_p(p1)))

p4 = 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F
byte = abc.Function03(ctypes.byref(ctypes.c_char_p(FileIn)), ctypes.byref(ctypes.c_char_p(FileOut)), ctypes.byref(ctypes.c_void_p(p4)))

i suppose call to functions 01 and 02 are OK. however, at Function03 call, i get an error that says "error: (10054, 'Connection reset by peer')".

i am then brought to Stream.py and a statement in "def read(self, count):" is highlighted. the statement is "buf = self.sock.recv(count)".

i believe the filenames are alright. thats pretty simple.

any advice? i'm stuck here and if this cant be solved, then i will have to use this DLL in C++.

thanx in advance.

hi,

thanx for your interest.

FileIn and fileOut string are read in from commandline arguments. they were the problem afterall.

byte = abc.Function03(FileIn, FileOut, ctypes.byref(ctypes.c_void_p(p4)))

the above line works. but it looks like the p1 - p3 i'm sending in and the p4 i'm reading out are not correct. the content of FileOut and p4 are not as expected.

perhaps my passing or references are not good.

i tried this, result is different but not as expected either.

byte = abc.B_ABC_Function02(hex(p2), hex(p3))

i tried this, result is different but not as expected either.

byte = abc.B_ABC_Function02(hex(p2), hex(p3))

Do you need to convert the returned value to hex perhaps??
i.e.

byte = hex( abc.B_ABC_Function02(hex(p2), hex(p3)) )

The above code should set the value of byte to be a hexadecimal representation of the value returned by your abc.B_ABC_Function02() call.
Does that give you the expected result?
Jas.

hi,

i'm sorry, but it looks like i wasn't clear enough.

my app is such that i use functions 01 & 02 to set params in the DLL. then i use function 03 to process the input file(FileIn). and i will get an output file(FileOut) and p4 which is used with the output file. the return from all the functions are just the status.

now, what i find is that the output file and p4 has not been processed as expected. i suspect this is due to incorrect settings of p1 - p3. most likely because i am not sending them in correctly.

as you can see from the example of use i have posted earlier, using a BYTE array in C/C++ works fine with the DLL. if only i could define this in Python.

hi all,

solution was quite simple actually. i had to create a 16 byte BYTE array and then pass it thru DLL function call.

to do that i had to define:
sixteen_bytes = ctypes.c_ubyte * 16
array = sizteen_bytes(0x0F, 15, 15... 15)

problem solved! :)

This question has already been answered. Start a new discussion instead.