Announcement

Collapse
No announcement yet.

Full path of my DLL in runtime?

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

  • Full path of my DLL in runtime?

    I want to create a DLL for other programmers, and make it friendly enough so that people can rename my DLL without causing any hiccups.
    The only problem is that because my DLL needs to do a CRC check on itself first, it needs to know the full path location and filename of itself

    Peter Lameijn kindly posted the following example, but it assumes that you know the actual filename of your DLL - I need to find the full path of my DLL when it is loaded regardless of what the filename is. Can anybody help?
    Code:
    Function ReturnPath () Export As String
      Dim ZString As Asciiz * 256
      GetModuleFileName GetModuleHandle("mydll.dll"), ZString,SizeOf (ZString)
      Function = ZString
    End Function


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

  • #2
    Wayne --
    Use hInstance parameter inside LibMain

    Exe:
    Code:
       #Compile Exe
       #Dim All
       #Register None
       #Include "Win32Api.Inc"
    
       Declare Function DllPath Lib "Pb.Dll" As String
    
       Function PbMain()
          MsgBox DllPath
       End Function
    Dll
    Code:
       #Compile Dll "Pb.Dll"
       #Register None
       #Dim All
       #Include "WIN32API.INC"
       
       Global hInstDll As Long
       Function LibMain(ByVal hInstance   As Long, ByVal fwdReason   As Long, _
                        ByVal lpvReserved As Long) Export As Long
          If fwdReason = %DLL_PROCESS_ATTACH Then hInstDll = hInstance
          LibMain = 1
       End Function
       
       Function DllPath Export As String
          Dim TmpAsciiz As Asciiz * %MAX_PATH
          If GetModuleFileName (hInstDll, TmpAsciiz, SizeOf (TmpAsciiz)) Then Function = TmpAsciiz
       End Function
    [This message has been edited by Semen Matusovski (edited March 12, 2001).]

    Comment


    • #3
      *

      [This message has been edited by Fred Oxenby (edited March 12, 2001).]
      Fred
      mailto:[email protected][email protected]</A>
      http://www.oxenby.se

      Comment


      • #4
        Wayne, a couple of things...

        A. Use "Dim ZString As Asciiz * %MAX_PATH", instead of 255 to be safe.
        B. Calling GetModuleHandle with an empty string will return the current module.

        Regards,


        ------------------
        Kev G Peel
        KGP Software, Bridgwater, UK.
        http://www.kgpsoftware.com
        kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

        Comment


        • #5
          Will this do it?

          Code:
          Function ApplicationPath() As String
          Local zTmp As Asciiz * 256
          Local LenExeName As Long
          LenExeName = GetModuleFileName(ByVal %NULL, zTmp, SizeOf(zTmp))
          If LenExeName Then
             LenExeName = Min&(LenExeName, SizeOf(zTmp))
             zTmp = Left$(zTmp, LenExeName)
          End If
          Function = Left$(zTmp,Instr(-1,zTmp,"\") -1 ) '-1 Removes trailing Backslash
          End Function

          ------------------
          Scott
          Scott Turchin
          MCSE, MCP+I
          http://www.tngbbs.com
          ----------------------
          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

          Comment


          • #6
            This will return the name of the exe calling!

            Btw it's %MAX_PATH + 1 (!!)

            As described above, use the instance handle IN libmain.


            ------------------
            hellobasic

            Comment


            • #7
              Edwin --
              I never saw a code in MSDN, which uses %MAX_PATH + 1 as third parameter.
              Meanwhile the third parameter includes NUL and if to use, for example, 10, OS returns maximum 9 chars (+NUL)

              Simple sample:
              Code:
                 #Compile Exe
                 #Register None
                 #Dim All
                 #Include "WIN32API.INC"
                 
                 Function PbMain
                    Dim TmpAsciiz As Asciiz * %MAX_PATH, ll As Long
                    GetModuleFileName 0, TmpAsciiz, SizeOf (TmpAsciiz)
                    ll = Len(TmpAsciiz): MsgBox TmpAsciiz,, Str$(ll)
                    GetModuleFileName 0, TmpAsciiz, ll
                    ll = Len(TmpAsciiz): MsgBox TmpAsciiz,, Str$(ll)
                 End Function
              [This message has been edited by Semen Matusovski (edited March 12, 2001).]

              Comment


              • #8

                Yeah, Edwin, You're right!

                I forgot that the original question was about getting the path of the DLL.
                You may notice that since the DLL is mapped into the calling EXE code, you will need the handle of the DLL, which is passed in LIBMAIN().

                GetModuleHandle("") will return the EXE module handle!

                Regards,

                ------------------
                Kev G Peel
                KGP Software, Bridgwater, UK.
                http://www.kgpsoftware.com
                kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                Comment


                • #9
                  thanks everyone, Semen your code did the trick perfectly (as usual!) - thanks


                  [This message has been edited by Wayne Diamond (edited March 12, 2001).]
                  -

                  Comment


                  • #10
                    Semen,
                    What do you mean?

                    I assume windows largest pathname can be 260 chars? not 259?
                    ASCIIZ + 1 = 261 wich can contain 260 chars...

                    ??


                    ------------------
                    hellobasic

                    Comment

                    Working...
                    X