小编xur*_*urc的帖子

在C#中使用Delphi的stuct数组和字符串

我一直在尝试以下列方式调用在Delphi中创建的方法:

 function _Func1(arrParams: array of TParams): Integer;stdcall;    

 type 
   TParams = record
   Type: int;
   Name: string;
   Amount : Real;
 end;
Run Code Online (Sandbox Code Playgroud)

我的代码是:

[DllImport("some.dll", EntryPoint = "_Func1", CallingConvention = CallingConvention.StdCall)]
public static extern int Func(
  [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.Struct)] TParams[] arrParams)
Run Code Online (Sandbox Code Playgroud)

结构是:

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TParams
{
  public int Type;
  [MarshalAs(UnmanagedType.AnsiBStr)]
  public string Name;
  public double Amount;
}
Run Code Online (Sandbox Code Playgroud)

当我调用此方法时,我收到错误:无法编组"TParams"类型的字段"名称":无效的托管/非托管类型组合(字符串字段必须与LPStr,LPWStr,BStr或ByValTStr配对).

然而,这些组合都不起作用,因为Delphi的字符串以其长度为前缀并且肯定是Ansi(我已经尝试了其他字符串参数).有谁有线索如何解决这个问题?

c# delphi pinvoke

4
推荐指数
1
解决办法
2090
查看次数

System.Data.SQLite BackupDatabase()抛出"不是错误"

我正在使用System.Data.SQLite(版本1.0.85.0)来连接我的SQLite数据库.我想使用SQLiteConnection.BackupDatabase()方法创建数据库备份,但是它会抛出SQLiteException并显示消息:"not an error"(重复两次).这是一些代码:

SQLiteConnection cnnIn = new SQLiteConnection("Data Source=test.db;foreign keys=True");
SQLiteConnection cnnOut = new SQLiteConnection("Data Source=backup.db;foreign keys=True");
cnnIn.Open();
cnnOut.Open();
cnnIn.BackupDatabase(cnnOut, "backup", "test", -1, null, -1);
cnnIn.Close();
cnnOut.Close();
Run Code Online (Sandbox Code Playgroud)

错误来自 System.Data.SQLite.SQLite3.InitializeBackup(SQLiteConnection destCnn, String destName, String sourceName)

有谁知道什么可能是错的?

PS.我必须说错误信息有点误导:)

c# system.data.sqlite

2
推荐指数
1
解决办法
4675
查看次数

标签 统计

c# ×2

delphi ×1

pinvoke ×1

system.data.sqlite ×1