Announcement

Collapse
No announcement yet.

Calling a PB DLL from IIS/ASP?

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

  • Calling a PB DLL from IIS/ASP?

    I'm developing a new website with IIS4.0 and most of the content
    on the webpage is going to be dynamic and I'm wanting to do some
    functions with powerbasic such as emailing users content when
    they login and/or logout. So I'm going to be making a DLL file
    for that. What I was wanting to know is how would I call the
    DLL file from an ASP file? Everything I've found on the web
    is about OLE/COM DLL files (activex) and they require the
    VBScript command (createobject) and to my knowledge that won't
    work for native DLL's. Any help is greatly appreciated.


    Thanks.

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    Two possible solutions are to either make a .TLB for your DLL and then you can call it "COM-Style" from ASP, or you can make an ISAPI dll and call your code from there. You could either make the ISAPI dll an "application" which handles its own output, or a filter, which can respond to tags in your output file (among many other abilities), like the ASP dll does.

    ------------------
    Troy King
    [email protected]
    Troy King
    katravax at yahoo dot com

    Comment


    • #3
      Originally posted by Troy King:
      Two possible solutions are to either make a .TLB for your DLL and then you can call it "COM-Style" from ASP, or you can make an ISAPI dll and call your code from there. You could either make the ISAPI dll an "application" which handles its own output, or a filter, which can respond to tags in your output file (among many other abilities), like the ASP dll does.

      I've heard of the .TLB format before what exactly is it and how
      would I make one? Is it to complicated? Would I be able to
      do it with PB/DLL?

      I have though about doing an ISAPI dll too but I really dislike this
      idea.

      Any other thoughts?

      ------------------
      -Greg
      -Greg
      [email protected]
      MCP,MCSA,MCSE,MCSD

      Comment


      • #4
        Originally posted by Gregery D Engle:
        I've heard of the .TLB format before what exactly is it and how
        would I make one? Is it to complicated? Would I be able to
        do it with PB/DLL?
        No, it's not that tough. It's a type library. VB generates one for you automatically when you create an ActiveX exe or ActiveX dll, and builds it into the binary file. All it is is a file that describes your interface in COM terms. There are two ways to make one, either using the IDL language (compiled with MIDL) or with the ODL language (compiled with MKTYPLIB). The ODL/MKTYPLIB is easier, but older than IDL/MIDL. Either way, you write a specially formatted text file describing your interfaces, and compile it. ActiveX clients can then refer ("set a reference") to the resulting .TLB file to get the definitions, and it calls into your dll/exe correctly using ActiveX-like syntax. MSDN is full of examples of both, and the book "Hardcore Visual Basic" contains a bit on it and TONS of samples, including TLBs (with either ODL or IDL source code, I forget which) for nearly the entire Win32 API. Earlier copies of the MSDN library even contain the entire text of the "Hardcore Visual Basic" book, but my April 2000 copy does not.

        Unfortunately, I haven't got a sample here at home; I'll see if I can modify the "proprietary" code at work to make an example and post it here. They're picky about anything resembling our code leaving the office .

        ------------------
        Troy King
        [email protected]
        Troy King
        katravax at yahoo dot com

        Comment


        • #5
          I'm too lazy to test, but I don't see reasons, why it's not possible to use ordinary dll,
          if to execute script(exe) is allowed.
          Anyway, there is PWS and it possible to test for localhost.

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

          Comment


          • #6
            Okay, threw together a quick .ODL for the "rounding" sample. Here's how they look:

            The PB Sample code (from rounding.bas)
            Code:
            #COMPILE DLL
            #INCLUDE "WIN32API.INC"
             
            FUNCTION LibMain(BYVAL hInstance   AS LONG, _
                             BYVAL fwdReason   AS LONG, _
                             BYVAL lpvReserved AS LONG) EXPORT AS LONG
             
              SELECT CASE fwdReason
             
                CASE %DLL_PROCESS_ATTACH
                  'Indicates that the DLL is being loaded by another process (a DLL
                  'or EXE is loading the DLL).  DLLs can use this opportunity to
                  'initialize any instance or global data, such as arrays.
             
                   LibMain = 1   'success!
             
                   'LibMain = 0   'failure!
                  EXIT FUNCTION
             
                CASE %DLL_PROCESS_DETACH
                  'Indicates that the DLL is being unloaded or detached from the
                  'calling application.  DLLs can take this opportunity to clean
                  'up all resources for all threads attached and known to the DLL.
             
                   LibMain = 1   'success!
             
                   'LibMain = 0   'failure!
                  EXIT FUNCTION
             
                CASE %DLL_THREAD_ATTACH
                  'Indicates that the DLL is being loaded by a new thread in the
                  'calling application.  DLLs can use this opportunity to
                  'initialize any thread local storage (TLS).
             
                   LibMain = 1   'success!
             
                   'LibMain = 0   'failure!
                  EXIT FUNCTION
             
                CASE %DLL_THREAD_DETACH
                  'Indicates that the thread is exiting cleanly.  If the DLL has
                  'allocated any thread local storage, it should be released.
             
                   LibMain = 1   'success!
             
                   'LibMain = 0   'failure!
                  EXIT FUNCTION
             
              END SELECT
             
              ' Any message which is not handled in the above SELECT CASE reaches
              ' this point and is unknown.
             
            END FUNCTION
             
            FUNCTION RoundNorm ALIAS "RoundNormal" (BYVAL dVal AS DOUBLE, BYVAL iPlaces AS LONG) EXPORT AS DOUBLE
                RoundNorm = SGN(dVal) * INT(ABS(dVal) * (10 ^ iPlaces) + 0.5) / (10 ^ iPlaces)
            END FUNCTION
             
            FUNCTION RoundUp ALIAS "RoundUp" (BYVAL dVal AS DOUBLE, BYVAL iPlaces AS LONG) EXPORT AS DOUBLE
                RoundUp = SGN(dVal) * INT(ABS(dVal) * (10 ^ iPlaces) + 0.9) / (10 ^ iPlaces)
            END FUNCTION
             
            FUNCTION RoundDown ALIAS "RoundDown" (BYVAL dVal AS DOUBLE, BYVAL iPlaces AS LONG) EXPORT AS DOUBLE
                RoundDown = SGN(dVal) * INT(ABS(dVal) * (10 ^ iPlaces)) / (10 ^ iPlaces)
            END FUNCTION
            And the .ODL file to go with it:
            Code:
            [
                uuid(7092C2A0-283B-11D4-9E25-00C04F4096BA),
                version(1.0),
                helpstring("Rounding Test")
            ]
            library PB_Rounding
            {
                [dllname("Rounding.dll")]
            	module RoundingFunctions
            	{
            		[entry("RoundNormal"            )] double stdcall RoundNormal     ([in] double dValue, [in] int iPlaces);
            		[entry("RoundUp"                )] double stdcall RoundUp         ([in] double dValue, [in] int iPlaces);
            		[entry("RoundDown"              )] double stdcall RoundDown       ([in] double dValue, [in] int iPlaces);
            	}; 
             
            };
            And how you'd call it from VB or ASP (sample here from VB):
            Code:
            MsgBox PB_Rounding.RoundDown(1.11111111, 3), vbOKOnly , "PB Rounding Sample"
            Before you can call that, you have to add a reference to the .TLB file created after you compile the ODL file.

            Hope this helps...

            ------------------
            Troy King
            [email protected]

            [This message has been edited by Troy King (edited July 10, 2000).]
            Troy King
            katravax at yahoo dot com

            Comment


            • #7
              Originally posted by Semen Matusovski:
              I'm too lazy to test, but I don't see reasons, why it's not possible to use ordinary dll,
              if to execute script(exe) is allowed.
              Anyway, there is PWS and it possible to test for localhost.
              It is possible to use a regular DLL if it conforms to ISAPI. It's just a regular DLL with a custom entry point.

              Also, if all Gregery wants to do is email users, that's possible with straight ASP using the CDONTS library or a third-party email object. It's just that I don't think there's a way to call a dll function from regular ASP without a TLB for the assist.

              ------------------
              Troy King
              [email protected]
              Troy King
              katravax at yahoo dot com

              Comment


              • #8
                Troy, when you refer to compiling the .ODL file, what compiler do you use?

                Also, does this 'compiled' .ODL file go in the same subdirectory, on the server, that the .DLL file resides?

                Finally, in IIS, does permission have to be allowed for executabless, or can a .DLL be accessed with only scripts 'allowed'?

                John Rayfield, Jr.


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

                Comment


                • #9
                  Gregory, they're making it way too confusing: we're already doing exactly what you want.
                  Here's how to do it:

                  You create an ASP page which in turn calls a VB ActiveX Dll (COM) which in turn calls the PBDLL.
                  You can choose whether or not to use optimization of the COM with the MTS (moving it to COM+) but that's a different trick.

                  VB ActiveX DLL:
                  the VB Class:
                  Make a sub or function with :
                  Dim hLib As Long
                  ' hook the PB DLL
                  hLib = LoadLibrary("<system dir>\<PB DLL name>.DLL")
                  If hLib = False Then
                  ' if it can't be hooked: forget it.
                  Exit sub / Function
                  End If

                  result = PBMain ' call your PB sub or function
                  Call FreeLibrary(hLib) ' free the DLL

                  the VB Module needs some declarations:
                  Declare <your PB DLL>

                  Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" _
                  (ByVal lpLibFileName As String) As Long

                  Declare Function FreeLibrary Lib "kernel32" _
                  (ByVal hLibModule As Long) As Long

                  DON'T FORGET:
                  Set the Project Properties to : 'Wait for component to be created'
                  Compile it.

                  ASP CODE:
                  <%
                  Dim X
                  ' create the VB object
                  set X = Server.createobject("<name of the VB project>.<class extenstion>")

                  Call Sub / Function '(the one in VB)


                  Basically, that does the trick and is very stable.
                  Good luck

                  J. Brouwers

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

                  Comment


                  • #10
                    Troy King,

                    Thanks for the ODL example, I messed around with it last night with
                    the examples I found on the MSDN site and it was just so
                    confusing to me. Your example looks good and I will mess around
                    with it when I get home.

                    One question. When I compile the ODL with VC++ and it makes the
                    .TLB file how would I register that without Visual Basic?

                    I know how to register it with Visual BAsic but I was just
                    wondering how I would without it.

                    Would regsvr32 "filename.tlb" work our do I have to do some other
                    tricks?


                    Semen,

                    Everything that I have found relating to VBscript and ASP is that
                    you have to use the CreateObject method and that requires
                    the dll to be registered with a guid and I can't can't do that
                    with normal PB/DLL files or native DLL files for that matter.

                    jeroen,

                    I like your idea and I have pondered that some as well. See at
                    first I was just using Webclasses within Visual Basic 6.0 but
                    it would appear to fail like every other time I compiled. It was
                    very sporadic in performance so I think I know hate Visual
                    Basic so I wish to stay away from that as much as possible
                    unless I am forced to deal with it again But thanks for
                    your example.



                    ------------------
                    -Greg
                    -Greg
                    [email protected]
                    MCP,MCSA,MCSE,MCSD

                    Comment


                    • #11
                      Originally posted by Gregery D Engle:
                      Troy King,

                      One question. When I compile the ODL with VC++ and it makes the
                      .TLB file how would I register that without Visual Basic?

                      You register it with REGTLIB mylib.tlb. I left something out in my quick sample, and that is how to give it a name so you can user Server.CreateObject("ObjectName") in ASP (though it works from VB by setting the reference). The sad part is I don't remember how to give it the name so it can be called from ASP, and I'm feeling pretty bad about leading you down the path without being able to provide that information. I'll bug another guy here that's done it and see if we can remember how to do it.

                      Originally by jeroen brouwers
                      Gregory, they're making it way too confusing
                      Jeroen, I don't think it's all that confusing, although I can't remember how to set the Library Name at the moment. Plus, this way he gets to avoid the overhead of the VB Runtime. It's back to the easy-vs-good argument, I guess, but some of us prefer to take the extra steps to make the result use less RAM. I mean if he's going to waste the RAM loading the VB Runtime anyway, he might as well write the entire object in VB. The couple of megs overhead is a bit much just to provide a simpler interface to the PB dll. My company also does it the way you mention, but it's only the beginners that do so (and no offense, I'm not suggesting you're a beginner). We've found that in the long run, the extra code for the TLB provides an overall easier-to-manage end product, and we can swap DLLs in and out without the hassle of stopping the server and going through the unregister-register game. Also, ISAPI applications aren't complicated either, certainly no more than VB COM objects, though filters can be tricky the first couple of times.


                      ------------------
                      Troy King
                      [email protected]
                      Troy King
                      katravax at yahoo dot com

                      Comment


                      • #12
                        Originally posted by John Rayfield:
                        Troy, when you refer to compiling the .ODL file, what compiler do you use?

                        Also, does this 'compiled' .ODL file go in the same subdirectory, on the server, that the .DLL file resides?

                        Finally, in IIS, does permission have to be allowed for executabless, or can a .DLL be accessed with only scripts 'allowed'?
                        ODLs are compiled with MKTYPLIB. The DLL doesn't have to go in the same path as the TLB; we usually drop it into SYSTEM32. I'm not certain, but I'm pretty sure that execute permission is not required, but that Scripts permission is enough. Now I need to remember how to create the library name so it can be used from ASP... The sample I posted works fine from VB (it was off the top of my head) but doesn't register an ole-server name that ASP can use.


                        ------------------
                        Troy King
                        [email protected]
                        Troy King
                        katravax at yahoo dot com

                        Comment


                        • #13
                          Troy,

                          I tried your example and I liked it but you are correct it won't
                          work with the CreateObject method in both VB/VBScript.

                          Any other ideas on how to get this working?

                          Thanks,
                          Greg

                          ------------------
                          -Greg
                          -Greg
                          [email protected]
                          MCP,MCSA,MCSE,MCSD

                          Comment


                          • #14
                            Creating a TLB and adding a reference in VB to your PB-DLL,
                            will only give you the ability to call routines in this PB-DLL
                            without declaring them.They will appear as a native part of VB.
                            Or more correct. You should not declare them at all.
                            With the object-browser (in VB) you will be able to browse your DLL




                            ------------------
                            Fred
                            mailto:[email protected][email protected]</A>
                            http://www.oxenby.se

                            Fred
                            mailto:[email protected][email protected]</A>
                            http://www.oxenby.se

                            Comment


                            • #15
                              Originally posted by Fred Oxenby:
                              Creating a TLB and adding a reference in VB to your PB-DLL,
                              will only give you the ability to call routines in this PB-DLL
                              without declaring them.They will appear as a native part of VB.
                              Or more correct. You should not declare them at all.
                              With the object-browser (in VB) you will be able to browse your DLL





                              We are trying to accomplish a way to load a PB-dll with VBScript
                              via the CreateObject command. To my knowledge VBScript really
                              only supports true OLE2/COM objects and we're trying to figure
                              out a way to get this working. Any thoughts are welcome.


                              ------------------
                              -Greg
                              -Greg
                              [email protected]
                              MCP,MCSA,MCSE,MCSD

                              Comment


                              • #16
                                Originally posted by Gregery D Engle:

                                To my knowledge VBScript really
                                only supports true OLE2/COM objects and we're trying to figure
                                out a way to get this working. Any thoughts are welcome.
                                I apologize for bringing up the subject of the TLB without having a way to call it from ASP... I could've sworn the ASP guys were using it, but after I did some digging, it turns out they were calling it from a VB object like Jeroen suggested. I was mistaken, and I'm sorry for that. It doesn't appear we found a way to call it from ASP. However, we did do a couple of ISAPI app dlls in PB, and those work fine in IIS, and were certainly faster than the ASP pages they replaced.


                                ------------------
                                Troy King
                                [email protected]
                                Troy King
                                katravax at yahoo dot com

                                Comment


                                • #17
                                  I've been watching this 'thread' because I'm also interested in calling functions within a DLL written in PowerBASIC from a 'script' written in VBScript. At this point, I'm 100% confused - is there a relatively 'simple' way to achieve this?

                                  John Rayfield, Jr.


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

                                  Comment


                                  • #18
                                    Originally posted by Troy King:
                                    I apologize for bringing up the subject of the TLB without having a way to call it from ASP... I could've sworn the ASP guys were using it, but after I did some digging, it turns out they were calling it from a VB object like Jeroen suggested. I was mistaken, and I'm sorry for that. It doesn't appear we found a way to call it from ASP. However, we did do a couple of ISAPI app dlls in PB, and those work fine in IIS, and were certainly faster than the ASP pages they replaced.


                                    Thats cool, it was a good idea though, hey I learned something
                                    neato though Looks like I'll have to use C++ then I refuse
                                    to go back to Visual Basic. I think I'll just make a C++ OLE/COM
                                    wrapper for a pb/dll but something u did say that sounded interesting
                                    was an ISAPI app in PB/DLL... I think that would be very nice.
                                    Is it hard to create? How?



                                    ------------------
                                    -Greg
                                    -Greg
                                    [email protected]
                                    MCP,MCSA,MCSE,MCSD

                                    Comment


                                    • #19
                                      Originally posted by Gregery D Engle:
                                      ...but something u did say that sounded interesting
                                      was an ISAPI app in PB/DLL... I think that would be very nice.
                                      Is it hard to create? How?
                                      Actually, pretty straight forward. There's even a sample that ships with PB/CC (but not with PB/DLL). The sample is available from the PB website here though: http://www.powerbasic.com/files/pub/...inet/isapi.zip . Note this is for an ISAPI app, not an ISAPI filter. However, it sounds like an ISAPI app is exactly what you want though. You can sort of think of it like a VB Webclass without the overhead; it runs in-process with IIS and you can have your app handle the output back to the user.

                                      ------------------
                                      Troy King
                                      [email protected]
                                      Troy King
                                      katravax at yahoo dot com

                                      Comment


                                      • #20
                                        Originally posted by John Rayfield:
                                        I've been watching this 'thread' because I'm also interested in calling functions within a DLL written in PowerBASIC from a 'script' written in VBScript. At this point, I'm 100% confused - is there a relatively 'simple' way to achieve this?
                                        John, I thought there was, but it turns out my office wasn't doing this like I thought we were, so now I'm not sure, without writing an OLE wrapper or calling it from a VB COM server. I apologize for the misinformation. I did task one of the other programmers with "remembering how we did it last time", though, and haven't told him I found we weren't doing it, so maybe he'll come up with something *grin*. I'll let you know directly if he learns how . I suppose it would also be possible using JA COM PB and creating a COM server with your DLL.... that just now occurred to me...

                                        ------------------
                                        Troy King
                                        [email protected]
                                        Troy King
                                        katravax at yahoo dot com

                                        Comment

                                        Working...
                                        X