SoFunction
Updated on 2024-11-19

How to use python's ctypes to call the health insurance center's dll dynamic library to download health insurance center bills

Requirements: According to the documentation of the medical insurance center and the dll dynamic library provided to call the relevant interface to download the medical insurance center's bill.

Documentation: A description of the calling dll dynamic library, which dll file to call, and a clear understanding of which function in this dll file to call.

Analysis: Combined with the document and the relevant introduction to figure out the relevant interface call process, from the above you can see that the interface call is a document, and then first call the INIT function for initialization, and then call the BUSINESS_HANDLE function in the medical insurance bureau sign in, and then in the second call the BUSINESS_HANDLE function to download the bill, and at the same time, according to the document analyze each time to call the function of the Input and output parameters of each function call according to the document. (The specific calling process and each function's input/output parameters must be analyzed according to the complete documentation of the medical insurance center.)

Code Description: As the signing and downloading bills called the same function, the difference is just different in and out of the reference, so the code will only show the call INIT function and BUSINESS_HANDLE function for signing, download the part not to write.

Code:

from ctypes import *
import os
['path'] += ';C:\localDll' # Add the dll dependency library directory to the system environment
 
def getBusiness():
 pDll = ("")
 str = ''
 # Dynamic library initialization with a successful result of 0
 res = (str)
 print(res)
 
 qiandao = '9100^100006^zzjdz^^^0000^^1^'
 yewuzhouqi = create_string_buffer(1024)
 p_qiandao = c_char_p()
 p_qiandao.value=("utf-8")
 res2 = pDll.BUSINESS_HANDLE(p_qiandao, yewuzhouqi)
 # Print the returned results
 print(res2)
 print()
 yu=()
 print(yu)
 
if __name__=="__main__":
 getBusiness()

Code Details:

1. ctypes is a library that comes with the python system and does not need to be deliberately installed. This code uses python version 3.7.

2. ['path'] += ';C:\localDll'. The dynamic library provided by the medical insurance center is not a single dll file, but a bunch of files, these dll files have mutual references in the relationship, if you directly load the file through the absolute path, the program will report an error directly saying that the specified module can not be found, so that a bunch of dll files in the C:\localDll c disk under the folder of localDll, C:\localDll can be replaced with your own dynamic library where the folder is located. localDll can be replaced with your own dynamic library is located under the folder, the command is to add the dynamic library path to the system environment environment, the following call file can be found directly from the system environment, do not have to write the absolute path and relative path.

3. pDll = (""). Load dynamic library files, load the syntax of many, such as CDLL, etc., if the command does not work you can try the other three ways to load dynamic libraries.

4. res = (str). Call the dynamic library initialization function for initialization, initialization does not enter the parameter, so str = "" on the line.

5. qiandao = '9100^100006^zzjdz^^^^0000^^^1^' #String for incoming parameters
yewuzhouqi = create_string_buffer(1024) #Allocate 1024 bytes of memory for this parameter.
p_qiandao = c_char_p() #Declare that the input parameter is a pointer in C char *p
p_qiandao.value=("utf-8") # Convert the input parameter to an array of bytes and assign it to the memory space pointed to by the address of the input parameter.

Note: The in and out parameters are both char *p, why not declare the out parameter as a c_char_p()? You can, but the problem is that the dynamic library has to modify the value in memory pointed by the pointer to the outgoing parameter, so declaring the pointer to the outgoing parameter directly without requesting the value of the memory size pointed by the outgoing parameter will result in a memory leak that will cause python to stop running directly. create_string_buffer(1024) is equivalent to the malloc function of the C language, which is equivalent to the malloc function of C language. When C declares a pointer type structure, it allocates a memory size to the structure.

6. res2 = pDll.BUSINESS_HANDLE(p_qiandao, yewuzhouqi). Call BUSINESS_HANDLE function, the function will have a return value of res2, at the same time the function will also change the value of the memory pointed to by the pointer out of the parameter yewuzhouqi.

7、print(res2)
    print()

Prints the return value of the function and the value of the outgoing parameter.

8、yu=()
    print(yu)

Convert the bytes array of the outgoing parameter to a str string.

summarize

to this article on how to use python ctypes call health insurance center dll dynamic library to download the bill of the health insurance center is introduced to this article, more related python ctypes call dll dynamic library content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!