Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

DLL in Delphi

Halim1973Halim1973 Member Posts: 31
Please Help.
I have a DLL written in VB.
it's called MEIBAC.DLL. This DLL manages the bill acceptor mechanism.
it uses several functions like:
MarsOpen{to open the serial port }
MarsClose{To close the serial port}
etc..
All those functions are part of the DLL but I cannot figure out how to use them in my code using Delphi 6.
I used the following declarations:
Function MarsOpen(aMarsControl : MARS_CONTROL; PFunction:Integer):Integer;StdCall; external 'MEIBAC.DLL' name 'MarsOpen';
Function MarsClose(aMarsControl : MARS_CONTROL):Integer; StdCall; external 'MEIBAC.DLL' name 'MarsClose';


and I added a Button called Open;
LoadLibrary('MEIBAC.DLL');
MarsOpen(MarsControl,2);


I am having access violation error...
Do I need to load the DLL using the runtime method?
Or what is the solution to that?

Comments

  • zibadianzibadian Member Posts: 6,349
    : Please Help.
    : I have a DLL written in VB.
    : it's called MEIBAC.DLL. This DLL manages the bill acceptor mechanism.
    : it uses several functions like:
    : MarsOpen{to open the serial port }
    : MarsClose{To close the serial port}
    : etc..
    : All those functions are part of the DLL but I cannot figure out how to use them in my code using Delphi 6.
    : I used the following declarations:
    : Function MarsOpen(aMarsControl : MARS_CONTROL; PFunction:Integer):Integer;StdCall; external 'MEIBAC.DLL' name 'MarsOpen';
    : Function MarsClose(aMarsControl : MARS_CONTROL):Integer; StdCall; external 'MEIBAC.DLL' name 'MarsClose';
    :
    :
    : and I added a Button called Open;
    : LoadLibrary('MEIBAC.DLL');
    : MarsOpen(MarsControl,2);
    :
    :
    : I am having access violation error...
    : Do I need to load the DLL using the runtime method?
    : Or what is the solution to that?
    :
    The problem is that you are using two methods of linking to a library at the same time: static linking and dynamic linking.
    The statement "external" will automatically load the library while the program is loading. This is the static link. Because the code is compiled into the program and the library is automatically loaded at start-up you can simply use MarsOpen() and MarsClose() like normal functions.
    The LoadLibrary() function is used in conjunction with the GetProcAddress() and CloseLibrary() to dynamically link a library to the program. This method is more error prone since you need to do the type-casting at runtime instead of compile time. I can give you an example code of dynamic linking if you're interested, but in this case if I were you I'd use static linking.
  • Halim1973Halim1973 Member Posts: 31
    : : Please Help.
    : : I have a DLL written in VB.
    : : it's called MEIBAC.DLL. This DLL manages the bill acceptor mechanism.
    : : it uses several functions like:
    : : MarsOpen{to open the serial port }
    : : MarsClose{To close the serial port}
    : : etc..
    : : All those functions are part of the DLL but I cannot figure out how to use them in my code using Delphi 6.
    : : I used the following declarations:
    : : Function MarsOpen(aMarsControl : MARS_CONTROL; PFunction:Integer):Integer;StdCall; external 'MEIBAC.DLL' name 'MarsOpen';
    : : Function MarsClose(aMarsControl : MARS_CONTROL):Integer; StdCall; external 'MEIBAC.DLL' name 'MarsClose';
    : :
    : :
    : : and I added a Button called Open;
    : : LoadLibrary('MEIBAC.DLL');
    : : MarsOpen(MarsControl,2);
    : :
    : :
    : : I am having access violation error...
    : : Do I need to load the DLL using the runtime method?
    : : Or what is the solution to that?
    : :
    : The problem is that you are using two methods of linking to a library at the same time: static linking and dynamic linking.
    : The statement "external" will automatically load the library while the program is loading. This is the static link. Because the code is compiled into the program and the library is automatically loaded at start-up you can simply use MarsOpen() and MarsClose() like normal functions.
    : The LoadLibrary() function is used in conjunction with the GetProcAddress() and CloseLibrary() to dynamically link a library to the program. This method is more error prone since you need to do the type-casting at runtime instead of compile time. I can give you an example code of dynamic linking if you're interested, but in this case if I were you I'd use static linking.
    :
    thank you for the explanation.. it was really helpfull.
    But I am still having the access violation problem When I use STDCall.
    if I take Stdcall out, the access violation is not showing but the program is still not working.
    I am using the static DLL load.
  • Halim1973Halim1973 Member Posts: 31
    : : : Please Help.
    : : : I have a DLL written in VB.
    : : : it's called MEIBAC.DLL. This DLL manages the bill acceptor mechanism.
    : : : it uses several functions like:
    : : : MarsOpen{to open the serial port }
    : : : MarsClose{To close the serial port}
    : : : etc..
    : : : All those functions are part of the DLL but I cannot figure out how to use them in my code using Delphi 6.
    : : : I used the following declarations:
    : : : Function MarsOpen(aMarsControl : MARS_CONTROL; PFunction:Integer):Integer;StdCall; external 'MEIBAC.DLL' name 'MarsOpen';
    : : : Function MarsClose(aMarsControl : MARS_CONTROL):Integer; StdCall; external 'MEIBAC.DLL' name 'MarsClose';
    : : :
    : : :
    : : : and I added a Button called Open;
    : : : LoadLibrary('MEIBAC.DLL');
    : : : MarsOpen(MarsControl,2);
    : : :
    : : :
    : : : I am having access violation error...
    : : : Do I need to load the DLL using the runtime method?
    : : : Or what is the solution to that?
    : : :
    : : The problem is that you are using two methods of linking to a library at the same time: static linking and dynamic linking.
    : : The statement "external" will automatically load the library while the program is loading. This is the static link. Because the code is compiled into the program and the library is automatically loaded at start-up you can simply use MarsOpen() and MarsClose() like normal functions.
    : : The LoadLibrary() function is used in conjunction with the GetProcAddress() and CloseLibrary() to dynamically link a library to the program. This method is more error prone since you need to do the type-casting at runtime instead of compile time. I can give you an example code of dynamic linking if you're interested, but in this case if I were you I'd use static linking.
    : :
    : thank you for the explanation.. it was really helpfull.
    : But I am still having the access violation problem When I use STDCall.
    : if I take Stdcall out, the access violation is not showing but the program is still not working.
    : I am using the static DLL load.
    :
    Now I am having another problem.
    I need to use : MARSOpen(MArsControl, procedure1)
    procedure1 is written in Delphi and MARSOpen is in the DLL.
    The function MARSOPEN is declared :
    Function MarsOpen(aMarsControl : MARS_CONTROL; Ievent:Integer):Integer; stdcall; external 'MEIBAC.DLL' name 'MarsOpen';

    Procedure1 is an eventhandler but I have to pass it's memory address as an integer or so. Doing so the function MArsOpen will open Marscontrol and uses the Eventhandler procedure as an event handler.

  • zibadianzibadian Member Posts: 6,349
    : : : : Please Help.
    : : : : I have a DLL written in VB.
    : : : : it's called MEIBAC.DLL. This DLL manages the bill acceptor mechanism.
    : : : : it uses several functions like:
    : : : : MarsOpen{to open the serial port }
    : : : : MarsClose{To close the serial port}
    : : : : etc..
    : : : : All those functions are part of the DLL but I cannot figure out how to use them in my code using Delphi 6.
    : : : : I used the following declarations:
    : : : : Function MarsOpen(aMarsControl : MARS_CONTROL; PFunction:Integer):Integer;StdCall; external 'MEIBAC.DLL' name 'MarsOpen';
    : : : : Function MarsClose(aMarsControl : MARS_CONTROL):Integer; StdCall; external 'MEIBAC.DLL' name 'MarsClose';
    : : : :
    : : : :
    : : : : and I added a Button called Open;
    : : : : LoadLibrary('MEIBAC.DLL');
    : : : : MarsOpen(MarsControl,2);
    : : : :
    : : : :
    : : : : I am having access violation error...
    : : : : Do I need to load the DLL using the runtime method?
    : : : : Or what is the solution to that?
    : : : :
    : : : The problem is that you are using two methods of linking to a library at the same time: static linking and dynamic linking.
    : : : The statement "external" will automatically load the library while the program is loading. This is the static link. Because the code is compiled into the program and the library is automatically loaded at start-up you can simply use MarsOpen() and MarsClose() like normal functions.
    : : : The LoadLibrary() function is used in conjunction with the GetProcAddress() and CloseLibrary() to dynamically link a library to the program. This method is more error prone since you need to do the type-casting at runtime instead of compile time. I can give you an example code of dynamic linking if you're interested, but in this case if I were you I'd use static linking.
    : : :
    : : thank you for the explanation.. it was really helpfull.
    : : But I am still having the access violation problem When I use STDCall.
    : : if I take Stdcall out, the access violation is not showing but the program is still not working.
    : : I am using the static DLL load.
    : :
    : Now I am having another problem.
    : I need to use : MARSOpen(MArsControl, procedure1)
    : procedure1 is written in Delphi and MARSOpen is in the DLL.
    : The function MARSOPEN is declared :
    : Function MarsOpen(aMarsControl : MARS_CONTROL; Ievent:Integer):Integer; stdcall; external 'MEIBAC.DLL' name 'MarsOpen';
    :
    : Procedure1 is an eventhandler but I have to pass it's memory address as an integer or so. Doing so the function MArsOpen will open Marscontrol and uses the Eventhandler procedure as an event handler.
    :
    :
    Then you can simply type-cast the procedure as an integer in the following way:
    [code]
    MARSOpen(MArsControl, integer(procedure1));
    [/code]
Sign In or Register to comment.