无法检索由 ref 作为输出参数传递给 DLL 函数的 ctype 的值
Unable to retrieve value of a ctype passed by ref as an output parameter to a DLL functio
我已经成功地初始化了我的 dll 并使用 ctypes 从中调用函数。
我还能够检索通过引用这些函数传递的输出参数的内容。我成功的输出参数是 c_int 或 c_double。
但是,当它们应该包含一个字符串并且我正在使用 c_wchar_p.
时,我无法检索这些输出参数的内容
这是代码。首先,我设置了所有变量。然后,我调用函数。
in_Iresult=ctypes.c_int(1) #Desired result line, from 0 up to Nresult-1
out_LaufradBez=ctypes.c_wchar_p() #Fan wheel designation
out_LaufradPdf=ctypes.c_wchar_p() #(if available) Path and file name of fan wheel data sheet
out_GehaeuseBez=ctypes.c_wchar_p() #Housing designation, if with housing
out_GehaeusePdf=ctypes.c_wchar_p() #(if available) Path and file name of housingdata sheet
out_GehaeuseOefWkl=ctypes.c_wchar_p() #housing angle, if with housing
out_EinstrBez=ctypes.c_wchar_p() #Designation of inlet cone, if with inlet cone
out_EinstrPdf=ctypes.c_wchar_p() #(if available) Path and file name of inlet cone data sheet
out_BildS=ctypes.c_wchar_p() #(if available) Path and file name of drawing
out_BildB=ctypes.c_wchar_p() #(if available) Path and file name of image
out_V=ctypes.c_double() #Volume flow
out_dP_fa=ctypes.c_double() #Pressure (static)
out_dP_tot=ctypes.c_double() #Pressure (total)
out_LpA=ctypes.c_double() #Sound pressure level, A-weighted
out_LwA=ctypes.c_double() #Sound power level, A-weighted
out_n=ctypes.c_double() #Speed
out_Pwe=ctypes.c_double() #Shaft power
out_PweMax=ctypes.c_double() #Max shaft power (over complete flow range)
out_Eta_fa=ctypes.c_double() #Mechanical efficiency static
out_Eta_tot=ctypes.c_double() #Mechanical efficiency total
out_EinbauBreite=ctypes.c_double() #Unit lenght
out_Ueberlappung=ctypes.c_double() #Axile gap
out_Anmerkung=ctypes.c_void_p() #Annotations to fan
Result=PunkerDLL.CAT_RESULTW(in_Iresult,
ctypes.byref(out_LaufradBez),
ctypes.byref(out_LaufradPdf),
ctypes.byref(out_GehaeuseBez),
ctypes.byref(out_GehaeusePdf),
ctypes.byref(out_GehaeuseOefWkl),
ctypes.byref(out_EinstrBez),
ctypes.byref(out_EinstrPdf),
ctypes.byref(out_BildS),
ctypes.byref(out_BildB),
ctypes.byref(out_V),
ctypes.byref(out_dP_fa),
ctypes.byref(out_dP_tot),
ctypes.byref(out_LpA),
ctypes.byref(out_LwA),
ctypes.byref(out_n),
ctypes.byref(out_Pwe),
ctypes.byref(out_PweMax),
ctypes.byref(out_Eta_fa),
ctypes.byref(out_Eta_tot),
ctypes.byref(out_EinbauBreite),
ctypes.byref(out_Ueberlappung),
ctypes.byref(out_Anmerkung))
print("L'extraction de données de fan a fonctionné si 0 :",Result)
s = "Hello, world!"
c_s = ctypes.c_wchar_p(s)
print(c_s)
print(c_s.value)
print(out_n)
print(out_n.value)
print(out_LaufradBez)
print(out_LaufradBez.value)
我可以毫不费力地打印出“Hello, world!”在终端中打印 c_double 类型的输出参数的值也没有问题,例如 out_n。但是,我似乎无法打印 c_void_p 参数的值。它使程序崩溃。
查看终端的打印输出:
L'extraction de données de fan a fonctionné si 0 : 0
c_wchar_p(2565936992944)
Hello, world!
c_double(1354.0)
1354.0
c_wchar_p(29555362188230724)
如您所见,out_LaufradBez.value 的打印输出值不是 compile/execute。如您所见,内存中有内容,但无法打印出该值。
有没有办法使用 print() 以外的其他方法打印出该值?否则,有没有办法知道导致程序崩溃的错误是什么?
基本上,我知道 DLL 函数已经正常工作,我知道变量有内容,但我无法打印它的值。
请暂停
如果您将 out_LaufradBez
指针的值视为十六进制,您会发现写入 c_wchar_p
的不是指针,而是 UTF-16 数据:
>>> x=29555362188230724
>>> hex(x)
'0x69007200700044'
>>> x.to_bytes(8,'little').decode('utf-16le')
'Dpri'
这表示 API 采用 wchar_t*
而不是 wchar_t**
但未显示原型。我怀疑 API 应该使用 ctypes.create_unicode_buffer(size)
而不是指针地址传递给预分配的缓冲区。
使用实际的 C 原型更新您的问题以获得更多指导。
我已经成功地初始化了我的 dll 并使用 ctypes 从中调用函数。 我还能够检索通过引用这些函数传递的输出参数的内容。我成功的输出参数是 c_int 或 c_double。 但是,当它们应该包含一个字符串并且我正在使用 c_wchar_p.
时,我无法检索这些输出参数的内容这是代码。首先,我设置了所有变量。然后,我调用函数。
in_Iresult=ctypes.c_int(1) #Desired result line, from 0 up to Nresult-1
out_LaufradBez=ctypes.c_wchar_p() #Fan wheel designation
out_LaufradPdf=ctypes.c_wchar_p() #(if available) Path and file name of fan wheel data sheet
out_GehaeuseBez=ctypes.c_wchar_p() #Housing designation, if with housing
out_GehaeusePdf=ctypes.c_wchar_p() #(if available) Path and file name of housingdata sheet
out_GehaeuseOefWkl=ctypes.c_wchar_p() #housing angle, if with housing
out_EinstrBez=ctypes.c_wchar_p() #Designation of inlet cone, if with inlet cone
out_EinstrPdf=ctypes.c_wchar_p() #(if available) Path and file name of inlet cone data sheet
out_BildS=ctypes.c_wchar_p() #(if available) Path and file name of drawing
out_BildB=ctypes.c_wchar_p() #(if available) Path and file name of image
out_V=ctypes.c_double() #Volume flow
out_dP_fa=ctypes.c_double() #Pressure (static)
out_dP_tot=ctypes.c_double() #Pressure (total)
out_LpA=ctypes.c_double() #Sound pressure level, A-weighted
out_LwA=ctypes.c_double() #Sound power level, A-weighted
out_n=ctypes.c_double() #Speed
out_Pwe=ctypes.c_double() #Shaft power
out_PweMax=ctypes.c_double() #Max shaft power (over complete flow range)
out_Eta_fa=ctypes.c_double() #Mechanical efficiency static
out_Eta_tot=ctypes.c_double() #Mechanical efficiency total
out_EinbauBreite=ctypes.c_double() #Unit lenght
out_Ueberlappung=ctypes.c_double() #Axile gap
out_Anmerkung=ctypes.c_void_p() #Annotations to fan
Result=PunkerDLL.CAT_RESULTW(in_Iresult,
ctypes.byref(out_LaufradBez),
ctypes.byref(out_LaufradPdf),
ctypes.byref(out_GehaeuseBez),
ctypes.byref(out_GehaeusePdf),
ctypes.byref(out_GehaeuseOefWkl),
ctypes.byref(out_EinstrBez),
ctypes.byref(out_EinstrPdf),
ctypes.byref(out_BildS),
ctypes.byref(out_BildB),
ctypes.byref(out_V),
ctypes.byref(out_dP_fa),
ctypes.byref(out_dP_tot),
ctypes.byref(out_LpA),
ctypes.byref(out_LwA),
ctypes.byref(out_n),
ctypes.byref(out_Pwe),
ctypes.byref(out_PweMax),
ctypes.byref(out_Eta_fa),
ctypes.byref(out_Eta_tot),
ctypes.byref(out_EinbauBreite),
ctypes.byref(out_Ueberlappung),
ctypes.byref(out_Anmerkung))
print("L'extraction de données de fan a fonctionné si 0 :",Result)
s = "Hello, world!"
c_s = ctypes.c_wchar_p(s)
print(c_s)
print(c_s.value)
print(out_n)
print(out_n.value)
print(out_LaufradBez)
print(out_LaufradBez.value)
我可以毫不费力地打印出“Hello, world!”在终端中打印 c_double 类型的输出参数的值也没有问题,例如 out_n。但是,我似乎无法打印 c_void_p 参数的值。它使程序崩溃。
查看终端的打印输出:
L'extraction de données de fan a fonctionné si 0 : 0
c_wchar_p(2565936992944)
Hello, world!
c_double(1354.0)
1354.0
c_wchar_p(29555362188230724)
如您所见,out_LaufradBez.value 的打印输出值不是 compile/execute。如您所见,内存中有内容,但无法打印出该值。
有没有办法使用 print() 以外的其他方法打印出该值?否则,有没有办法知道导致程序崩溃的错误是什么?
基本上,我知道 DLL 函数已经正常工作,我知道变量有内容,但我无法打印它的值。
请暂停
如果您将 out_LaufradBez
指针的值视为十六进制,您会发现写入 c_wchar_p
的不是指针,而是 UTF-16 数据:
>>> x=29555362188230724
>>> hex(x)
'0x69007200700044'
>>> x.to_bytes(8,'little').decode('utf-16le')
'Dpri'
这表示 API 采用 wchar_t*
而不是 wchar_t**
但未显示原型。我怀疑 API 应该使用 ctypes.create_unicode_buffer(size)
而不是指针地址传递给预分配的缓冲区。
使用实际的 C 原型更新您的问题以获得更多指导。