如题!

解决方案 »

  1.   

    动态:loadlibary()
    静态:将you.dll的you.lib和这个DLL提供的接口*.h加入工程里
      

  2.   

    在.cpp文件中包含:
    #import "XXX.dll" no_namespace, named_guids在使用DLL的函数中:
    IXXXXX pat;
    try
    {
    pat.CreateInstance(__uuidof(XXXXX));
    }
    catch (...)
    {
                 .....
    }
    IXXXXX,和XXXXX在你的XXX.tlh里
    例:
    ....
    struct __declspec(uuid("df9ab796-79bd-4fde-925b-abb355414ef5"))
    RTPAudio;
        // [ default ] interface IRTPAudiostruct __declspec(uuid("a71d1e34-fd2d-473b-9a43-423c136de2af"))
    IRTPAudio : IDispatch
    {
    .....
    上面就写为: IRTPAudioPtr pat;
    try
    {
    pat.CreateInstance(__uuidof(RTPAudio));
    }
    catch (...)
    {
      

  3.   

    我用VC++建立了一个对话框工程,想在其中调用DELPHI写的一个串口取卡号数据的DLL文件(KETComm.dll)
    代码如下:
    unit CRCommUnit;interfaceuses SysUtils,Windows,scktcomp,classes;type
      TCRComm=class(TPersistent)
      private
        CRComID:THandle; //读卡器通讯串口设备句柄
        CRBuffer:string;
        StrRead:string;
        ReadBuf:array [0..500] of Byte;
        function CRRead:string;
      public
        constructor Create;
        destructor Destroy; override;
        function OpenCom(ComPort:string;BaudRate:integer):integer;
        function CloseCom:integer;
        function SetComHandle(SetComID:integer):integer;
        function ReadCardSN(var CRID:integer;CardSN:PChar;CardType,CodeType:integer):integer;
        function ClearBuffer:integer;
      end;
    implementation//=======================================
    // function ByteToBCD(value:byte):string;
    // 功  能:将字节转换为BCD码
    // 参  数:value --一个字节
    // 返回值:BCD码
    //========================================
    function ByteToBCD(value:byte):string;
    begin
      Result:=IntToHex(value,2);
    end;
    //=======================================
    // function BCDToByte(A:char;B:char):byte;
    // 功  能:将BCD码转换为字节
    // 参  数:A --第一个字符
    //         B --第二个字符
    // 返回值:字节
    //========================================
    function BCDToByte(A:char;B:char):byte;
    begin
      if A in ['0'..'9'] then Result:= Ord(A)-48
      else Result:=ord(A)-55;
      if B in ['0'..'9'] then Result:= Result*16+Ord(B)-48
      else Result:=Result*16+ord(B)-55;
    end;function DecToBCD(val:integer):byte;
    var
     i,j:integer;
    begin
      i:=val div 10;
      j:=val mod 10;
      Result:=i*16+j;
    end;function BCDToDec(val:byte):byte;
    var
     i,j:integer;
    begin
      i:=val div 16;
      j:=val mod 16;
      Result:=i*10+j;
    end;function HexToDec(HexChar:string):byte;
    var
      DecH,DecL:Byte;
    begin
      if Length(HexChar) > 1 then
      begin
        if HexChar[1] in ['0'..'9'] then DecH:= ord(HexChar[1])-48
        else DecH:=ord(HexChar[1])-55;
        if HexChar[2] in ['0'..'9'] then DecL:= ord(HexChar[2])-48
        else DecL:=ord(HexChar[2])-55;
        Result:=DecH*10+DecL;
      end
      else begin
        if HexChar[1] in ['0'..'9'] then DecL:= ord(HexChar[1])-48
        else DecL:=ord(HexChar[1])-55;
        Result:=DecL;
      end;
    end;//================================================
    // 读卡器函数
    //================================================
    constructor TCRComm.Create;
    begin
      inherited Create;
      CRComID:=0;
    end;destructor TCRComm.Destroy;
    beginend;//==================================
    // function CRCloseCom:integer;
    // 功  能: 关闭读卡器通讯端口
    // 参  数:无
    // 返回值:= 0  关闭成功
    //         = -1 关闭失败
    //==================================
    function TCRComm.CloseCom:integer;
    begin
      if not CloseHandle(CRComID) then  Result := -1
      else begin
        CRComID:=0;
        Result := 0;
      end;
    end;
    //================================================
    // function CROpenCom(ComPort:string;BaudRate:integer):integer;
    // 功  能: 打开读卡器通讯端口
    // 参  数: ComPort - 通讯串口号
    //          BaudRate     -波特率
    // 返回值:>0 :  打开成功,返回串口的设备句柄
    //         = -1:  打开失败
    //================================================
    function TCRComm.OpenCom(ComPort:string;BaudRate:integer):integer;
    var
      MyDCB:TDCB;
    begin
      if CRComID>0 then CloseCom;
      Result := -1;
      CRComID := CreateFile(Pchar(ComPort),
                          GENERIC_READ or GENERIC_WRITE,
                          0,
                          Nil,
                          OPEN_EXISTING,
                          0,
                          0);
      if (CRComID = INVALID_HANDLE_VALUE) then Exit;
      if not GetCommState(CRComID,MyDCB) then Exit;
      MyDCB.BaudRate :=BaudRate;
      MyDCB.ByteSize := 8;            // 8 Bits 数据
      MyDCB.StopBits := ONESTOPBIT;   // 一位停止位
      MyDCB.Parity:=0;
      if not SetCommState(CRComID,MyDCB) then Exit;
      if not SetupComm(CRComID,200,200) then Exit;
      SetCommMask(CRComID,EV_TXEMPTY);  // 准备在 WaitCommEvent 中等待 TxR 信号.
      CRBuffer:='';
      Result := CRComID;
    end;function TCRComm.SetComHandle(SetComID:integer):integer;
    begin
      CRComID:=SetComID;
      Result:=0;
    end;//================================================
    // function CRClearBuffer:integer;
    // 功  能: 清除读卡器缓冲区中的数据
    // 返回值:=0 :  成功
    //         = 其它值: 失败
    //================================================
    function TCRComm.ClearBuffer:integer;
    var
      ErrorFlag:DWord;
      Size:Dword;
      cs:TCOMSTAT;
      TempBuf:array[0..300] of byte;
    begin
      ClearCommError(CRComID,ErrorFlag,@cs);
      if cs.cbInQue>300 then cs.cbInQue:=300;
      ReadFile(CRComID,TempBuf,cs.cbInQue,Size,nil) ;
      CRBuffer:='';
      Result:=0;
    end;//================================================
    // function CRRead:string;
    // 功  能: 读读卡器数据
    // 返回值: 读取的数据
    //================================================
    function TCRComm.CRRead:string;
    var
      ErrorFlag:DWord;
      i:integer;
      Size:Dword;
      cs:TCOMSTAT;
      DataLen:integer;
      BeginPos,EndPos:integer;
    begin
      Result:='';
      StrRead:='';
      ClearCommError(CRComID,ErrorFlag,@cs);
      if cs.cbInQue>300 then cs.cbInQue:=300;
      ReadFile(CRComID,ReadBuf,cs.cbInQue,Size,nil) ;
      for i := 0 to Size - 1 do
      begin
        strRead := strRead+Chr(ReadBuf[i]);
      end;
      strRead:=CRBuffer+strRead;  //合并数据
      BeginPos:=Pos(Chr($7F),strRead);
      if BeginPos>0 then
        Delete(strRead,1,BeginPos);
      DataLen:=Length(strRead);
      if DataLen<16 then
      begin
        CRBuffer:=strRead;
        exit;
      end;
      EndPos:=Pos(#$0D#$0A,strRead);
      if EndPos>0 then
      begin
        DataLen:=Length(strRead);
        CRBuffer:=Copy(strRead,EndPos+2,DataLen-EndPos-1);
        if EndPos=15 then
        begin
          Result:=Copy(strRead,1,14);
        end;
      end;
    end;
    //================================================
    // function CRReadCardSN(var CRID:integer;CardSN:PChar;CardType,CodeType:integer):integer;
    // 功  能: 取得读卡器读取的卡号
    // 参  数: CRID   - 返回读卡器编号
    //          CardSN - 读取卡的序列号
    //          CardType - 维根输出格式
    //               =0 支持26BIT
    //               =1 支持36BIT
    //               =2 支持44BIT
    //               =3 支持34BIT
    //          CodeType - 卡号获取方法
    //               =0 全部BIT作为卡片编号,不足5字节,高位补0
    //               =1 除去校验位,取全部BIT,不足5字节,高位补0
    //               =2 除去校验位,取低位2字节,高位补0
    //               =3 除去校验位,取低位3字节,高位补0
    //               =4 除去校验位,取低位4字节,高位补0
    //               =5 除去校验位,取低位5字节
    // 返回值: =0  读取成功
    //          =其它值 读取失败
    //================================================
    function TCRComm.ReadCardSN(var CRID:integer;CardSn:PChar;CardType,CodeType:integer):integer;
    var
      i,j:integer;
      ReadCardSn:string;
      TempBuf:string;
      CheckByte:Byte;
      CardBuf:array [0..9] of Byte;
      OddBit,EvenBit:Byte;
      TempByte:Byte;
      StartPos:integer;
      

  4.   

    intCardCode:Longint;
      strCardCode:string;
    begin
      Result := -1;
      TempBuf:=CRRead;
      if TempBuf='' then exit;
      if Length(TempBuf) < 14 then exit;
      CRID:=BCDToByte(TempBuf[1],TempBuf[2]);
      //卡号检验
      CheckByte:=BCDToByte(TempBuf[1],TempBuf[2]) ;
      for i:=0 to 4 do
        CheckByte:=CheckByte xor BCDToByte(TempBuf[2*i+3],TempBuf[2*i+4]) ;
      if CheckByte <> BCDToByte(TempBuf[13],TempBuf[14]) then exit;
      for i:=0 to 9 do
        CardBuf[i]:=BCDToByte('0',TempBuf[i+3]);
      case CodeType of
        0:begin //0-全部BIT作为卡片编号,不足5字节,高位补0
          case CardType of
            0:begin//支持26BIT
              //生成奇校验码
              OddBit:=1;
              for i:=0 to 2 do
              for j:=0 to 3 do
                OddBit:=OddBit xor ((CardBuf[7+i] shr j) and 1) ;
              //生成偶校验码
              EvenBit:=0;
              for i:=0 to 2 do
              for j:=0 to 3 do
                EvenBit:=EvenBit xor ((CardBuf[4+i] shr j) and 1) ;
              ReadCardSn:='00';
              TempByte:=(EvenBit shl 1) + (CardBuf[4] shr 3 );
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[4] and 7) shl 5)+ (CardBuf[5] shl 1) + (CardBuf[6] shr 3);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[6] and 7) shl 5)+ (CardBuf[7] shl 1) + (CardBuf[8] shr 3);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[8] and 7) shl 5)+ (CardBuf[9] shl 1) + OddBit;
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
            end;
            1:begin//支持36BIT
              //生成奇校验码
              OddBit:=1;
              for i:=0 to 3 do
              for j:=0 to 3 do
                OddBit:=OddBit xor ((CardBuf[2+i] shr j) and 1) ;
              OddBit:=OddBit xor ((CardBuf[6] shr 3) and 1);
              //生成偶校验码
              EvenBit:=0;
              for i:=0 to 3 do
              for j:=0 to 3 do
                EvenBit:=EvenBit xor ((CardBuf[6+i] shr j) and 1) ;
              EvenBit:=EvenBit xor ((CardBuf[6] shr 3) and 1);
              ReadCardSn:='';
              TempByte:=(OddBit shl 3) + (CardBuf[2] shr 1 );
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[2] and 1) shl 7)+ (CardBuf[3] shl 3) + ((CardBuf[4] and $F) shr 1);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[4] and 1) shl 7)+ (CardBuf[5] shl 3) + ((CardBuf[6] and $F) shr 1);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[6] and 1) shl 7)+ (CardBuf[7] shl 3) + ((CardBuf[8] and $F) shr 1);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[8] and 1) shl 7)+ (CardBuf[9] shl 3) + EvenBit;
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
            end;
            2:begin//支持44BIT
              ReadCardSn:=Copy(TempBuf,3,10);
            end;
            3:begin//支持34BIT
              //生成奇校验码
              OddBit:=1;
              for i:=0 to 3 do
              for j:=0 to 3 do
                OddBit:=OddBit xor ((CardBuf[6+i] shr j) and 1) ;
              //生成偶校验码
              EvenBit:=0;
              for i:=0 to 3 do
              for j:=0 to 3 do
                EvenBit:=EvenBit xor ((CardBuf[2+i] shr j) and 1) ;
              ReadCardSn:='';          TempByte:=(EvenBit shl 1) + (CardBuf[2] shr 3 );
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[2] and 7) shl 5)+ (CardBuf[3] shl 1) + (CardBuf[4] shr 3);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[4] and 7) shl 5)+ (CardBuf[5] shl 1) + (CardBuf[6] shr 3);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[6] and 7) shl 5)+ (CardBuf[7] shl 1) + (CardBuf[8] shr 3);
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
              TempByte:=((CardBuf[8] and 7) shl 5)+ (CardBuf[9] shl 1) + OddBit;
              ReadCardSn:=ReadCardSn+ByteToBCD(TempByte);
            end;
          end;
        end;
        1:begin//1-16进制除校验位,取全部字节,高位补0
          ReadCardSn:=Copy(TempBuf,3,10);
        end;
        2:begin//2-16进制除校验位,取低位2字节,高位补0
          ReadCardSn:='000000'+Copy(TempBuf,9,4);
        end;
        3:begin//3-16进制除校验位,取低位3字节,高位补0
          ReadCardSn:='0000'+Copy(TempBuf,7,6);
        end;
        4:begin//4-16进制除校验位,取低位4字节,高位补0
          ReadCardSn:='00'+Copy(TempBuf,5,8);
        end;
        5:begin//5-16进制除校验位,取低位5字节。
          ReadCardSn:=Copy(TempBuf,3,10);
        end;
        6:begin //6-16进制3字节转10进制,取6位,高位补0
          Delete(TempBuf,1,2);
          intCardCode:=BCDToByte(TempBuf[5],TempBuf[6]) shl 16
                     + BCDToByte(TempBuf[7],TempBuf[8]) shl 8
                     + BCDToByte(TempBuf[9],TempBuf[10]);
          strCardCode:=Format('%0.16d',[intCardCode]);
          Delete(strCardCode,1,10);
          ReadCardSn:='0000'+strCardCode;
        end;
        7:begin //7-16进制4字节转10进制,取6位,高位补0
          Delete(TempBuf,1,2);
          intCardCode:=BCDToByte(TempBuf[3],TempBuf[4]) shl 24
                     + BCDToByte(TempBuf[5],TempBuf[6]) shl 16
                     + BCDToByte(TempBuf[7],TempBuf[8]) shl 8
                     + BCDToByte(TempBuf[9],TempBuf[10]);
          strCardCode:=Format('%0.16d',[intCardCode]);
          Delete(strCardCode,1,10);
          ReadCardSn:='0000'+strCardCode;
        end;
      end;
      StrPCopy(CardSn,ReadCardSn);
      Result:=0;
    end;end.小弟是初学者,请大侠们把操作步骤写详细些,不胜感激!