简体   繁体   English

C#-将类似结构的行为转换为第三方函数

[英]C# - behavior of similar structures into a third-party function

I have one third-party exported function and three structures of the same size, same numer of members but different member types: 我有一个第三方导出函数和三个相同大小,相同成员数但不同成员类型的结构:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1, Size = 34)]
public struct StructA
{
    public Int32 id;
    public Int16 year;
    public Char month;
    public Char day;
    // and 12 more Char members
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1, Size = 34)]
public struct StructB
{
    public Int32 id;
    public Int16 year;
    public Byte month;
    public Byte day;
    // and 12 more Byte members
    public Int64 padding1; 
    public Int32 padding2; 
    public Int16 padding3;
    // padding fields supply the 14 bytes left of the total size (34)
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1, Size = 34)]
public struct StructC
{
    public Int32 id;
    public Int16 year;
    public Int16 month;
    public Int16 day;
    // and 12 more Int16 members
}

private static extern Int32 foo_nction(ref struct structure);

I would expect that StructA and StructC have the same result because they have the same number of parameters in the same order and each parameter correspond not in types but in sizes, but the most similar results are between StructA and StructB despite their types are not equivalent in sizes. 我希望StructA和StructC具有相同的结果,因为它们具有相同数量的参数,并且顺序相同,并且每个参数的类型和大小都不对应,但是最相似的结果在StructA和StructB之间,尽管它们的类型不相等在大小上。

THE FACTS ARE THAT: 事实是:

When I pass StructA as parameter to the 3rd-party function everything works perfect. 当我将StructA作为参数传递给第三方函数时,一切正常。

When I pass StructB as parameter its members get the expected values but truncated because Char's physical bytes are two and Byte's is one (as our friendly forum pals remarked). 当我将StructB作为参数传递时,它的成员获得了预期的值,但由于Char的物理字节为2,而Byte的物理字节为1,则被截断了(正如我们的友好论坛上的朋友所说)。

When I pass StructC as parameter the values from the 3rd-party function melt as if one Int16-type value could storage two Char-type values. 当我将StructC作为参数传递时,来自第三方函数的值将融化,好像一个Int16类型的值可以存储两个Char类型的值一样。 For example, when I pass StructA as parameter I receive date data that forms "2013-5-27" and when I pass StructB I receive date data that forms "2013-6917-2" 例如,当我传递StructA作为参数时,我收到的日期数据形式为“ 2013-5-27”,而当我传递StructB时,我收到的日期数据形式是“ 2013-6917-2”

Could anyone please explain the reason of this behavior? 谁能解释这种现象的原因吗?

Update: 更新:

Sorry for that I did't build an example for the test. 抱歉,我没有为测试建立示例。 I'd suggest that to have a look at this answer: 我建议看一下这个答案:

Calling C++ function from C#, with lots of complicated input and output parameters 从C#调用C ++函数,具有许多复杂的输入和输出参数

and other answers of that question are also good with more explanation. 该问题的其他答案也可以提供更多解释,也不错。

I think the whole point is you are confused with [ marshaling ]. 我认为重点是您对[ 封送处理 ]感到困惑。 Your data, either structure or array was not passed directly to C++ side, but marshal as what it is, that is, it's copied into a pinned memory, and then passed to the C++ code. 您的数据(结构或数组)没有直接传递到C ++端,而是按原样封送 ,即,将其复制到固定的内存中,然后传递给C ++代码。 The CharSet is not used to deside the physical length of data but how to interpret the characters. CharSet不是用来表示数据的物理长度,而是用来解释字符。


The physical bytes of Char is two; Char的物理字节为两个; but byte is one. 但是byte是1。 Your data might be truncated. 您的数据可能会被截断。

Have a look of char and byte . 看一下charbyte

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#-从第三方库导入类并使其成为派生类(或类似的东西) - C# - import class from third-party library and make it a derived class (or something similar) Visual Studio 中是否有针对 C# 的第三方分析工具? - Are there any third-party profiling tools for C# in Visual Studio? 需要由第三方实现的C#接口 - C# Interface that needs to be implemented by a third-party 来自 Azure 函数的 C# HttpClient POST 请求带有用于第三方 API 的授权标记,被剥离了标头和正文 - C# HttpClient POST requests from Azure Function with Authorization tag intended for third-party API are stripped of Headers and Body 如何使用C#查找第三方应用程序(如Google Earth)的安装目录? - How to find the installation directory of a third-party application, such as Google Earth, using C#? 我不能使用第三方 dll C# ASP .NET 4.8 - I can't use a third-party dll C# ASP .NET 4.8 从Excel调用时,第三方DLL起作用,而从VisualBasic / C#调用时,第三方DLL崩溃 - The third-party DLL works when called from Excel and crashes in the case of call from VisualBasic/C# 如何正确地将字符串从非托管第三方dll返回到C#? - How do I properly return a string from an unmanaged third-party dll to C#? 如何在包含第三方DLL文件的同时部署C#应用程序? - How to deploy a C# application while including third-party DLL files? 引用非托管第三方程序集的C#项目在Debug中构建良好,但在Release配置中失败 - The C# project referencing unmanaged third-party assembly builds fine in Debug, but fails in Release configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM