Does P/Invoke execute the DLL and then shut it down?












7















If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory? Or will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?



When I need a certain C++ project to have its memory persistant, should I be creating an ActiveX/COM Server to have its memory persist, and yet be able to call it from C#?










share|improve this question



























    7















    If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory? Or will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?



    When I need a certain C++ project to have its memory persistant, should I be creating an ActiveX/COM Server to have its memory persist, and yet be able to call it from C#?










    share|improve this question

























      7












      7








      7








      If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory? Or will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?



      When I need a certain C++ project to have its memory persistant, should I be creating an ActiveX/COM Server to have its memory persist, and yet be able to call it from C#?










      share|improve this question














      If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory? Or will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?



      When I need a certain C++ project to have its memory persistant, should I be creating an ActiveX/COM Server to have its memory persist, and yet be able to call it from C#?







      c# c++ .net dll pinvoke






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 '13 at 12:47









      RobinicksRobinicks

      43.2k115339541




      43.2k115339541
























          2 Answers
          2






          active

          oldest

          votes


















          5















          If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory?




          No. Once the DLL is loaded it will stay loaded. The DLL's lifetime is not tied to a function call. This means that variables in the DLL that have static storage persist beyond the initial p/invoke call.






          share|improve this answer


























          • Where is the memory stored? Can you elaborate on that?

            – Robinicks
            Mar 24 '13 at 13:24











          • Unmanaged DLLs are loaded into the address space of the managed program's Application Domain. Note that you can dyamically load and unload Application Domains, which removes the unmanaged DLLs from memory when the app domain is unloaded.

            – Matthew Watson
            Mar 24 '13 at 13:41











          • Where is what memory stored? Be precise about the question.

            – David Heffernan
            Mar 24 '13 at 13:41











          • I meant where is the memory of the C++ DLL stored... when run by a .NET app? ... "will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?"

            – Robinicks
            Mar 24 '13 at 16:12













          • It's handled by the system. What you have is a native, unmanaged DLL. It's loaded with a call to LoadLibrary. Just like the .net runtime itself. .net doesn't "take charge" of anything. It just uses the standard platform mechanisms to load DLLs.

            – David Heffernan
            Mar 24 '13 at 16:13



















          2














          If you are creating an object from a C++-DLL, it will actually persist until you delete it or rather dispose it. As you need to manually remove unmanaged objects, it will stay.






          share|improve this answer
























          • So is it therefore possible to have a fully functional C++ project running as a DLL, and have simple static function P/Invoke'd from .NET with nothing breaking on the C++ side? Even if the C++ project uses objects and DirectX and complex things?

            – Robinicks
            Mar 24 '13 at 12:53











          • It depends on what you understand by fully functional? Consider for example COM, where you are creating unmanaged components which you can then use in a managed environment. SO it should be possible.

            – bash.d
            Mar 24 '13 at 12:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15598636%2fdoes-p-invoke-execute-the-dll-and-then-shut-it-down%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5















          If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory?




          No. Once the DLL is loaded it will stay loaded. The DLL's lifetime is not tied to a function call. This means that variables in the DLL that have static storage persist beyond the initial p/invoke call.






          share|improve this answer


























          • Where is the memory stored? Can you elaborate on that?

            – Robinicks
            Mar 24 '13 at 13:24











          • Unmanaged DLLs are loaded into the address space of the managed program's Application Domain. Note that you can dyamically load and unload Application Domains, which removes the unmanaged DLLs from memory when the app domain is unloaded.

            – Matthew Watson
            Mar 24 '13 at 13:41











          • Where is what memory stored? Be precise about the question.

            – David Heffernan
            Mar 24 '13 at 13:41











          • I meant where is the memory of the C++ DLL stored... when run by a .NET app? ... "will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?"

            – Robinicks
            Mar 24 '13 at 16:12













          • It's handled by the system. What you have is a native, unmanaged DLL. It's loaded with a call to LoadLibrary. Just like the .net runtime itself. .net doesn't "take charge" of anything. It just uses the standard platform mechanisms to load DLLs.

            – David Heffernan
            Mar 24 '13 at 16:13
















          5















          If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory?




          No. Once the DLL is loaded it will stay loaded. The DLL's lifetime is not tied to a function call. This means that variables in the DLL that have static storage persist beyond the initial p/invoke call.






          share|improve this answer


























          • Where is the memory stored? Can you elaborate on that?

            – Robinicks
            Mar 24 '13 at 13:24











          • Unmanaged DLLs are loaded into the address space of the managed program's Application Domain. Note that you can dyamically load and unload Application Domains, which removes the unmanaged DLLs from memory when the app domain is unloaded.

            – Matthew Watson
            Mar 24 '13 at 13:41











          • Where is what memory stored? Be precise about the question.

            – David Heffernan
            Mar 24 '13 at 13:41











          • I meant where is the memory of the C++ DLL stored... when run by a .NET app? ... "will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?"

            – Robinicks
            Mar 24 '13 at 16:12













          • It's handled by the system. What you have is a native, unmanaged DLL. It's loaded with a call to LoadLibrary. Just like the .net runtime itself. .net doesn't "take charge" of anything. It just uses the standard platform mechanisms to load DLLs.

            – David Heffernan
            Mar 24 '13 at 16:13














          5












          5








          5








          If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory?




          No. Once the DLL is loaded it will stay loaded. The DLL's lifetime is not tied to a function call. This means that variables in the DLL that have static storage persist beyond the initial p/invoke call.






          share|improve this answer
















          If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory?




          No. Once the DLL is loaded it will stay loaded. The DLL's lifetime is not tied to a function call. This means that variables in the DLL that have static storage persist beyond the initial p/invoke call.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 '13 at 13:50

























          answered Mar 24 '13 at 12:50









          David HeffernanDavid Heffernan

          520k348221214




          520k348221214













          • Where is the memory stored? Can you elaborate on that?

            – Robinicks
            Mar 24 '13 at 13:24











          • Unmanaged DLLs are loaded into the address space of the managed program's Application Domain. Note that you can dyamically load and unload Application Domains, which removes the unmanaged DLLs from memory when the app domain is unloaded.

            – Matthew Watson
            Mar 24 '13 at 13:41











          • Where is what memory stored? Be precise about the question.

            – David Heffernan
            Mar 24 '13 at 13:41











          • I meant where is the memory of the C++ DLL stored... when run by a .NET app? ... "will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?"

            – Robinicks
            Mar 24 '13 at 16:12













          • It's handled by the system. What you have is a native, unmanaged DLL. It's loaded with a call to LoadLibrary. Just like the .net runtime itself. .net doesn't "take charge" of anything. It just uses the standard platform mechanisms to load DLLs.

            – David Heffernan
            Mar 24 '13 at 16:13



















          • Where is the memory stored? Can you elaborate on that?

            – Robinicks
            Mar 24 '13 at 13:24











          • Unmanaged DLLs are loaded into the address space of the managed program's Application Domain. Note that you can dyamically load and unload Application Domains, which removes the unmanaged DLLs from memory when the app domain is unloaded.

            – Matthew Watson
            Mar 24 '13 at 13:41











          • Where is what memory stored? Be precise about the question.

            – David Heffernan
            Mar 24 '13 at 13:41











          • I meant where is the memory of the C++ DLL stored... when run by a .NET app? ... "will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?"

            – Robinicks
            Mar 24 '13 at 16:12













          • It's handled by the system. What you have is a native, unmanaged DLL. It's loaded with a call to LoadLibrary. Just like the .net runtime itself. .net doesn't "take charge" of anything. It just uses the standard platform mechanisms to load DLLs.

            – David Heffernan
            Mar 24 '13 at 16:13

















          Where is the memory stored? Can you elaborate on that?

          – Robinicks
          Mar 24 '13 at 13:24





          Where is the memory stored? Can you elaborate on that?

          – Robinicks
          Mar 24 '13 at 13:24













          Unmanaged DLLs are loaded into the address space of the managed program's Application Domain. Note that you can dyamically load and unload Application Domains, which removes the unmanaged DLLs from memory when the app domain is unloaded.

          – Matthew Watson
          Mar 24 '13 at 13:41





          Unmanaged DLLs are loaded into the address space of the managed program's Application Domain. Note that you can dyamically load and unload Application Domains, which removes the unmanaged DLLs from memory when the app domain is unloaded.

          – Matthew Watson
          Mar 24 '13 at 13:41













          Where is what memory stored? Be precise about the question.

          – David Heffernan
          Mar 24 '13 at 13:41





          Where is what memory stored? Be precise about the question.

          – David Heffernan
          Mar 24 '13 at 13:41













          I meant where is the memory of the C++ DLL stored... when run by a .NET app? ... "will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?"

          – Robinicks
          Mar 24 '13 at 16:12







          I meant where is the memory of the C++ DLL stored... when run by a .NET app? ... "will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?"

          – Robinicks
          Mar 24 '13 at 16:12















          It's handled by the system. What you have is a native, unmanaged DLL. It's loaded with a call to LoadLibrary. Just like the .net runtime itself. .net doesn't "take charge" of anything. It just uses the standard platform mechanisms to load DLLs.

          – David Heffernan
          Mar 24 '13 at 16:13





          It's handled by the system. What you have is a native, unmanaged DLL. It's loaded with a call to LoadLibrary. Just like the .net runtime itself. .net doesn't "take charge" of anything. It just uses the standard platform mechanisms to load DLLs.

          – David Heffernan
          Mar 24 '13 at 16:13













          2














          If you are creating an object from a C++-DLL, it will actually persist until you delete it or rather dispose it. As you need to manually remove unmanaged objects, it will stay.






          share|improve this answer
























          • So is it therefore possible to have a fully functional C++ project running as a DLL, and have simple static function P/Invoke'd from .NET with nothing breaking on the C++ side? Even if the C++ project uses objects and DirectX and complex things?

            – Robinicks
            Mar 24 '13 at 12:53











          • It depends on what you understand by fully functional? Consider for example COM, where you are creating unmanaged components which you can then use in a managed environment. SO it should be possible.

            – bash.d
            Mar 24 '13 at 12:54
















          2














          If you are creating an object from a C++-DLL, it will actually persist until you delete it or rather dispose it. As you need to manually remove unmanaged objects, it will stay.






          share|improve this answer
























          • So is it therefore possible to have a fully functional C++ project running as a DLL, and have simple static function P/Invoke'd from .NET with nothing breaking on the C++ side? Even if the C++ project uses objects and DirectX and complex things?

            – Robinicks
            Mar 24 '13 at 12:53











          • It depends on what you understand by fully functional? Consider for example COM, where you are creating unmanaged components which you can then use in a managed environment. SO it should be possible.

            – bash.d
            Mar 24 '13 at 12:54














          2












          2








          2







          If you are creating an object from a C++-DLL, it will actually persist until you delete it or rather dispose it. As you need to manually remove unmanaged objects, it will stay.






          share|improve this answer













          If you are creating an object from a C++-DLL, it will actually persist until you delete it or rather dispose it. As you need to manually remove unmanaged objects, it will stay.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 '13 at 12:51









          bash.dbash.d

          11.2k22036




          11.2k22036













          • So is it therefore possible to have a fully functional C++ project running as a DLL, and have simple static function P/Invoke'd from .NET with nothing breaking on the C++ side? Even if the C++ project uses objects and DirectX and complex things?

            – Robinicks
            Mar 24 '13 at 12:53











          • It depends on what you understand by fully functional? Consider for example COM, where you are creating unmanaged components which you can then use in a managed environment. SO it should be possible.

            – bash.d
            Mar 24 '13 at 12:54



















          • So is it therefore possible to have a fully functional C++ project running as a DLL, and have simple static function P/Invoke'd from .NET with nothing breaking on the C++ side? Even if the C++ project uses objects and DirectX and complex things?

            – Robinicks
            Mar 24 '13 at 12:53











          • It depends on what you understand by fully functional? Consider for example COM, where you are creating unmanaged components which you can then use in a managed environment. SO it should be possible.

            – bash.d
            Mar 24 '13 at 12:54

















          So is it therefore possible to have a fully functional C++ project running as a DLL, and have simple static function P/Invoke'd from .NET with nothing breaking on the C++ side? Even if the C++ project uses objects and DirectX and complex things?

          – Robinicks
          Mar 24 '13 at 12:53





          So is it therefore possible to have a fully functional C++ project running as a DLL, and have simple static function P/Invoke'd from .NET with nothing breaking on the C++ side? Even if the C++ project uses objects and DirectX and complex things?

          – Robinicks
          Mar 24 '13 at 12:53













          It depends on what you understand by fully functional? Consider for example COM, where you are creating unmanaged components which you can then use in a managed environment. SO it should be possible.

          – bash.d
          Mar 24 '13 at 12:54





          It depends on what you understand by fully functional? Consider for example COM, where you are creating unmanaged components which you can then use in a managed environment. SO it should be possible.

          – bash.d
          Mar 24 '13 at 12:54


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15598636%2fdoes-p-invoke-execute-the-dll-and-then-shut-it-down%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$