Hello, I am writing a program in tkinter that uses the ctypes module in python to operate the dll file made of c++.
The problem is that the error "access violation reading 0x00000048" appears as shown below. Currently, we have confirmed the use of c_double type, but the use of file location (Python:str type) in c_char_p type keeps getting that error.
I'm waiting for the expert's harsh words to see if there's a problem with the code or if there's a code that needs to be added.
Errors
C:/Users/administration/source/repos/Read/Read/test.RAW Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\administration\AppData\Local\Programs\Python\Python37-32\lib\tkinter_init.py", line 1705, in __call_ return self.func(*args) File "C:\Users\administration\source\repos\Read\Read\Read.py", line 65, in clickOK IEEE.main(C.c_double(tole), C.c_char_p(filename.encode('utf-8'))) OSError: exception: access violation reading 0x00000048
Python code
import tkinter as tk
from tkinter import filedialog, ttk, Menu
import ctypes as C
import tkinter.scrolledtext as tkst
from ctypes.util import find_library
from ctypes import *
filename = ""
#Open File Function
def openfile():
filename = filelog.askopenfilename (initialdir = "C:/Users/administration/source/repos/Read/Read", title = "Please select a file", filetypes = ('RAW file', '*.RAW'), ('all files',*).*"))) the path to the #filename = file
class foo(C.Structure):
_fields_=[(filename,c_char_p)]
#calculation button function, this is where the problem occurs.
def clickOK():
IEEE = C.CDLL('IEEE')
tole = 0.00005
c_main = IEEE['main']
IEEE.main.argtypes = [C.c_double,C.c_char_p]
IEEE.main(C.c_double(tole), C.c_char_p(filename.encode('utf-8')))
#Part of the problem.
txt = open("IEEE result.txt", 'r')
data = txt.read()
scrt.insert(tk.INSERT, data)
scrt.see(tk.END)
#There is no problem because the bottom part is the basic setting.
if __name__ == '__main__':
#Title and Size
win = tk.Tk()
win.title("Power Flow Simulator")
win.resizable(True, True)
#win.geometry("600x400+200+200")
#Inner Label
labelGender = ttk.Label(win, text="Method:")
labelGender.grid(column=0, row=0)
labelMargin = ttk.Label(win, text="tolerance:")
labelMargin.grid(column=1, row=0)
#MethodFormat
Method = tk.StringVar()
MethodCombo = ttk.Combobox(win, width=20, textvariable=Method)
MethodCombo['values'] = ("Gauss-Seidel method", "Newton-Raphson method")
MethodCombo.grid(column=0, row=1)
MethodCombo.current(0)
#Margin Format
Margin = tk.IntVar()
MarginEntered = ttk.Entry(win, width=20, textvariable=Margin)
MarginEntered.grid(column=1, row=1)
#Calculation button
action = ttk.Button(win, text="Calculate", command=clickOK)
action.grid(column=2, row=1)
#Calculation contents window
scrt = tkst.ScrolledText(win, width=75, height=15, wrap=tk.WORD)
scrt.grid(column=0, row=2, columnspan=3)
scrt.focus_set()
#Menu window format, file
menu_bar = Menu(win)
file_menu = Menu(menu_bar, tearoff = 0)
file_menu.add_command (label = 'Open File', command = openfile)
file_menu.add_separator()
file_menu.add_command (label = 'end', command = win.quit)
menu_bar.add_cascade (label = 'file', menu = file_menu)
#Menu window format, help
file_menu2 = Menu(menu_bar, tearoff = 0)
file_menu.add_separator()
file_menu2.add_command (label = 'information')
menu_bar.add_cascade (label = 'help', menu = file_menu2)
win.config(menu = menu_bar)
tk.mainloop()
The reason for the error is not known from the code you provided.
First of all, the python code is provided only with ctypes-related code, and the c/c++ code should also be viewed. In particular, you need to make sure that you have done it with the outside C. Also, I used CDLL, and in Windows, dll is usually written as stdcall. In other words, if you wrote the calling convention as stdcall, you must use windll.
If the C++ DLL is an open library, please let me know what it is.
© 2024 OneMinuteCode. All rights reserved.