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. General Programming
  3. C / C++ / MFC
  4. how to exchange large string between any prog and dll

how to exchange large string between any prog and dll

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
6 Posts 4 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.
  • T Offline
    T Offline
    tbrake
    wrote on last edited by
    #1

    Hi I need to exchange large Strings between my own dll and my programm. Any Idears ?? THX

    R J 2 Replies Last reply
    0
    • T tbrake

      Hi I need to exchange large Strings between my own dll and my programm. Any Idears ?? THX

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

      extern string mystring; Or what do you mean with "exchange" ? ~RaGE();

      1 Reply Last reply
      0
      • T tbrake

        Hi I need to exchange large Strings between my own dll and my programm. Any Idears ?? THX

        J Offline
        J Offline
        James R Twine
        wrote on last edited by
        #3

        If the DLL is loaded into your processes address space, you can already see each other's memory - no extra steps required.  Just allocate space for the string, and pass it to a method in the DLL.  Or pass the memory to a method in the DLL and have it return the string in it.  Or have the DLL allocate the memory and return it to you.  Etc.    Peace! -=- James


        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        DeleteFXPFiles & CheckFavorites (Please rate this post!)

        B 1 Reply Last reply
        0
        • J James R Twine

          If the DLL is loaded into your processes address space, you can already see each other's memory - no extra steps required.  Just allocate space for the string, and pass it to a method in the DLL.  Or pass the memory to a method in the DLL and have it return the string in it.  Or have the DLL allocate the memory and return it to you.  Etc.    Peace! -=- James


          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          DeleteFXPFiles & CheckFavorites (Please rate this post!)

          B Offline
          B Offline
          BadKarma
          wrote on last edited by
          #4

          James R. Twine wrote:

          Or have the DLL allocate the memory and return it to you.

          Don't forget the let the dll free the memory it has allocated. Every module exe/dll is resposible for its own allocated memory. codito ergo sum

          J 1 Reply Last reply
          0
          • B BadKarma

            James R. Twine wrote:

            Or have the DLL allocate the memory and return it to you.

            Don't forget the let the dll free the memory it has allocated. Every module exe/dll is resposible for its own allocated memory. codito ergo sum

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            Actually, I abhor writing hand-off memory functions, where memory is allocated by the called function and returned from it with the understanding that the caller is now responsible for it.  IME, there seems to be an "out of sight, out of mind" mentality where if the caller did not make an explicit call to a memory allocating function/operator that they are familiar with (new, malloc(...), SysAllocString(...), etc.), they tend not to realize that they are now responsible for deallocating it.    I tend to write functions that need memory such that they take a pointer to a previously-allocated block of memory and the size of that block.  That way, it is clear that the caller manages the memory.    I was just detailing examples for the poster.  Besides, I am fairly certain that a DLL loaded into your address space can allocate memory that you can free (as long as it was allocated while in the context of your process, and not another).    Peace! -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            B 1 Reply Last reply
            0
            • J James R Twine

              Actually, I abhor writing hand-off memory functions, where memory is allocated by the called function and returned from it with the understanding that the caller is now responsible for it.  IME, there seems to be an "out of sight, out of mind" mentality where if the caller did not make an explicit call to a memory allocating function/operator that they are familiar with (new, malloc(...), SysAllocString(...), etc.), they tend not to realize that they are now responsible for deallocating it.    I tend to write functions that need memory such that they take a pointer to a previously-allocated block of memory and the size of that block.  That way, it is clear that the caller manages the memory.    I was just detailing examples for the poster.  Besides, I am fairly certain that a DLL loaded into your address space can allocate memory that you can free (as long as it was allocated while in the context of your process, and not another).    Peace! -=- James


              If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              DeleteFXPFiles & CheckFavorites (Please rate this post!)

              B Offline
              B Offline
              BadKarma
              wrote on last edited by
              #6

              James R. Twine wrote:

              Besides, I am fairly certain that a DLL loaded into your address space can allocate memory that you can free

              It used to be no problem, on OS like Win9x and Win2000. We had writen our own Dll who encrypted some data returning the BYTE* pointer to the encrypted data which was freed after use in our application. Without any problems what so ever. We then moved to WinXP and our application crashed on the the delete[] of the memory returning in a access violation. Found this on the net:

              Potential Problems When Crossing DLL Boundaries

              It is not possible to allocate memory in a DLL (either explicitly with malloc / new or
              implicitly with aforementioned strdup) and pass the pointer across the DLL boundary into
              the process space to free it, when the DLL and the application are using different copies
              of the CRT (C-RunTime) libraries. This will cause a nasty memory access violation or worse:
              corrupt the heap! When you bump into an assert in the CRT lib when it tries to execute free(),
              you will know you have run into this very problem.

              Memory is only valid for the copy of the CRT where they were allocated. This means that when
              you are using two different CRT libraries (LIBCMT.LIB and MSVCRT.LIB for example) together,
              you cannot expect one to correctly free something the other has allocated. Because they most
              probably are using different heap managers, the heap runs the risk of becoming corrupted when
              you run the application in release mode! Pity the programmer who has to track the bug that
              crashes his application this way.

              So its better safe the sorrow. codito ergo sum

              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