我想在我的python项目中调用.NET dll。我想使用一个函数,它返回字符串并获取string参数。但不知何故,我无法得到字符串,我只需得到字符串的第一个字符,有时它会引发,以下错误:
'charmap' codec can't encode characters in position 1-15: character maps to <undefined>编辑:通过放置一些代码来解决错误。
encode(sys.stdout.encoding, errors='replace')但这一次的结果并不完全是我想要的。结果是:b'h\xe7\x8c\x80'具有输入"hello"
这是我的代码:
import ctypes
import time
hllDll = ctypes.WinDLL ("C:\\Users\\yazilimhpaio2\\Downloads\\mydll.dll")
hllApiProto = ctypes.WINFUNCTYPE (
ctypes.c_wchar_p, # Return type.
ctypes.c_wchar_p # Parameter 1 ...
)
hllApiParams = (1,"p1",0),
hllApi = hllApiProto (("ReturnMyString", hllDll), hllApiParams)
var = "hello"
p1 = ctypes.c_wchar_p (var)
x = hllApi (p1)
print(x.encode(sys.stdout.encoding, errors='replace'))
time.sleep(3)ReturnMyString函数获取字符串参数并返回该参数。但是当我运行这段代码时,它只会打印参数的第一个字母。
我在python中发现了P用于字符串。。所以我不明白我的代码有什么问题。
任何帮助都将不胜感激..。
编辑:
导出的dll函数:
[ComVisible(true)]
[DllExport("ReturnMyString", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static string ReturnMyString(string value)
{
return value;
}原型:
public static string ReturnMyString(string)发布于 2016-08-24 14:04:51
如果使用非托管导出,其封送处理示例默认为".Net将这些字符串封送为单字节Ansi“。如果是这样的话,使用c_char_p并从Python传递字节字符串。
https://stackoverflow.com/questions/39036658
复制相似问题