var
  Form1: TForm1;implementation
  function MCS_InitComm(port: byte;baud: dword): short;stdcall;
           external 'MCS_SR.dll' name 'MCS_InitComm';
  function MCS_ExitComm: short;stdcall;
           external 'MCS_SR.dll' name 'MCS_ExitComm';
 function  MCS_ErrorMsg(wErrNo:word;bErrMsg:PChar):short;stdcall;
          external 'MCS_SR.dll' name 'MCS_ErrorMsg';
 function MCS_TestDoor:short;stdcall;
          external 'MCS_SR.dll' name 'MCS_TestDoor';
 Function MCS_SetStringMode  (bStringMode:byte) :short;stdcall;
          external 'MCS_SR.dll' name 'MCS_SetStringMode';
 Function MCS_GetStringMode  (bStringMode:byte) :short;stdcall;
          external 'MCS_SR.dll' name 'MCS_GetStringMode';
 Function IsCommOpened  () :short;stdcall;
          external 'MCS_SR.dll' name 'IsCommOpened';
 Function SLE4442_OpenCard (bATR:PChar) :short;stdcall;
          external 'MCS_SR.dll' name 'SLE4442_OpenCard';
 Function SLE4442_ReadChar(wAddr:integer;wLength:integer;bReadData:PChar):short;stdcall;
          external 'MCS_SR.dll' name 'SLE4442_ReadChar';
 Function SLE4442_VerifyPWD(bPassWord1:byte;bPassWord2:byte;bPassWord3:byte):short;stdcall;
          external 'MCS_SR.dll' name 'SLE4442_VerifyPWD';
 Function SLE4442_WriteChar(wAddr:integer;wLength:integer;bWriteData:PChar) :short;stdcall;
          external 'MCS_SR.dll' name 'SLE4442_WriteChar';
 Function SLE4442_ReadPAC(bPAC:PByte):short;stdcall
          external 'MCS_SR.dll' name'SLE4442_ReadPAC';
 Function SLE4442_CloseCard():short;stdcall;
          external 'MCS_SR.dll' name 'SLE4442_CloseCard';以上是DELPHI,请帮忙给翻译一下!翻译成C#!
十分感谢!

解决方案 »

  1.   

    太累了,告诉你类型转换对应,你自己写吧
    byte: byte
    dword: uint
    short: Int16
    word: UInt16
    pchar: [MarshalAs(UnmanagedType.LPTStr)]string
    如果是传出,用
    [MarshalAs(UnmanagedType.LPTStr)]StringBuilder
    integer: int
    PByte: [MarshalAs(UnmanagedType.LPArray)]byte[]
      

  2.   

    DELPHI代码如下:
    function MCS_InitComm(port: byte;baud: dword): short;stdcall;
               external 'MCS_SR.dll' name 'MCS_InitComm';我转换成C#如下 :
      public class NativeWIN32 
            { 
                [DllImport("MCS_SR.dll", SetLastError=true)] 
                public static extern int MCS_InitComm(byte bPort,int dwCommBaudRate
                    );                   }         private void button1_Click(object sender, System.EventArgs e)
            {  
                i=NativeWIN32.MCS_InitComm(0,115200);
            }
    但是错误!请愚翁给指正一下!
      

  3.   

    Have a try!
    [DllImport("MCS_SR.dll", SetLastError=true)]
    public static extern Int16 MCS_InitComm(byte bPort,uint dwCommBaudRate );
      

  4.   

    DELPHI代码如下:
    Function SLE4442_ReadChar(wAddr:integer;wLength:integer;bReadData:PChar):short;stdcall;
              external 'MCS_SR.dll' name 'SLE4442_ReadChar';C#代码如下:
     [DllImport("MCS_SR.dll", SetLastError=true)] 
                public static extern Int16 sle4442_readchar(int wAddr,int wLength,[MarshalAs(UnmanagedType.LPTStr)]string  bReadData);是读卡部分,现在是读卡出现问题!
    请指正,愚翁!(这个希望能详细一点,包括怎样定义STRING)
    十分感谢!
      

  5.   

    [DllImport("MCS_SR.dll", SetLastError=true)] 
                public static extern Int16 sle4442_readchar(int wAddr,int wLength,[MarshalAs(UnmanagedType.LPTStr)]string  bReadData);
    调用它时出错“无法在 DLL MCS_SR.dll 中找到名为 sle4442_readchar 的入口点。”
      

  6.   

    DELPHI代码中:
      Function MCS_SetStringMode  (bStringMode:byte) :short;stdcall;
              external 'MCS_SR.dll' name 'MCS_SetStringMode';//call
            MCS_SetStringMode(1);//设置成字符模式
            i:= SLE4442_OpenCard(pChar(str1));
    在C#中:
                [DllImport("MCS_SR.dll", SetLastError=true)] 
                public static extern bool MCS_SetStringMode(byte bStringMode);
    //call
    NativeWIN32.MCS_SetStringMode(1);
    在C#中读出来的全是“口”
    愚翁,再指正一下,快成功了!
      

  7.   

    对于string的传递,你在dllimport中加上charset标识试试
    [DllImport("MCS_SR.dll", CharSet=CharSet.Auto, SetLastError=true)]
      

  8.   

    可是好像不行的,读不出来!http://community.csdn.net/Expert/topic/5111/5111904.xml?temp=.1341669
    这里有代码,有时间的话,帮忙看看!
    十分感谢!
      

  9.   

    SLE4442_ReadChar
    SLE4442_ReadChar 函数从SLE4432/SLE4442卡片的存储区读取一串字符。此函数受MCS_SetStringMode影响。WORD SLE4442_ReadChar(
    WORD _wAddr,
    WORD _wLength,
    BYTE *_bReadData
    );
     
    参数
    _wAddr 
    指定要读取的字符串在卡片存储区内的起始地址。 _wLength 
    指定要读取的字符串长度。SLE4432/SLE4442存储区的长度为256字节。 _bReadData 
    字符型指针,指向从卡片存储区内读到的字符串并返回给应用程序。在HEXADECIMAL模式下,应用程序应确保在调用此函数前,已分配给_bReadData指针至少_wLength个字节的空间;在ASCIISTRING模式下,应确保已分配给_bReadData指针至少(_wLength * 2)个字节的空间。 返回值
    如果函数调用成功,返回值为0。如果函数调用失败,返回值不为0。如需得到错误描述,请调用MCS_ErrorMsg。说明
    此函数从卡片内的_wAddr地址起读取_wLength字节长的字符串,并存储在_bReadData指针内返回给应用程序。这是API函数介绍,给指正一下!和这个有关系没?
    谢谢
      

  10.   

    对于这个函数,你可以如下试试
    [DllImport("MCS_SR.dll")]
    public static extern Int16 SLE4442_ReadChar(int wAddr,int wLength,[Out, MarshalAs(UnmanagedType.LPArray)]byte[] bReadData);//Call
    byte[] bReadData = new byte[256];
    SLE4442_ReadChar( wAddr, wLength, bReadData );
      

  11.   

    愚翁,你好,你能不能抽空帮看看我的帖子.我的难题和这有点像.谢谢你啊
    http://community.csdn.net/Expert/topic/5111/5111598.xml?temp=.3217127