Announcement

Collapse
No announcement yet.

Calling DLL from VB

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Calling DLL from VB

    I have a question about calling DLL's from VB using PB/DLL.
    Note: I do not understand this fully, so cut me a little slack, please.
    From a VB program, I want to call functions in a DLL written in C that was compiled using the stdCall method.
    If interested info on the DLL is at: http://gdal.velocet.ca/projects/shapelib/index.html
    I know VB only allows you to call DLL's that were compiled using the Pascal calling convention.
    I have also read that PB/DLL allows you to call either type.
    Could a simple PB/DLL DLL be written that could be called by VB, that in turn would call the C DLL, so that
    a VB program could use the C DLL? I am thinking that this would only be about three lines per function that you want to use.

    I know the best way would be to just write the application in PB/DLL to start with... But could this be done?
    This would be very handy for me in some situations.
    Thanks,
    Tom

    #2
    Yes it should be able to be done quite easily, since PB supports CDECL, and SDECL/STDCALL conventions (and also BDECL/Pascal).

    Anyway, it is really just a matter of converting the C headers to PowerBASIC.

    As an aside, I thought VB used SDECL/STDCALL rather than BDECL - how else would it work with PowerBASIC's default passing method (SDECL/STDCALL) unless you reversed the parameter order? (or is this my VB ignorance showing through?! )

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


      #3
      VB uses, of course, stdcall (otherwise how it calls Windows API functions ?)

      But a text of MSDN sometimes is not clear. For example: http://support.microsoft.com/support.../q161/3/01.asp
      "API functions are declared using the Pascal calling convention".
      Similar phrase is able to confuse everybody.

      ------------------
      E-MAIL: [email protected]

      Comment


        #4
        Semen,

        The Pascal call is a stdcall. A lot of older C header files
        still call for this convention, however the newer C compilers
        map this by the newer header files (standard C headers). In
        similar fashion, "near" and "far" meant something in 16-bit.
        In 32-bit, they just get mapped out and have no meaning.

        It is somewhat confusing though.

        Cheers,
        Cecil

        ------------------

        Comment


          #5
          Cecil --

          > The Pascal call is a stdcall.

          Probably I lost something, but I beleive that correctly to say "The Pascal call was replaced by stdcall in 32-bit C compilers".
          The Pascal calling convention was used in MS-DOS and 16-bit and, as I understand, meant passing parameters from left to right (unlike stdcall).

          ------------------
          E-MAIL: [email protected]

          Comment


            #6
            Da. It's easy to get confused about the API calling convention, as it was
            defined as PASCAL in 16-bit code. For compatibility purposes, Microsoft
            defines PASCAL in 32-bit code as meaning "stdcall"... except for a few
            places where it still means "Pascal"...!

            If you see PASCAL used in a C/C++ definition, though, it's 99.99% certain
            that the routine uses the "stdcall" calling convention.

            ------------------
            Tom Hanlin
            PowerBASIC Staff

            Comment


              #7
              Semen,

              Yes, you did miss it. Tom's explanation was more
              elegant than mine but with the same results.

              From the windef.h header file:

              #define pascal __stdcall

              Cheers,
              Cecil

              ------------------

              Comment


                #8
                Bear in mind that C is case-sensitive, so "pascal" is not the same as "PASCAL",
                and it's "PASCAL" that's used in the headers. Right idea, though!

                ------------------
                Tom Hanlin
                PowerBASIC Staff

                Comment


                  #9
                  Cecil, Tom --
                  can you explain terms "Basic, Pascal, Fortran, C, standart calling conventions" and a history of their appearence ?
                  In which order MS-DOS editions of these compilers pushed parameters into stack, caller or called function was responsible to clean a stack ?

                  BTW, I opened my old assembler function for Turbo Basic.
                  Actually it's set of functions with variable nomber of parameters.
                  To recognize "type-of-operation" I added additional parameter: Call Dos (x1, x2 ..., xn, type-of-function)
                  Why last instead of first ? Unlike I forgot all (10-12 years ago), it's clear that I have no another way, because Turbo Basic pushed parameters from left to right.

                  [This message has been edited by Semen Matusovski (edited May 16, 2001).]

                  Comment


                    #10
                    Tom,

                    You're absolutely right. I copied the wrong line from
                    the header file.

                    Should be: #define PASCAL __stdcall

                    Cheers,
                    Cecil

                    ------------------


                    [This message has been edited by Cecil Williams (edited May 16, 2001).]

                    Comment


                      #11
                      Shucks, I didn't even know there was a Fortran calling convention!
                      Really, that sort of historical research is more than I want to get into.
                      As near I recall, BASIC and Pascal shared a calling convention, but they
                      had a few different types (strings in particular). C passed parameters in
                      the reverse order and expected the main program to clean up the stack.
                      You can find details on the more popular conventions in your PB help file
                      under "Declare".

                      ------------------
                      Tom Hanlin
                      PowerBASIC Staff

                      Comment


                        #12
                        I read the PB/DLL help under declare, and that is where I got the idea for my original question.
                        The part that confuses me is the idea of who "cleans up" the stack, and exactly what that means.
                        My first idea was to use PB/DLL to write a DLL that simply had a declare for each function in
                        the "C" DLL and then a PB/DLL function for each "C" that basically called the same function.
                        So from VB I would call the PB function that would in turn call the C function. My guess is
                        that it isn't as easy as that... it never seems to be!
                        Would I also have to write some code in the PB functions to "clean up" the stack somehow?

                        Is there a way to write a "universal" function that would act as a "translator" so VB could
                        use it to call any function in a C DLL?

                        I think if one of you PB and C programmers could get me started as to how to do this for one or
                        two of the functions, I would be able to do the rest of them. Then I would have a way to use the
                        "shapefile" functions from VB. Would any one care to get me started?

                        Thanks,
                        Tom

                        Comment


                          #13
                          This has been discussed before. I'm inclined to think that the issue of who
                          cleans up the stack shouldn't even be mentioned in the manual, because it
                          seems to confuse people. Basically, it's just a technical detail. To be quite
                          specific, it's a technical detail that's handled by the compiler(s) for you,
                          and you don't need to deal with it at all unless you're writing routines in
                          assembly language.

                          Universal function translator... hmmm. Possible, perhaps, but it would be
                          extremely clunky. It does not strike me as likely to be a happy solution.

                          If the C routines use the STDCALL convention, which is fairly likely, you don't
                          need a translator at all. This is the first thing to investigate! If it turns out
                          that the C routines use the native CDECL convention, you'll definitely want a
                          PowerBASIC interface between the C DLL and VB.


                          ------------------
                          Tom Hanlin
                          PowerBASIC Staff

                          Comment


                            #14
                            Re-reading the original post, I see that the C DLL does use STDCALL. This is the same
                            calling method used by Visual Basic and PowerBASIC (by default). You don't need any
                            special interface between the DLL and VB in this case.

                            ------------------
                            Tom Hanlin
                            PowerBASIC Staff

                            Comment


                              #15
                              Looking back at the original post myself, I may have said it wrong. It is in the standard C method which is CDECl, I think.
                              I wish I knew more about it. At least I know that I don't have to mess with the stack in order to us PB/DLL to translate from the VB to the CDECL calling convention. So I guess it would just be a function that just calls another function with the same name.
                              I don't really know where to start, but I will try to see if I can guess my way through it and post it here to see if anyone can find a problem with it. Even a simple example would help me a lot.
                              Thanks for the help...
                              Tom

                              Comment


                                #16
                                The wrapper function can't have the same name as the real function, or there
                                won't be any way for the compiler to tell the difference.

                                Show us a declaration for one of the C routines and we'll post an example.

                                ------------------
                                Tom Hanlin
                                PowerBASIC Staff

                                Comment

                                Working...
                                X
                                😀
                                🥰
                                🤢
                                😎
                                😡
                                👍
                                👎