Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. [in, out] variables - best not to reuse them! [moved]

[in, out] variables - best not to reuse them! [moved]

Scheduled Pinned Locked Moved Clever Code
12 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Haroon Sarwar
    wrote on last edited by
    #1

    spent a while wondering why szProp2 was returning a truncated string...D'oh! :-O TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize=256; MsiGetProperty(hMsi, "Property1", szProp1, &dwSize); MsiGetProperty(hMsi, "Property2", szProp2, &dwSize); (the last argument of MsiGetProperty takes the size of the buffer szProp1 as input, and returns the number of characters copied into szProp1 as output;P) -- moved at 8:08 Monday 16th April, 2007

    C R J K 4 Replies Last reply
    0
    • H Haroon Sarwar

      spent a while wondering why szProp2 was returning a truncated string...D'oh! :-O TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize=256; MsiGetProperty(hMsi, "Property1", szProp1, &dwSize); MsiGetProperty(hMsi, "Property2", szProp2, &dwSize); (the last argument of MsiGetProperty takes the size of the buffer szProp1 as input, and returns the number of characters copied into szProp1 as output;P) -- moved at 8:08 Monday 16th April, 2007

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      True, even if it is more on the side of code-error than of code-horror one. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      H 1 Reply Last reply
      0
      • C CPallini

        True, even if it is more on the side of code-error than of code-horror one. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        H Offline
        H Offline
        Haroon Sarwar
        wrote on last edited by
        #3

        :laugh:true, but this would fit perfectly in this forum if it was still called 'hall of shame' ;P....

        D 1 Reply Last reply
        0
        • H Haroon Sarwar

          spent a while wondering why szProp2 was returning a truncated string...D'oh! :-O TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize=256; MsiGetProperty(hMsi, "Property1", szProp1, &dwSize); MsiGetProperty(hMsi, "Property2", szProp2, &dwSize); (the last argument of MsiGetProperty takes the size of the buffer szProp1 as input, and returns the number of characters copied into szProp1 as output;P) -- moved at 8:08 Monday 16th April, 2007

          R Offline
          R Offline
          Rage
          wrote on last edited by
          #4

          Occured to me also once when playing with the registry, I think some of the Win32 API reading functions does this as well...

          Company policy : no access to the internet but CP ~RaGE()

          1 Reply Last reply
          0
          • H Haroon Sarwar

            :laugh:true, but this would fit perfectly in this forum if it was still called 'hall of shame' ;P....

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #5

            Well, it would fit in "Subtle bugs" forum nicely. But it is kinda WTF from design on API. It just begs to be (mis)used this way.


            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

            1 Reply Last reply
            0
            • H Haroon Sarwar

              spent a while wondering why szProp2 was returning a truncated string...D'oh! :-O TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize=256; MsiGetProperty(hMsi, "Property1", szProp1, &dwSize); MsiGetProperty(hMsi, "Property2", szProp2, &dwSize); (the last argument of MsiGetProperty takes the size of the buffer szProp1 as input, and returns the number of characters copied into szProp1 as output;P) -- moved at 8:08 Monday 16th April, 2007

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              I can honestly say I have never seen that one before; which is saying a lot. :laugh:

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              1 Reply Last reply
              0
              • H Haroon Sarwar

                spent a while wondering why szProp2 was returning a truncated string...D'oh! :-O TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize=256; MsiGetProperty(hMsi, "Property1", szProp1, &dwSize); MsiGetProperty(hMsi, "Property2", szProp2, &dwSize); (the last argument of MsiGetProperty takes the size of the buffer szProp1 as input, and returns the number of characters copied into szProp1 as output;P) -- moved at 8:08 Monday 16th April, 2007

                K Offline
                K Offline
                Kochise
                wrote on last edited by
                #7

                Use C's "delayed assignement" : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSize=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&(dwSize=256)**); or even : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSizeProp1; DWORD dwSizeProp2; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSizeProp1=dwSizeProp2=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&dwSizeProp2**); It looks weird at first, but it works and avoid several bugs of this kind ! Kochise

                In Code we trust !

                H V P 3 Replies Last reply
                0
                • K Kochise

                  Use C's "delayed assignement" : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSize=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&(dwSize=256)**); or even : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSizeProp1; DWORD dwSizeProp2; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSizeProp1=dwSizeProp2=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&dwSizeProp2**); It looks weird at first, but it works and avoid several bugs of this kind ! Kochise

                  In Code we trust !

                  H Offline
                  H Offline
                  Haroon Sarwar
                  wrote on last edited by
                  #8

                  Kochise wrote:

                  MsiGetProperty(hMsi, "Property1", szProp1, &(dwSize=256)); MsiGetProperty(hMsi, "Property2", szProp2, &(dwSize=256));

                  good idea...code is much nicer to look at :)

                  J 1 Reply Last reply
                  0
                  • H Haroon Sarwar

                    Kochise wrote:

                    MsiGetProperty(hMsi, "Property1", szProp1, &(dwSize=256)); MsiGetProperty(hMsi, "Property2", szProp2, &(dwSize=256));

                    good idea...code is much nicer to look at :)

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    :cool: I have never seen or thought of that before. I do not recommend it though, because the next, less experienced, C programmer that comes along will be saying WTF. :doh: I admit that I like the way it looks and consider it very creative. So I am very glad Kochise posted it. :-D

                    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                    K 1 Reply Last reply
                    0
                    • K Kochise

                      Use C's "delayed assignement" : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSize=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&(dwSize=256)**); or even : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSizeProp1; DWORD dwSizeProp2; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSizeProp1=dwSizeProp2=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&dwSizeProp2**); It looks weird at first, but it works and avoid several bugs of this kind ! Kochise

                      In Code we trust !

                      V Offline
                      V Offline
                      Vivek Rajan
                      wrote on last edited by
                      #10

                      >. MsiGetProperty(hMsi, "Property1", szProp1, &(dwSize=256)); Wow! Never seen that one before. Thanks

                      1 Reply Last reply
                      0
                      • J John R Shaw

                        :cool: I have never seen or thought of that before. I do not recommend it though, because the next, less experienced, C programmer that comes along will be saying WTF. :doh: I admit that I like the way it looks and consider it very creative. So I am very glad Kochise posted it. :-D

                        INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                        K Offline
                        K Offline
                        Kochise
                        wrote on last edited by
                        #11

                        Yeah, since I'm currently writing my own C compiler (not yet C++), I found very interresting coding tricks being just possible/allowed by the standard. It's fun, permisive and very powerful ! Kochise

                        In Code we trust !

                        1 Reply Last reply
                        0
                        • K Kochise

                          Use C's "delayed assignement" : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSize; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSize=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&(dwSize=256)**); or even : TCHAR szProp1[256]; TCHAR szProp2[256]; DWORD dwSizeProp1; DWORD dwSizeProp2; MsiGetProperty(hMsi, "Property1", szProp1, **&(dwSizeProp1=dwSizeProp2=256)**); MsiGetProperty(hMsi, "Property2", szProp2, **&dwSizeProp2**); It looks weird at first, but it works and avoid several bugs of this kind ! Kochise

                          In Code we trust !

                          P Offline
                          P Offline
                          peterchen
                          wrote on last edited by
                          #12

                          The beauty of C++ - there's always a nasty looking trick that one day will save your backus.


                          We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                          My first real C# project | Linkify!|FoldWithUs! | sighist

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups