ctypes wintypes WCHAR字符串附加空格
ctypes wintypes WCHAR String Additional White Spaces
为什么每个字符后面都有空格?
C++DLL
test.h:
#ifndef TEST_DLL_H
#define TEST_DLL_H
#define EXPORT __declspec(dllexport) __stdcall
#include <iostream>
#include <Windows.h>
namespace Test_DLL
{
struct Simple
{
TCHAR a[1024];
};
extern "C"
{
int EXPORT simple(Simple* a);
}
};
#endif
test.cpp:
#include "test.h"
int EXPORT Test_DLL::simple(Simple* a)
{
std::wcout << a->a << std::endl;
return 0;
}
Python
test.py:
import ctypes
from ctypes import wintypes
class MyStructure(ctypes.Structure):
_fields_ = [("a", wintypes.WCHAR * 1024)]
a = "Hello, world!"
hDLL = ctypes.LibraryLoader(ctypes.WinDLL)
hDLL_Test = hDLL.LoadLibrary(r"...test.dll")
simple = hDLL_Test.simple
mystruct = MyStructure(a=a)
ret = simple(ctypes.byref(mystruct))
结果:
H e l l o , w o r l d !
问题是在C++DLL方面吗?或者我在Python方面遗漏了什么?
一开始我认为这是代码中的一些小问题。在调试时,我发现情况并非如此。从您的示例开始,我开发了另一个示例,说明了一些关键点。
测试.h:
#if !defined(TEST_DLL_H)
#define TEST_DLL_H
#if defined(_WIN32)
# if defined(TEST_EXPORTS)
# define TEST_API __declspec(dllexport)
# else
# define TEST_API __declspec(dllimport)
# endif
# define CALLING_CONVENTION __cdecl
#else
# define __TEXT(X) L##X
# define TEXT(X) __TEXT(X)
# define TEST_API
# define CALLING_CONVENTION
#endif
namespace TestDll {
typedef struct Simple_ {
wchar_t a[1024];
} Simple;
extern "C" {
TEST_API int CALLING_CONVENTION simple(Simple *pSimple);
TEST_API int CALLING_CONVENTION printStr(char *pStr);
TEST_API int CALLING_CONVENTION wprintWstr(wchar_t *pWstr);
TEST_API wchar_t* CALLING_CONVENTION wstr();
TEST_API void CALLING_CONVENTION clearWstr(wchar_t *pWstr);
}
};
#endif // TEST_DLL_H
test.cpp:
#define TEST_EXPORTS
#include "test.h"
#if defined(_WIN32)
# include <Windows.h>
#else
# include <wchar.h>
# define __FUNCTION__ "function"
#endif
#include <stdio.h>
//#include <iostream>
#define PRINT_MSG_0() printf("From C: - [%s] (%d) - [%s]n", __FILE__, __LINE__, __FUNCTION__)
#define WPRINT_MSG_0() wprintf(L"From C: - [%s] (%d) - [%s]n", TEXT(__FILE__), __LINE__, TEXT(__FUNCTION__))
#define DUMMY_TEXT_W L"Dummy text."
//using namespace std;
int TestDll::simple(Simple *pSimple) {
//std::wcout << pSimple->a << std::endl;
WPRINT_MSG_0();
int ret = wprintf(L"%s", pSimple->a);
wprintf(L"n");
return ret;
}
int TestDll::printStr(char *pStr) {
PRINT_MSG_0();
int ret = printf("%s", pStr);
printf("n");
return ret;
}
int TestDll::wprintWstr(wchar_t *pWstr) {
WPRINT_MSG_0();
int ret = wprintf(L"%s", pWstr);
wprintf(L"n");
int len = wcslen(pWstr);
char *buf = (char*)pWstr;
wprintf(L"Hex (%d): ", len);
for (int i = 0; i < len * sizeof(wchar_t); i++)
wprintf(L"%02X ", buf[i]);
wprintf(L"n");
return ret;
}
wchar_t *TestDll::wstr() {
wchar_t *ret = (wchar_t*)malloc((wcslen(DUMMY_TEXT_W) + 1) * sizeof(wchar_t));
wcscpy(ret, DUMMY_TEXT_W);
return ret;
}
void TestDll::clearWstr(wchar_t *pWstr) {
free(pWstr);
}
main.cpp:
#include "test.h"
#include <stdio.h>
#if defined(_WIN32)
# include <Windows.h>
#endif
int main() {
char *text = "Hello, world!";
TestDll::Simple s = { TEXT("Hello, world!") };
int ret = simple(&s); // ??? Compiles even if namespace not specified here !!!
printf(""simple" returned %dn", ret);
ret = TestDll::printStr("Hello, world!");
printf(""printStr" returned %dn", ret);
ret = TestDll::wprintWstr(s.a);
printf(""wprintWstr" returned %dn", ret);
return 0;
}
code.py:
#!/usr/bin/env python3
import sys
import ctypes
DLL_NMAME = "./test.dll"
DUMMY_TEXT = "Hello, world!"
WCharArr1024 = ctypes.c_wchar * 1024
class SimpleStruct(ctypes.Structure):
_fields_ = [
("a", WCharArr1024),
]
def main():
test_dll = ctypes.CDLL(DLL_NMAME)
simple_func = test_dll.simple
simple_func.argtypes = [ctypes.POINTER(SimpleStruct)]
simple_func.restype = ctypes.c_int
stuct_obj = SimpleStruct(a=DUMMY_TEXT)
print_str_func = test_dll.printStr
print_str_func.argtypes = [ctypes.c_char_p]
print_str_func.restype = ctypes.c_int
wprint_wstr_func = test_dll.wprintWstr
wprint_wstr_func.argtypes = [ctypes.c_wchar_p]
wprint_wstr_func.restype = ctypes.c_int
wstr_func = test_dll.wstr
wstr_func.argtypes = []
wstr_func.restype = ctypes.c_wchar_p
clear_wstr_func = test_dll.clearWstr
clear_wstr_func.argtypes = [ctypes.c_wchar_p]
clear_wstr_func.restype = None
#print("From PY: [{:s}]".format(stuct_obj.a))
ret = simple_func(ctypes.byref(stuct_obj))
print(""{:s}" returned {:d}".format(simple_func.__name__, ret))
ret = print_str_func(DUMMY_TEXT.encode())
print(""{:s}" returned {:d}".format(print_str_func.__name__, ret))
#ret = wprint_wstr_func(ctypes.cast(DUMMY_TEXT.encode(), ctypes.c_wchar_p))
ret = wprint_wstr_func(DUMMY_TEXT)
print(""{:s}" returned {:d}".format(wprint_wstr_func.__name__, ret))
s = wstr_func()
print(""{:s}" returned "{:s}"".format(wstr_func.__name__, s))
#clear_wstr_func(s)
if __name__ == "__main__":
#print("Python {:s} on {:s}n".format(sys.version, sys.platform))
main()
更改:
- 删除了C++层(以排除尽可能多的变量),仅依赖C
- 将代码调整为符合Nix(我在Ubtu上运行过它,但我遇到了其他不打算讨论的问题)
- 增加了更多的功能(这是一个调试过程),以收集尽可能多的信息
- 进行了一些重命名、重构和其他不重要的更改
- 在调查过程中,我发现了一个有趣的问题(来自main.cpp)。显然,simple函数会编译,即使我没有准备声明它的命名空间。这不适用于其他函数经过一些快速尝试,我意识到这是因为Simple参数(可能是因为它也是命名空间的一部分?)。无论如何,没有花太多时间,也没有弄清真相,可能是未定义的行为(这只是因为运气不好)
- 窄函数和宽函数是混合的,即NO-NO,仅用于调试/演示目的
输出:
e:WorkDevStackOverflowq054269984>"c:Installx86MicrosoftVisual Studio Community2015vcvcvarsall.bat" x64 e:WorkDevStackOverflowq054269984>dir /b code.py main.cpp test.cpp test.h e:WorkDevStackOverflowq054269984>cl /nologo /DDLL /DUNICODE /MD /EHsc test.cpp /link /NOLOGO /DLL /OUT:test.dll test.cpp Creating library test.lib and object test.exp e:WorkDevStackOverflowq054269984>cl /nologo /DUNICODE /MD /EHsc main.cpp /link /NOLOGO /OUT:main.exe test.lib main.cpp e:WorkDevStackOverflowq054269984>dir /b code.py main.cpp main.exe main.obj test.cpp test.dll test.exp test.h test.lib test.obj e:WorkDevStackOverflowq054269984>main.exe From C: - [test.cpp] (23) - [TestDll::simple] Hello, world! "simple" returned 13 From C: - [test.cpp] (31) - [TestDll::printStr] Hello, world! "printStr" returned 13 From C: - [test.cpp] (39) - [TestDll::wprintWstr] Hello, world! Hex (13): 48 00 65 00 6C 00 6C 00 6F 00 2C 00 20 00 77 00 6F 00 72 00 6C 00 64 00 21 00 "wprintWstr" returned 13 e:WorkDevStackOverflowq054269984>"e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe" code.py Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32 F r o m C : - [ t e s t . c p p ] ( 2 3 ) - [ T e s t D l l : : s i m p l e ] H e l l o , w o r l d ! "simple" returned 13 From C: - [test.cpp] (31) - [TestDll::printStr] Hello, world! "printStr" returned 13 F r o m C : - [ t e s t . c p p ] ( 3 9 ) - [ T e s t D l l : : w p r i n t W s t r ] H e l l o , w o r l d ! H e x ( 1 3 ) : 4 8 0 0 6 5 0 0 6 C 0 0 6 C 0 0 6 F 0 0 2 C 0 0 2 0 0 0 7 7 0 0 6 F 0 0 7 2 0 0 6 C 0 0 6 4 0 0 2 1 0 0 "wprintWstr" returned 13 "wstr" returned "Dummy text."
- 它似乎与Python相关
- 字符串本身没有混乱(它们的长度和wprintf返回值是正确的)。更像是stdout是罪魁祸首
然后,我更进一步:
e:WorkDevStackOverflowq054269984>for /f %f in ('dir /b "e:WorkDevVEnvspy_064*"') do ("e:WorkDevVEnvs%fScriptspython.exe" code.py) e:WorkDevStackOverflowq054269984>("e:WorkDevVEnvspy_064_02.07.15_test0Scriptspython.exe" code.py ) Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32 From C: - [test.cpp] (23) - [TestDll::simple] Hello, world! "simple" returned 13 From C: - [test.cpp] (31) - [TestDll::printStr] Hello, world! "printStr" returned 13 From C: - [test.cpp] (39) - [TestDll::wprintWstr] Hello, world! Hex (13): 48 00 65 00 6C 00 6C 00 6F 00 2C 00 20 00 77 00 6F 00 72 00 6C 00 64 00 21 00 "wprintWstr" returned 13 "wstr" returned "Dummy text." e:WorkDevStackOverflowq054269984>("e:WorkDevVEnvspy_064_03.04.04_test0Scriptspython.exe" code.py ) Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32 From C: - [test.cpp] (23) - [TestDll::simple] Hello, world! "simple" returned 13 From C: - [test.cpp] (31) - [TestDll::printStr] Hello, world! "printStr" returned 13 From C: - [test.cpp] (39) - [TestDll::wprintWstr] Hello, world! Hex (13): 48 00 65 00 6C 00 6C 00 6F 00 2C 00 20 00 77 00 6F 00 72 00 6C 00 64 00 21 00 "wprintWstr" returned 13 "wstr" returned "Dummy text." e:WorkDevStackOverflowq054269984>("e:WorkDevVEnvspy_064_03.05.04_test0Scriptspython.exe" code.py ) Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32 F r o m C : - [ t e s t . c p p ] ( 2 3 ) - [ T e s t D l l : : s i m p l e ] H e l l o , w o r l d ! "simple" returned 13 From C: - [test.cpp] (31) - [TestDll::printStr] Hello, world! "printStr" returned 13 F r o m C : - [ t e s t . c p p ] ( 3 9 ) - [ T e s t D l l : : w p r i n t W s t r ] H e l l o , w o r l d ! H e x ( 1 3 ) : 4 8 0 0 6 5 0 0 6 C 0 0 6 C 0 0 6 F 0 0 2 C 0 0 2 0 0 0 7 7 0 0 6 F 0 0 7 2 0 0 6 C 0 0 6 4 0 0 2 1 0 0 "wprintWstr" returned 13 "wstr" returned "Dummy text." e:WorkDevStackOverflowq054269984>("e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe" code.py ) Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32 F r o m C : - [ t e s t . c p p ] ( 2 3 ) - [ T e s t D l l : : s i m p l e ] H e l l o , w o r l d ! "simple" returned 13 From C: - [test.cpp] (31) - [TestDll::printStr] Hello, world! "printStr" returned 13 F r o m C : - [ t e s t . c p p ] ( 3 9 ) - [ T e s t D l l : : w p r i n t W s t r ] H e l l o , w o r l d ! H e x ( 1 3 ) : 4 8 0 0 6 5 0 0 6 C 0 0 6 C 0 0 6 F 0 0 2 C 0 0 2 0 0 0 7 7 0 0 6 F 0 0 7 2 0 0 6 C 0 0 6 4 0 0 2 1 0 0 "wprintWstr" returned 13 "wstr" returned "Dummy text." e:WorkDevStackOverflowq054269984>("e:WorkDevVEnvspy_064_03.07.02_test0Scriptspython.exe" code.py ) Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 F r o m C : - [ t e s t . c p p ] ( 2 3 ) - [ T e s t D l l : : s i m p l e ] H e l l o , w o r l d ! "simple" returned 13 From C: - [test.cpp] (31) - [TestDll::printStr] Hello, world! "printStr" returned 13 F r o m C : - [ t e s t . c p p ] ( 3 9 ) - [ T e s t D l l : : w p r i n t W s t r ] H e l l o , w o r l d ! H e x ( 1 3 ) : 4 8 0 0 6 5 0 0 6 C 0 0 6 C 0 0 6 F 0 0 2 C 0 0 2 0 0 0 7 7 0 0 6 F 0 0 7 2 0 0 6 C 0 0 6 4 0 0 2 1 0 0 "wprintWstr" returned 13 "wstr" returned "Dummy text."
如图所示,从Python3.5开始,行为是可复制的。
我认为这是因为[Python]:PEP 529——将Windows文件系统编码更改为UTF-8,但这仅适用于3.6版本。
然后我开始阅读(我甚至试图在Python3.4和Python 3.5之间进行区分),但没有取得多大成功。我浏览过的一些文章:
- [MSDN]:Windows与C++-使用Printf与现代C++
- [MSDN]:VS2005,控制台,Unicode,wcout失败
- [Python3]:Python 3.5的新增功能
然后我注意到[SO]:在Windows控制台应用程序中输出unicode字符串(@DuckMaestro的答案),并开始玩[MS.Docs]:_setmode。
添加:
#include <io.h>
#include <fcntl.h>
static int set_stdout_mode(int mode) {
fflush(stdout);
int ret = _setmode(_fileno(stdout), mode);
return ret;
}
并像test.cpp中的int stdout_mode = set_stdout_mode(_O_TEXT);
一样调用它,然后从C输出任何内容(和C++:std::wcout
行未注释),得到:
e:WorkDevStackOverflowq054269984>"e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe" code.py Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32 Hello, world! From C: - [test.cpp] (32) - [TestDll::simple] Hello, world! "simple" returned 13 From C: - [test.cpp] (40) - [TestDll::printStr] Hello, world! "printStr" returned 13 From C: - [test.cpp] (48) - [TestDll::wprintWstr] Hello, world! Hex (13): 48 00 65 00 6C 00 6C 00 6F 00 2C 00 20 00 77 00 6F 00 72 00 6C 00 64 00 21 00 "wprintWstr" returned 13 "wstr" returned "Dummy text."
- 虽然它有效,但我不知道为什么。它可能是未定义的行为
- 打印_setmode的返回值,显示Python 3.4和main.exe自动将模式设置为_O_TEXT(>0x4000),而较新的Python版本(不起作用的版本)将其设置为_O_BINARY(0x8000)-这显然是的原因(可能与以下内容有关:[Python]:问题#16587-Py_Initialize在Windows上破坏wprintf)
- 当调用printf或
std::cout
时,试图将其设置为任何与宽相关的常量(_O_U16TEXT、_O_U8TEXT)会使程序崩溃(即使在使用宽函数时恢复原始模式-在窄函数之前)
- 尝试输出真实的Unicode字符将不起作用(很可能)
- 您可以在Python端实现相同的目标:
msvcrt.setmode(sys.stdout.fileno(), 0x4000)
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 移除前面有空格的前2个字符串
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 在字符串中插入空格
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 使用另一个字符串从字符串中删除空格
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- C++ "noskipws"没有按预期工作,如何正确允许字符串中的空格?
- 在 c++ 中逐行阅读字符串,不要以空格停止
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 如何存储包含多个空格的字符串 c++
- 在特定位置为字符串添加空格
- 有没有办法在 c++ 中获取带有空格的完整字符串?
- C++完全复制要字符串的字符列表,而不会忽略多个空格
- Winsock2:当我尝试发送带有空格的字符串时,该函数在遇到空格时似乎停止发送
- 从没有空格的字符串中提取整数
- 用于拆分空格字符串的程序不起作用
- 解析C++中的完整(从头到尾)字符串,包括所有空格
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 如何获取我的区域设置的空格字符串?