当前位置:编程学习 > C#/ASP.NET >>

急急急急急急!!!!一个DELPHI代码如何转换成c#代码?(附代码)

1、dll接口说明
功能将报税文件*.bin转换为 excle文件 并存放在指定的目录中。
int __stdcall FPMX2ExcelFile(unsigned char *pathstr, //*.bin文件的路径
      int pathlen,                                   //*bin 文件路径的长度
      unsigned char *destpathstr,                    //*.xls文件的路径
      int destpathlen,                               //*.xls文件的路径长度
      unsigned char *stationName,                    //油站名称
      int stationNameLen)                            //油站名称长度

返回值说明
成功 为 1
失败 小于1   
 -1 //*.bin源路径为空
 -2//*.xls目标路径为空
 -3 //*.bin源路径不存在
 -4 //*.xls目标路径不存在
 -5 //mxsy.bin 索引文件格式错误
 -6 //*.bin 明细文件打开失败
 -7 //*.bin明细文件格式错误

DELPHI代码为:
procedure TForm1.btn1Click(Sender: TObject);
type
  TPuFun = function (pathstr:PChar;pathlenge:Integer;destpathstr:PChar;destlenge:Integer;stationName:PChar;stationlenge:Integer):Integer; stdcall;
var
  Th: THandle;
  Tf: TPuFun;
  str1,str2,str3:string;
  iii:Integer;
begin
  str1:='c:\';
  str2:='c:\';
  str3:='rrr';
  Th := LoadLibrary(PChar('toexcelprj.dll'));
  @tf := GetProcAddress(Th, PChar('FPMX2ExcelFile'));
  iii:=Tf(PChar(str1),3,PChar(str2),3,PChar(str3),3);
  ShowMessage(IntToStr(iii));
end; 


我写的c#代码为:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport("C:\\WINDOWS\\system32\\toexcelprj.dll", EntryPoint = "FPMX2ExcelFile", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
       
        public static extern int FPMX2ExcelFile(string pathstr1, int pathlen, string pathstr2, int destpathlen, string stationName, int stationNameLen);
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string pathstr1 = "C:\\";
            int pathlen = 3;
            string pathstr2 = "C:\\";
            int destpathlen = 3;
            string stationName = "abcd";
            int stationNameLen = 3;

            int i = FPMX2ExcelFile(pathstr1, pathlen, pathstr2, destpathlen, stationName, stationNameLen);
            MessageBox.Show(i.ToString());



        }

       
    }
}


正确的输出应该是1,DELPHI的输出也是1,二我写的C#程序的输出为-1.不知道什么原因
--------------------编程问答-------------------- 为了让它不沉,只能自己顶了 --------------------编程问答--------------------             string stationName = "abcd"; 
            int stationNameLen = 3; 

abcd长度是4吧 --------------------编程问答-------------------- 还是不行,不知道还有其他错的地方吗 --------------------编程问答-------------------- 搞定了,谢谢,
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,