关于C#调用C的DLL传参问题(急急)!

YYBYYB1984 2008-10-19 03:12:10
C语言中封装的方法:
int winsjjh( const char * forms, const char *dwbh, const char *instr,
   const char * infile, char *outstr, char *outfile )
forms:编码
dwbh:编
instr:上传结构
infile:上传文件名
outstr:下传结构
outfile:下传文件名

上传下传结构定义:
struct djz
{
  char jym[7];
  char dwbh[9];
  char wdlsh[10];
  char czy[5];
char jyrq[9];
char dkpz[10];
  char cch[3];  
  char ccms[40];  
char rev1[30];
char rev2[100];
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace BankConnectForm
{

[StructLayout(LayoutKind.Sequential)]
public struct djz
{
public string jym;
public string dwbh;
public string wdlsh;
public string czy;
public string jyrq;
public string dkpz;
public string cch ;
public string ccms;
public string rev1;
public string rev2;
}

public partial class FrmBank : Form
{
[DllImport("netwin.dll")]
public static extern int winsjjh(
string forms,
string dwbh,
djz instr,
string infile,
ref djz outstr,
string outfile
);
public FrmBank()
{
InitializeComponent();
}
private void btnUP_Click(object sender, EventArgs e)
{
djz d1=new djz();
d1.jym = "jym";
d1.dwbh = "dwbm";
d1.wdlsh = "wdbh";
d1.czy = this.textBox4.Text.ToString().Trim();
d1.jyrq = this.dateTimePicker1.Value.ToString("yyyyMMdd");
string infile=this.txtFilePath.Text .ToString ();
djz d2=new djz();
int i;
string strTemp="";
i=winsjjh("116102", "38161720",d1, infile, ref d2, strTemp);
}

}
}

返回的i一直不对,请高手指点下
,这里的参数该如何匹配??


...全文
273 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
YYBYYB1984 2008-10-20
  • 打赏
  • 举报
回复
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
指定大小后会出现这样的错误。

有没有C#调用C程序dll的参数对应关系啊。
net0003 2008-10-19
  • 打赏
  • 举报
回复
li45214521 2008-10-19
  • 打赏
  • 举报
回复
想用哪个用哪个
不过在堆上分配的内存有点问题,好像有保护的内存作用
byte 相当于c语言的char
char 相当于c++的wchar_t
YYBYYB1984 2008-10-19
  • 打赏
  • 举报
回复
那如何判断是用byte还是string 呢??
li45214521 2008-10-19
  • 打赏
  • 举报
回复

[StructLayout(LayoutKind.Sequential)]
public struct djz
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=7 )]
public string jym;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=9 )]
public string dwbh;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=10 )]
public string wdlsh;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=5 )]
public string czy;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=9 )]
public string jyrq;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=10 )]
public string dkpz;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=3 )]
public string cch ;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=40 )]
public string ccms;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=30 )]
public string rev1;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=100 )]
public string rev2;
}

Deathsign 2008-10-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 li45214521 的回复:]
C# code
[StructLayout(LayoutKind.Sequential)]
public unsafe struct djz
{
public fixed byte jym[7];
public fixed byte dwbh[9];
public fixed byte wdlsh[10];
public fixed byte czy[5];
public fixed byte jyrq[9];
public fixed byte dkpz[10];
public fixed byte cch[3];  
public fixed byte ccms[40];  
public fixed byte re…
[/Quote]

必须把解决方案的 不安全模式开开菜能编译
Deathsign 2008-10-19
  • 打赏
  • 举报
回复
int winsjjh( const char * forms, const char *dwbh, const char *instr,
   const char * infile, char *outstr, char *outfile )


const char 分2个情况
传入用string
如果是传出必须用StringBuilder
li45214521 2008-10-19
  • 打赏
  • 举报
回复

[StructLayout(LayoutKind.Sequential)]
public unsafe struct djz
{
public fixed byte jym[7];
public fixed byte dwbh[9];
public fixed byte wdlsh[10];
public fixed byte czy[5];
public fixed byte jyrq[9];
public fixed byte dkpz[10];
public fixed byte cch[3];  
public fixed byte ccms[40];  
public fixed byte rev1[30];
public fixed byte rev2[100];
}

YYBYYB1984 2008-10-19
  • 打赏
  • 举报
回复
确实全部为指针,这个接口我没有源代码,只有DLL,根据文件说明,确实如此。
必须要指定大小??
superliu1122 2008-10-19
  • 打赏
  • 举报
回复
[StructLayout(LayoutKind.Sequential)]
public struct djz
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=7 )]
public string jym;
public string dwbh;
public string wdlsh;
public string czy;
public string jyrq;
public string dkpz;
public string cch ;
public string ccms;
public string rev1;
public string rev2;
}

要指定大小

C语言中封装的方法:
int winsjjh( const char * forms, const char *dwbh, const char *instr,
   const char * infile, char *outstr, char *outfile )
你这个函数不对吧 怎么参数全是字符指针


110,553

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧