如何在 C# 中读取打包记录中的固定大小字符串的 Delphi 数组

发布于 2024-08-19 23:57:52 字数 1335 浏览 4 评论 0原文

我需要将数据库中的 blob 字段读取到 ac# 应用程序中。

然而,Blob 字段是由 Delphi 应用程序使用以下方法写入数据库的:

 procedure WriteABlob(Blob : TBlobField; var Buffer; size : integer);
 var 
     s : String;
 begin
     setlength(s,size);
     move(buffer,s[1],Size);
     Blob.Value := S;
 end;

写入数据库的结构不是一个简单的结构,它包含类似

MyVariable : Array[0..3] of String[80];

或更糟的内容,其中一些包含

MyRecord = Packed Record
case byte of
    1: (
        iValue:Integer;
       )
    2: (
        cValue:Char;
       )
end;

我一直在尝试从数据库,然后使用

Marshal.PtrToStructure()

以便将其移动到结构中

我的结构定义如下:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1, Size = 10710)]
    public struct MyBlobField
    {
        ...
        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.AnsiBStr,SizeConst = SpecificArraySize)]
        public String[] ArrayofFixedLengthStrings;
        ...
    }

但是在调用 Marshal.PtrToStructure() 时出现错误:

无法封送字段 'ArrayofFixedLengthStrings' 类型 “MyBlobField”:无效 托管/非托管类型组合 (String[] 必须与 LPStr、LPWStr、BStr 的 ArraySubType 或 LPTStr)。

我想知道是否可以在 CustomMarshaler 上定义一个属性,它接受与 String[] 的配对,

有什么想法可以将 blob 的内容读入 c# 吗?

I need to read a blob field from a database into a c# app.

However the blob field was written to the database by a Delphi App using the following method:

 procedure WriteABlob(Blob : TBlobField; var Buffer; size : integer);
 var 
     s : String;
 begin
     setlength(s,size);
     move(buffer,s[1],Size);
     Blob.Value := S;
 end;

The Structure written to the database is not a simple structure and contains things like

MyVariable : Array[0..3] of String[80];

or worse some of them contain

MyRecord = Packed Record
case byte of
    1: (
        iValue:Integer;
       )
    2: (
        cValue:Char;
       )
end;

I've been trying reading in the bytes from the database and then using

Marshal.PtrToStructure()

in order to move it into the struct

My Struct is defined as follows:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1, Size = 10710)]
    public struct MyBlobField
    {
        ...
        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.AnsiBStr,SizeConst = SpecificArraySize)]
        public String[] ArrayofFixedLengthStrings;
        ...
    }

But i get an error when calling Marshal.PtrToStructure():

Cannot marshal field
'ArrayofFixedLengthStrings' of type
'MyBlobField': Invalid
managed/unmanaged type combination
(String[] must be paired with an
ArraySubType of LPStr, LPWStr, BStr or
LPTStr).

I was wondering if there was a attribute I could define on a CustomMarshaler which would accept a pairing with a String[]

Any ideas how I could read the contents of the blob into c#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-08-26 23:57:52

想通了...

将struct String20声明为

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct String20
    {
        [MarshalAs(UnmanagedType.U1)]
        public byte StringSize;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        public String Value;
    }

显然,固定长度字符串(即delphi ShortString)开头的字符串大小标识符只有1个字节。
然后将 header_identifiers 的定义更改为:

       [MarshalAs(UnmanagedType.ByValArray, SizeConst = max_header_identifiers)]
        public String20[] header_identifiers;

还发现 Delphi Packed Boolean 需要强制转换为

[MarshalAs(UnmanagedType.I1)]
public bool BooleanVar;

Figured it out...

Declared a struct String20 as

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct String20
    {
        [MarshalAs(UnmanagedType.U1)]
        public byte StringSize;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        public String Value;
    }

Apparently the string size identifier at the beginning of a fixed length string (i.e a delphi ShortString) is only 1 byte.
Then changed the definition of header_identifiers to:

       [MarshalAs(UnmanagedType.ByValArray, SizeConst = max_header_identifiers)]
        public String20[] header_identifiers;

Also found that Delphi Packed Boolean need to be cast as

[MarshalAs(UnmanagedType.I1)]
public bool BooleanVar;
扭转时空 2024-08-26 23:57:52

我的第一条评论是“String[80]”是一个固定长度的字符串(在结构中应该更容易处理),在 C# 中,您试图将它放入“String[]”中 - 这实际上是对字符串的引用(指针)。

我的下一个评论是,在最坏的情况下,您可以尝试将其读入字节数组,然后取出所需的字节并将它们操作到目标结构中。

My first comment is that the "String[80]" is a fixed-length string (which should be easier to handle in a structure), and in C# you are trying to put it into a "String[]" - which is actually a reference (pointer) to a string.

My next comment is that you could, at worst, try reading it into an array of bytes, and pull out the bytes you need and manipulate them into the destination structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文