Announcement

Collapse
No announcement yet.

Code safety.

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

  • Code safety.

    Hello everyone.
    I am a long time visitor to these forums, first time post.
    Also, long time programmer(Cobol, Business Basic).
    I know this program works, But, Is this code safe? Any memory leaks?
    The DLL part is simulating what a C language program might be doing.

    Code:
     
    #COMPILE DLL "TEST.DLL"
    DECLARE FUNCTION SysAllocStringByteLen LIB "OLEAUT32.DLL" ALIAS "SysAllocStringByteLen" (psz AS ASCIIZ, BYVAL ulen AS DWORD) AS DWORD
    SUB TEST(ix AS LONG) EXPORT
        ix=SysAllocStringByteLen("Testing......", 16)
    END SUB
    Code:
    #COMPILE EXE
    DECLARE SUB TEST LIB "TEST.DLL"(s AS STRING)
    FUNCTION PBMAIN() AS LONG
    LOCAL z AS STRING
        TEST z
        MSGBOX z
    END FUNCTION
    Thanks for your comments.

  • #2
    I don't think so.
    The caller should handle the return value but not a byref value.
    If your caller can handle it, simply use As String for return value.
    This will be a BSTR and destroyed by the caller (should be)
    hellobasic

    Comment


    • #3
      Forget copying the "C" code. Just figure out what the "C" code is doing and do it in BASIC.

      In PB it's easy:
      Code:
      #COMPILE EXE
      ...
       LOCAL Z AS STRING
       CALL   test(Z) 
      ...
      #COMPILE DLL
      SUB TEST (X AS STRING) EXPORT 
        X = LSET$("Testing....", 16 USING $NUL)
      END SUB
      (LSET$ function not in alpha-button page of help file, but it is in the index)

      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Or put the cursor on LSET$ and click the help icon.
        Rod
        In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

        Comment


        • #5
          >Or put the cursor on LSET$ and click the help icon

          Um, that works for the detail ..but it means you aready know LSET$ is the function you want to use.....which I suspect is not the case here.

          Just FWIW, another example here of why I believe verb-for-verb, statement-for-statement, function-for-function ports from one language to another are stone losers.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Um, that works for the detail ..but it means you aready know LSET$ is the function you want to use.....which I suspect is not the case here.
            And here I was thinking that it might have been already in some sample code he had at hand and didn't know the significance thereof and therefore didn't realize how significant it was. I would have thought that he investigated on his own before posting, and passed it by.
            You are not wrong, so I think I'll quit thinking!!
            Rod
            In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

            Comment


            • #7
              That's the problem with everyone's documentation... it's only easy to use if you know what statement/function you want to use... but what you usually want to know is exactly that: which statement or function you want to use!

              About all you can do is think of string (or array, or arithmetic) function in the same 'family' as your current problem, go to the help for that and hope "see also" gives you a steer in the right direction.

              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Eg this case... Best guess from the help file index would be "String Formatting."

                That takes you to the page for USING$.

                Does "See Also" for USING$ suggest RSET$, LSET$ or CSET$? Nope.
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  There is also the 'Command Summary' with the section 'String Operations' that lists all String functions/statements. Command Summary is right on the Home page of the help file, easier to find than in the printed manual, and while it(LSET$/LSET) might be made to show up a few more places, it is there. Here again, you have to know what you're looking for, and be able to recognize it when you see it.
                  And while now a tad off topic, I'll go a little further afield and state that I noticed today that the two items in the "FILE" dropdown menu that were added with PB9, referring to Project files, do not show up in the Help file under the "File Menu", either electronically or printed. With tongue in cheek, I wonder whether I should leave this for someone else to find and report?!
                  Rod
                  In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                  Comment


                  • #10
                    >There is also the 'Command Summary' ...

                    Cool. I had never found that one before. Not too bad, not bad at all.

                    I think I might have found it sooner had the home page had the sub-links right there, eg ....
                    Code:
                    Command Summary 
                     Array Operations
                     COM Commands
                     Communication Control
                     Compiler Operations
                     Debugging and Error Control
                     File Commands
                     Flow Control
                     Graphic Commands
                     Input Commands
                     Memory Management
                     Metastatements
                     Numeric Operations
                     Operating System
                     Printing Commands
                     String Operations
                     Thread Control
                     Time Commands
                      Misc Operations
                    ... but I don't think that would have fit on the page too well.
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      I think I might have found it sooner had the home page had the sub-links right there,
                      One would think it would be a good place for a mouse hover - tool tip type of display of sub-links, if that's possible in the current 'Help' file scheme of things.
                      Didn't look too long, but I never found any tooltips anywhere in the help file.
                      Rod
                      In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                      Comment


                      • #12
                        Originally posted by Rodney Hicks View Post
                        Didn't look too long, but I never found any tooltips anywhere in the help file.
                        That's nothing. I can't even find "Command Summart". Is it in on the web page (somewhere)? Or in the Help file (somewhere else)?
                        It's a pretty day. I hope you enjoy it.

                        Gösta

                        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                        Comment


                        • #13
                          If you aren't sure you're on the Home Page of the Help file, click the Home Icon and there will be the buttons with each starting character of the KEYWORDS and under them the following list:
                          Introducing PowerBASIC For Windows
                          New Statements and Functions
                          Command Summary
                          Data Types
                          Programming Reference
                          Built-in numeric equates
                          Built-in string equates
                          Glossary
                          Register
                          Technical Support
                          Downloads
                          Peer Support Forums
                          Third item.
                          Rod
                          In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                          Comment


                          • #14
                            Originally posted by Rodney Hicks View Post
                            If you aren't sure you're on the Home Page of the Help file, click the Home Icon and there will be the buttons...
                            There are none so blind as those who do not see.

                            Thx Rod.

                            ==========================================
                            "No problem can be solved
                            until it is reduced to some simple form.
                            The changing of a vague difficulty
                            into a specific, concrete form
                            is a very essential element in thinking."
                            J.P. Morgan
                            ==========================================
                            It's a pretty day. I hope you enjoy it.

                            Gösta

                            JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                            LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                            Comment


                            • #15
                              Thank you all for responding.

                              >This will be a BSTR and destroyed by the caller (should be).

                              I think this is the main question. Is the caller De-Allocating the buffer
                              created by SysAllocStringByteLen.

                              >Forget copying the "C" code. Just figure out what the "C" code is doing
                              and do it in BASIC.

                              The caller(EXE) has no control on the language used to write the DLL. It will specify to use a buffer created with SysAllocStringByteLen.

                              Thanks again.

                              Comment

                              Working...
                              X