Using Delphi Dll in VB.Net 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ankur30884
    New Member
    • Sep 2007
    • 7

    Using Delphi Dll in VB.Net 2005

    HI

    I have a Dll which devloped in Delphi 7.0

    this dll woking perfevtly on delphi
    but when i try to connect this dll in .Net 2.0's windows application it got error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    how can i use Delphi dll in .net 2.0's Windows base application

    i try to both Windows API Style to communicate with dll <Import....> and Declare Function but i can't....
  • radcaesar
    Recognized Expert Contributor
    • Sep 2006
    • 759

    #2
    Can i have ur dllimport code..

    It should be like this

    [DllImport( "lib1.dll", CallingConventi on = CallingConventi on.StdCall, CharSet=CharSet .Unicode )]

    Comment

    • ankur30884
      New Member
      • Sep 2007
      • 7

      #3
      My friend this the code which is writed in .net windows application to communicate with DLL which is developed in Delphi 7 Second edition. i m using free edition of Delphi 7, i m not using code gear RAD Studio 2007


      <DllImport("COM M232DLL.dll", EntryPoint:="RS topComm", SetLastError:=T rue, CharSet:=CharSe t.Unicode, _
      'ExactSpelling: =True, CallingConventi on:=CallingConv ention.Winapi)> _
      'Public Shared Sub RStopComm()

      'End Sub

      '<DllImport("CO MM232DLL.dll", EntryPoint:="RO penComm", SetLastError:=T rue, CharSet:=CharSe t.Unicode, _
      'ExactSpelling: =True, CallingConventi on:=CallingConv ention.Winapi)> _
      'Public Shared Function ROpenComm(ByVal Port As Integer) As String

      'End Function

      '<DllImport("CO MM232DLL.dll", EntryPoint:="RR eadUID", SetLastError:=T rue, CharSet:=CharSe t.Unicode, _
      'ExactSpelling: =True, CallingConventi on:=CallingConv ention.Winapi)> _
      'Public Shared Function RReadUID(ByVal MACNo As Integer) As String

      'End Function


      and hear is my delphi code






      unit Unit1;

      interface

      uses
      Windows,WinProc s ,WinTypes,Messa ges, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls;

      //COMM232.DLL API Function
      function OpenComm(tt:int eger):integer;s tdcall;far;exte rnal'Comm232.dl l';
      procedure StopComm;stdcal l;far;external 'Comm232.dll' name 'StopComm';
      function ReadVersion(Mac No:integer;Vers ion:pchar):inte ger;stdcall;far ;external 'Comm232.dll'
      function SetLED(MacNo:in teger;SetVal:in teger):integer; stdcall;far;ext ernal'Comm232.d ll';
      function SetBell(MacNo:i nteger;SetVal:i nteger):integer ;stdcall;far;ex ternal'Comm232. dll';
      function SetNewMacNo(Mac No:integer;NewM acNo:integer):i nteger;stdcall; far;external'Co mm232.dll';
      function GetMacNo(MacNo: pint):integer;s tdcall;far;exte rnal'Comm232.dl l';
      function ReadBlock(MacNo :integer;block: integer;ReadOnl y:pint;RXD:pcha r):integer;stdc all;far;externa l'Comm232.dll';
      function ReadManyBlock(M acNo:integer;bl ock:integer;num ber:integer;RXD :pchar):integer ;stdcall;far;ex ternal'Comm232. dll';
      function WriteBlock(MacN o:integer;block :integer;TXD:pc har):integer;st dcall;far;exter nal'Comm232.dll ';
      function WriteManyBlock( MacNo:integer;b lock:integer;nu mber:integer;TX D:pchar):intege r;stdcall;far;e xternal'Comm232 .dll';
      function ReadUID(MacNo:i nteger;Number:p int;UID:pchar): integer;stdcall ;far;external'C omm232.dll';
      function LockBlock(MacNo :integer;block: integer):intege r;stdcall;far;e xternal'Comm232 .dll';

      //COMM232DLL Function Decl.
      procedure RStopComm();std call;export;
      function ROpenComm(port: integer):string ;stdcall;export ;

      function RReadUID(MACNo: integer):string ;stdcall;export ;

      function RReadBlock(MACN o: integer;BlockNo : integer;TBlockN o: integer):string ;stdcall;export ;
      function RWriteBlock(MAC No: integer;BlockNo : integer;data:st ring):string;st dcall;export;
      function RLockBlock(MACN o: integer;BlockNo : integer):string ;stdcall;export ;

      function RGetMacNo():str ing;stdcall;exp ort;
      function RSetMACNo(MACNo : integer;TMACNo: integer):string ;stdcall;export ;

      function RSetLED(MACNo: Integer;Status: boolean):intege r;stdcall;expor t;
      function RSetBell(MACNo: Integer;Status: boolean):intege r;stdcall;expor t;
      '

      library Comm232DLL;

      { Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }

      uses
      SysUtils,
      Classes,
      Unit1 in 'Unit1.pas';


      exports
      //AddLong;
      RStopComm,
      ROpenComm,
      RReadUID,
      RReadBlock,
      RWriteBlock,
      RLockBlock,
      RGetMACNo,
      RSetMACNo,
      RSetLED,
      RSetBell;
      end.



      Originally posted by radcaesar
      Can i have ur dllimport code..
      It should be like this

      [DllImport( "lib1.dll", CallingConventi on = CallingConventi on.StdCall, CharSet=CharSet .Unicode )]

      Comment

      • vyruz
        New Member
        • Feb 2008
        • 1

        #4
        hi there
        i have the exact same problem here
        how did u get rid of the stupid error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."???
        im using vb.net 2008 and i dont have the source code of the Delphi dll :(
        plz help me if u can
        thx

        Comment

        Working...