首页 \ 问答 \ C# - 如何编组LPWSTR数组?(C# - how to marshal an array of LPWSTR?)

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
更新时间:2022-01-17 19:01

最满意答案

列出子目录中__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 to this previous StackOverflow Post -> Loading all modules in a folder in Python

相关问答

更多
  • 在主要的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 ...
  • 你试图在解释器中给出命令行参数... 如果安装了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 ...
  • 更新 首先安装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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)