Announcement

Collapse
No announcement yet.

Question on Global variables in a DLL

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

    Question on Global variables in a DLL

    In a DLL, is it true that its global variables are only valid while inside that DLL and not shareable to the Main prog which calls that DLL ?

    I would like to ensure that all the Global variables in a DLL can only work inside that DLL, and not seen by the Main calling prog.

    #2
    That is correct.
    "Not my circus, not my monkeys."

    Comment


      #3
      Thanks Eric

      Comment


        #4
        Via direct source code invocation NO (i.e., without use of pointers or memory peeking). But most definitely YES if you pass pointers.

        For example, given...

        DLL
        Code:
        GLOBAL gstrVariable AS STRING
        
        SUB SetGlobal()
           gstrVariable = "Hi I am data"
        END SUB
        
        SUB GetPointer(BYREF dwdPassed AS DWORD)
           SetGlobal
           dwdPassed = VARPTR(gstrVariable)
        END SUB
        MainProgram
        Code:
        SUB PBMAIN()
           LOCAL pstrVariable AS STRING PTR
           GetPointer(pstrVariable)
           ? @pstrVariable
        END SUB
        NOTE: Yes I know horrible example

        If you pass the pointer value outside the DLL to the calling EXE it can most definitely access the value of your global.
        <b>George W. Bleck</b>
        <img src='http://www.blecktech.com/myemail.gif'>

        Comment


          #5
          I think you need an EXPORT in there somewhere. But in this case, the OP is trying to avoid sharing the values between modules. Yes there are workarounds if you want to purposely bypass the default behavior.
          "Not my circus, not my monkeys."

          Comment


            #6
            Yeah yeah the export as I said bad example. It was more to set the tone it CAN be done. Can also peek account the DLL memory space as it is withing your own. Depends on how bad that global needs to be "protected".
            <b>George W. Bleck</b>
            <img src='http://www.blecktech.com/myemail.gif'>

            Comment


              #7
              Originally posted by George Bleck View Post
              It was more to set the tone it CAN be done. Can also peek account the DLL memory space as it is withing your own. Depends on how bad that global needs to be "protected".
              Given the OP's
              "would like to ensure that all the Global variables in a DLL can only work inside that DLL, and not seen by the Main calling prog."
              your posts are a bit like:
              "Can I see a black cat in a dark cellar at night? Yes. If it shiines its flashlight on itself"

              (Note the word "flashlight" used to avoid disturbing people in the US. I would normally have said torch" )
              =========================
              https://camcopng.com
              =========================

              Comment


                #8
                What is OP?

                Comment


                  #9
                  Originally posted by Mannish Bhandari View Post
                  What is OP?
                  Depending on context: "Original Post" or "Original Poster" i.e. normally post #1 in the thread but also maybe something subsequently posted by the original poster.
                  =========================
                  https://camcopng.com
                  =========================

                  Comment


                    #10
                    (Note the word "flashlight" used to avoid disturbing people in the US. I would normally have said torch" )​
                    Can I make that:
                    "(Note the word "flashlight" used to avoid disturbing certain people in the US. I would normally have said torch" ) ?

                    ((and some how I suspect that won't happen again for a while [end ] ))​
                    Dale

                    Comment


                      #11
                      Eric, I'm not OP, but it's an interesting question and I hadn't ever considered the answer. Thanks for answering. Follow-on question, what about pointers? Could a pointer inside the MAIN be passed to a DLL and the reference remain faithful?

                      I guess I could code it myself to test. But I'm lazy at the moment. But if it works, and you needed global variables in a DLL, I suppose a work-around would be to make them available via pointers. I still wish there was a way to refer to an array with a pointer in PB.
                      Christopher P. Becker
                      signal engineer in the defense industry
                      Abu Dhabi, United Arab Emirates

                      Comment


                        #12
                        See post 4.
                        Will also work with GLOBAL in dot exe and pointer in DLL.
                        Use a "helper" function in DLL to exchange pointer. (as in post 4)
                        Will remain "faithful" (valid) unless DLL is unloaded. (re-exchange pointer if DLL reloaded)
                        I still wish there was a way to refer to an array with a pointer in PB.
                        How about pointer to first array element, pass pointer, then DIM AT in other module? ("Helper" function would also pass/receive LBOUND and UBOUND.)

                        Cheers,
                        Dale

                        Comment


                          #13
                          Originally posted by Christopher Becker View Post
                          I still wish there was a way to refer to an array with a pointer in PB.
                          WHat do you mean by "refer to an array" ?

                          Have you read Help on "Pointers To Arrays"?
                          Also Help on "Varptr" and "Passing Arrays"
                          =========================
                          https://camcopng.com
                          =========================

                          Comment


                            #14
                            Originally posted by Christopher Becker View Post
                            E But if it works, and you needed global variables in a DLL, I suppose a work-around would be to make them available via pointers.
                            or, as Bob wrote in Help:

                            GLOBAL variables are not shared between programs and DLLs or between multiple instances of the same DLL. That is, a GLOBAL variable is only global within its own module. The simplest way to expose a variable to a DLL is to pass the variable (by reference) to the target DLL
                            =========================
                            https://camcopng.com
                            =========================

                            Comment


                              #15
                              A passed array can be copied into a global array and the original array can be shared with with PbMain.
                              Next post passes an array from PbMain using join binary into a global array in a DLL function.

                              Comment


                                #16
                                PassArray JOIN$(sArray(), BINARY), LBOUND(sArray), UBOUND(sArray)

                                Code:
                                #COMPILE EXE
                                DECLARE SUB PassArray LIB "MyDll.Dll"(s AS STRING,low AS LONG,high AS LONG)
                                GLOBAL gs() AS STRING   'does not have to be global
                                
                                FUNCTION PBMAIN
                                 REDIM gs(1)
                                 ARRAY ASSIGN gs()="Cool","beans"
                                 PassArray JOIN$(gs(),BINARY),LBOUND(gs),UBOUND(gs)
                                END FUNCTION
                                Code:
                                #COMPILE DLL
                                GLOBAL gs() AS STRING
                                DECLARE SUB PassArray LIB "MyDll.Dll"(s AS STRING,low AS LONG, high AS LONG)
                                
                                SUB PassArray (s AS STRING,low AS LONG,high AS LONG) EXPORT
                                 REDIM gs(low,high)  'redim to correct number of elements
                                 PARSE s,gs(),BINARY 'parse into global array
                                END SUB

                                Comment


                                  #17
                                  isn't that passing the contents of an array into an identical but totally separate array? Any changes in one will not "appear" in the other.
                                  Dale

                                  Comment


                                    #18
                                    Originally posted by Mike Doty View Post
                                    Pass array to a DLL function that copies array to a global array[code]
                                    Alternatively, see Variant Data Types


                                    =========================
                                    https://camcopng.com
                                    =========================

                                    Comment


                                      #19
                                      > isn't that passing the contents of an array into an identical but totally separate array? Any changes in one will not "appear" in the other.
                                      Dale,
                                      True

                                      Post number 1
                                      In a DLL, is it true that its global variables are only valid while inside that DLL and not shareable to the Main prog which calls that DLL ?

                                      I would like to ensure that all the Global variables in a DLL can only work inside that DLL, and not seen by the Main calling prog.​

                                      Comment


                                        #20
                                        Mannish,

                                        Thank you. You may have gotten your answer early, but it still sparked a good discussion.
                                        Dale

                                        Comment

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