C# - 如何编组LPWSTR数组?(C# - how to marshal an array of LPWSTR?)
我试图编组一个类似于以下内容的c ++结构:
typedef struct _SOME_STRUCT { DWORD count; LPWSTR *items; }
“items”是LPWSTR的数组(确切的数字由“count”表示)。 在C#中,我将结构表示为:
[StructLayoutAttribute(LayoutKind.Sequential)] internal struct SOME_STRUCT { internal uint count; internal IntPtr items; }
然后在我的代码中我正在做这样的事情(其中mystruct的类型为SOME_STRUCT):
if (mystruct.count > 0) { for (int x = 0; x < mystruct.count; x++) { IntPtr ptr = new IntPtr(mystruct.items.ToInt64() + IntPtr.Size * x); string item = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(ptr)); } }
计数是正确的,但字符串项出现乱码。 我确信我必须做一些愚蠢的事情,因为我之前已经使用过其他类型的数组...而不是LPWSTR。
I am trying to marshal a c++ struct that looks like the following:
typedef struct _SOME_STRUCT { DWORD count; LPWSTR *items; }
"items" is an array of LPWSTR's (the exact number is indicated by "count"). In C# I am representing the struct as:
[StructLayoutAttribute(LayoutKind.Sequential)] internal struct SOME_STRUCT { internal uint count; internal IntPtr items; }
Then in my code I am doing something like this (where mystruct is of type SOME_STRUCT):
if (mystruct.count > 0) { for (int x = 0; x < mystruct.count; x++) { IntPtr ptr = new IntPtr(mystruct.items.ToInt64() + IntPtr.Size * x); string item = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(ptr)); } }
The count is correct, but the string item is coming out garbled. I'm sure I must be doing something daft as i've had this work before with arrays of other types...just not LPWSTR.
原文:https://stackoverflow.com/questions/6234739
最满意答案
列出子目录中
__init__.py
中的模块,然后在mcdb-mail-parser.py
Import
该目录非常类似于此前的StackOverflow Post - > 在Python中的文件夹中加载所有模块
List the modules in the
__init__.py
that's in the sub directory and then have theImport
inmcdb-mail-parser.py
reference that directoryVery similar to this previous StackOverflow Post -> Loading all modules in a folder in Python
相关问答
更多-
命令行上的Python模块(Python Modules on commandline)[2022-11-01]
在主要的Python文档中 , SimpleHTTPServer文档的底部有一个注释 。 其他模块如pdb和timeit有类似的注释。 In the main Python documentation, there is a note at the bottom of the SimpleHTTPServer documentation. Other modules such as pdb and timeit have similar notes. -
PYTHONPATH的值不表示要运行哪个python可执行文件。 相反,它是一种增强python可执行文件在导入操作期间查找模块的目录的方法。 Apple发布了带有OS X的Python版本; 这些解释器的路径在/usr/bin 。 从traceback( /Library/Frameworks )中的路径开始,您似乎安装了一个较新的Python 2.7,可能是从python.org下载的。 默认情况下,该python可能在/usr/local/bin安装了符号链接; 它的规范bin目录在框架中,位于/Li ...
-
在较新版本的Enthought Python中故意禁用了help('modules'),因为它可靠地导致使用一组标准EPD包的段错误。 列出您在EPD中安装的所有软件包的一种方法是使用enpkg -l 。 请注意,此工具仅列出使用EPD安装程序或enpkg本身安装的软件包; 它不会知道您手动安装的软件包,例如pip。 有关enpkg工具的更多信息,请输入enpkg --help ,或者在此Enthought知识库文章中找到: https : //support.enthought.com/entries/2 ...
-
如果您尝试使用本地Python模块,在您的情况下,“lib”将是“tmp.py”模块的目录: import os, sys lib_path = os.path.abspath(os.path.join('..', '..', '..', 'lib')) sys.path.append(lib_path) import mymodule 资料来源: https : //stackoverflow.com/a/4284378/950427 If you are trying to use a local ...
-
Python在CMD中导入模块(Python importing modules in CMD)[2020-01-10]
你试图在解释器中给出命令行参数... 如果安装了PIP,则根本不需要调用解释器。 只需致电: C:\Users\T****J>pip install send2trash 从命令提示符。 否则,如果您使用的是pip模块,则需要将命令行参数传递给命令行而不是传递给解释器... C:\Users\T****J>python -m pip install send2trash you are trying to give command line arguments within the interprete ... -
安装Python模块(Install Python Modules)[2022-10-05]
更新 首先安装pywin32和pyHook ...... 提取存档的内容 将文件pyhk.py复制到python Lib目录。 在Windows中, Lib目录将是\Lib 例: c:\python\Lib 在Linux中,它通常会在 /usr/lib/python2.7/ Update First install pywin32 and pyHook... Extract the contents of the archive Copy the file pyhk. ... -
Python不会隐式导入整个包层次结构。 您必须明确指出使用__init__.py文件在包的哪个级别导入的内容。 当您在__init__.py设置__all__ = ['knights']时,它仅适用于导入该模块的所有语句,例如: >>> import holygrail >>> holygrail.knights.say() Traceback (most recent call last): File "
", line 1, in AttributeError: 'm ... -
列出子目录中__init__.py中的模块,然后在mcdb-mail-parser.py Import该目录 非常类似于此前的StackOverflow Post - > 在Python中的文件夹中加载所有模块 List the modules in the __init__.py that's in the sub directory and then have the Import in mcdb-mail-parser.py reference that directory Very similar ...
-
看一下pip3脚本的第一行。 第一行(以#!开头应该指向与python 3的符号链接相同的可执行文件: > head -n 1 /usr/local/bin/pip #!/usr/local/bin/python3.6 > ls -ld /usr/local/bin/python3 lrwxr-xr-x 1 root wheel 9 Dec 25 22:37 /usr/local/bin/python3@ -> python3.6 如果不是这种情况,请卸载pip并使用正确的Python版本再次安装 ...
-
Python stdlib中的test模块不包含名为testEqual()的函数。 它的文档开头 注意: test包仅供Python内部使用。 它是为Python的核心开发人员的利益而记录的。 不鼓励在Python的标准库之外使用此包,因为此处提到的代码可以在Python版本之间更改或删除,恕不另行通知。 您确定您所关注的本指南没有自己的test.py程序,而您应该使用它吗? The test module in the Python stdlib doesn't contain a function ca ...