- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以:
我不能相反方向的第 2 步。
这是我的 C++ 代码(使用 clion 和 cygwin 作为共享库使用 C++14 编译)。
#include <iostream>
wchar_t aa[2];
extern "C" {
int DoA()
{
return 10;
}
int DoB(wchar_t * in)
{
if (in[1] == 'a')
{
return 25;
}
return 30;
}
wchar_t * DoC()
{
aa[0] = 'a';
aa[1] = 'b';
return aa;
}
}
这是我的 python 3.6.1 代码,显示了我能做什么和不能做什么。那么我应该如何获取我的字符串并在 python 中使用它呢?我希望使用带有 wstring_at 的地址来获取值,但它不起作用。
from ctypes import *
import os.path
print('Hello')
itExist = os.path.exists('C:/Users/Daan/CLionProjects/stringproblem/cmake-build-release/cygstringproblem.dll')
print(itExist)
lib = cdll.LoadLibrary('C:/Users/Daan/CLionProjects/stringproblem/cmake-build-release/cygstringproblem.dll')
print('dll loaded')
A = lib.DoA()
print(A)
Bx = lib.DoB(c_wchar_p('aaa'))
print(Bx)
By = lib.DoB(c_wchar_p('bbb'))
print(By)
Ca = lib.DoC()
print(Ca)
print('Issue is coming')
Cb = wstring_at(Ca,2)
print(Cb)
这是错误的输出。
Hello
True
dll loaded
10
25
30
-1659080704
Issue is coming
Traceback (most recent call last):
File "ShowProblem.py", line 19, in <module>
Cb = wstring_at(Ca,2)
File "C:\Users\Daan\AppData\Local\Programs\Python\Python36\lib\ctypes\__init__.py", line 504, in wstring_at
return _wstring_at(ptr, size)
OSError: exception: access violation reading 0xFFFFFFFF9D1C7000
最佳答案
我在 Linux 上重现了您的问题,并通过定义 DoC
函数的返回类型来更正它:
from ctypes import *
print('Hello')
lib = cdll.LoadLibrary(PATH_TO_TOUR_LIB)
print('dll loaded')
# this line solved the issue for me
lib.DoC.restype = c_wchar_p
A = lib.DoA()
print(A)
Bx = lib.DoB(c_wchar_p('aaa'))
print(Bx)
By = lib.DoB(c_wchar_p('bbb'))
print(By)
Ca = lib.DoC()
print(Ca)
print('Issue is coming')
Cb = wstring_at(Ca,2)
print(Cb)
我还动态分配了内存(一些Python专家可能会对此发表评论,我猜这会导致内存泄漏):
extern "C" {
int DoA()
{
return 10;
}
int DoB(wchar_t * in)
{
if (in[1] == 'a')
{
return 25;
}
return 30;
}
wchar_t * DoC()
{
wchar_t* aa = new wchar_t[2];
aa[0] = 'a';
aa[1] = 'b';
return aa;
}
}
让我知道它是否适用于 Windows。
关于python - 使用 ctypes 和 wchar_t 时如何从 C++ 获取字符串到 python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50395520/
error C2440: 'initializing': cannot convert from 'TCHAR [260]' to 'std::basic_string,std::allocator>
首先。我想说,我不是 C++ 程序员。outlook 字符串在 visual studio 2015 中生成错误,并显示标题中的消息。 HWND windowHandle = (HWND)FindPr
我正在尝试检查一个 wchar_t * 是否等于另一个 wchar_t * 不区分大小写。有人可以把我放在正确的位置吗? 这是我尝试过的: wchar_t *vectored[80] = { ...
这是我的代码: wchar_t wbuffer[512]; wchar_t* wc = (wchar_t*) malloc(buffer_size); int buflen = 0;
如何正确转换这种方式? VARIANT varIndex; CString csIndex; //Index BSTR csIndex = (LPCSTR)(_bstr
我将以下代码作为我正在编写的字符串类中的私有(private)函数(它是从 wstring_convert “借来的”,并由我进行了一些修改): template std::string my_st
我有这样的东西: vector elements; 其中包含: 083938F8 083938FA 083938FC 083938FE 08393900 08393902 08393904 我想将它们
字符的签名不规范。因此,有signed char和unsigned char类型。因此,使用单个字符的函数必须使用既可以包含有符号字符又可以包含无符号字符的参数类型(此 类型选择为int),因为如果参
我在 Visual Studio C++ 2013 Community Edition 中遇到了一些奇怪的事情,这可能是编译器错误,也可能是我正在编写在没有警告的情况下进行编译的无效代码。 考虑以下片
我无法理解为什么某个隐式转换没有按我预期的那样工作。我有以下类(class) ref class ManagedWStringArrayWrapper { wchar_t** m_pointe
我有一个名为 DirToEmpty 的变量,它保存临时目录的路径。 我有另一个名为 TempBufDir 的变量,它将保持与 DirToEmpty 相同的值,但反斜杠被转义。 预期行为示例: wcha
我需要在附加 '\n' 后将多字节字符发送到套接字,但我拥有的是一个 const wchar_t*。我如何转换它? 最佳答案 如果您的问题是如何实际操作常量的内容,请考虑 const_cast . 关
我正在尝试使用语言环境方面将 wstring 转换为字符串,但我遇到了以下错误: test_facet.cpp: In function ‘int main()’: test_facet.cpp:14
我正在尝试使用 C++ 更改文件的属性我究竟做错了什么 ?代码完全拿下 https://learn.microsoft.com/en-us/windows/win32/stg/writeread-sa
我尝试将以下字符串数组传递给函数: const wchar_t *testTokens[] = { L"function", L"(",
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我尝试将阿拉伯字符存储为 wchar_t,然后将其打印到控制台,但无法使其工作。 我已将控制台设置为 UTF-8,如果我只是将阿拉伯字母输入控制台,它们就会正确显示(尽管未加入)。 我根据( How
我想存储一个包含扩展 ascii 表中字符的字符串,并打印它们。我尝试过: wchar_t wp[] = L"Росси́йская Акаде́мия Нау́к "; printf("%S", w
在C++ Visual Studio 2017中使用main函数是否错误如下: int main(int argc, wchar_t* argv[]) 因为我的程序可以接收特殊字符。 最佳答案 请阅读
我有这个运算符重载器。我的程序在创建新的 wchar_t 数组时崩溃。 myObject &operator += (const myObject &s) { wchar_t *cat = w
我是一名优秀的程序员,十分优秀!