如何在 C# 中读取打包记录中的固定大小字符串的 Delphi 数组
我需要将数据库中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了...
将struct String20声明为
显然,固定长度字符串(即delphi ShortString)开头的字符串大小标识符只有1个字节。
然后将 header_identifiers 的定义更改为:
还发现 Delphi Packed Boolean 需要强制转换为
Figured it out...
Declared a struct String20 as
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:
Also found that Delphi Packed Boolean need to be cast as
我的第一条评论是“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.