C ++ Dll在C#中导入多次 [英] C++ Dll Import in C# More Than Once

查看:109
本文介绍了C ++ Dll在C#中导入多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个案例:



i在一个类中使用c ++非托管dll( dll1 )到c#中(<​​b> class1 )在那一页中调用我的c ++ dll的 abc()函数,



现在我使用了相同的c ++ dll( dll1 )进入另一个类( class2 )并调用相同的函数 abc()但这次使用不同的参数值,



我的问题是:

当我将相同的dll( dll1 )导入我的( class2) )并调用函数 abc(),是否会为整个函数 abc()创建不同的内存,或者它将使用已经调用的 abc()来自class1的函数,



我的函数的变量将共享相同的内存,或者它们将在第二个期间再次初始化并具有不同的内存和价值。



i可以通过在c#中的两个不同类上调用相同的函数来检查变量值,但我对此感到困惑如何在这样的水平上处理记忆。

任何建议都表示赞赏。





建议后我更清楚地意识到我的需求:如果我不能在没有内存映射的情况下在两个进程之间共享数据,我可以在两个独立的dll之间共享数据,或者与c / c ++或c#中的内存映射一样复杂。



i没有办法测试它而不做一些复杂的代码但是我想知道哪个更可行在相同dll的两个进程之间共享数据或者之间共享数据不同dll的两个过程。

解决方案

这有点复杂...



整个想法一个DLL就是它独立于你的代码而存在,并且你只需要几个应用程序就可以使用它。



当你在C#中添加一个非托管DLL时它是加载 - 第二次系统意识到它被加载并且不再尝试添加它 - 两个页面将使用相同的代码。

但是......这并不意味着它们将共享或重用相同的内存,因为C#中的内存是基于堆栈或堆的 - 并且堆通常不可用于非托管应用程序。



如果DLL在您调用的方法中使用普通内存,它将使用堆栈中的内存作为调用时在C#代码中执行的线程DLL方法 - 正常情况下,当方法退出并且控制返回到C#代码时,内存将被释放。

如果它尝试使用malloc样式的内存,那么它将从与托管堆不同的堆 - 并且DLL(以及你的代码)将负责释放它 - 垃圾收集器根本不涉及。



那么它会共享还是重用内存?也许。可能不是。没有看代码就很难分辨! :笑:

连连呢?是的,我有一个建议:不要担心;一切都会好的。



粗略地讲,这是交易:流程是隔离的,并在一个单独的地址空间中执行。数据地址总是不同的;您不能在一个过程中更改数据并影响另一个过程。在一个过程中,共享数据存储器。但是,代码的所有物理数据存储器总是被重用,即使在不同的进程之间也是如此。



您认为您的担忧有所不同吗?是的,由于间接,您多次加载相同的DLL。它与C#和托管代码无关(或者与托管代码无关)。但代码存储器总是在系统范围内分配一次。



顺便说一句,这只是所有代码存储器始终是只读的原因之一此外,目前加载执行的所有可执行模块都锁定在磁盘上。



-SA


i have a case where :

i have used a c++ unmanaged dll(dll1) into c# at one class(class1) calling abc() function of my c++ dll in that one page,

now i have used the same c++ dll(dll1) into another class(class2) and calling the same function abc() but with different parameters values this time,

my question is this :
when i import the the same dll(dll1) into my (class2) and call the function abc(), will there be a different memory created for the whole function abc() or will it use the already called abc() function from class1,

will the variables of my function share the same memory or they will be initialized again during second and have different memory and values.

i can check variables value by calling the same function on two different classes in c# but i am little confused about how memory is handled at such a level.
Any suggestions are appreciated.


after suggestions i came to realize my need more clearly : if i cannot share data between two processs without memory mapping, can i share data between two separate dlls, or with that be as complicated as doing memory mapping in either c/c++ or c#.

i have no way to test it out without doing some complex code but i would like to know which is more feasible sharing data between two process of same dll or sharing data between two process of different dll.

解决方案

It's a little complicated...

The whole idea of a DLL is that it exists independently of your code, and that you only need the one for several applications to use.

When you add an unmanaged DLL in C# it is loaded - the second time the system realises it is loaded and does not attempt to add it again - both pages will use the same code.
But...that doesn't mean they will share or reuse the same memory, because the memory in C# is either stack or heap based - and the heap is not normally available to unmanaged applications.

If the DLL uses "normal" memory within the method you call, it will use memory on the stack for the Thread that is executing in your C# code when it calls the DLL method - and as normal that memory will be "deallocated" when the method exits and control returns to your C# code.
If it tries to use malloc-style memory, then it will be allocated from a different "heap" than the managed heap - and the DLL (and thus your code) will be responsible for freeing it - the garbage collector is not involved at all.

So will it share or reuse memory? Maybe. Probably not. Difficult to tell without looking at the code! :laugh:


Suggestions? Yes, I have a suggestion for you: don't worry about it; everything will be all right.

Roughly speaking, here is the deal: processes are isolated and are executed in a separate address space. The data addresses are always distinct; you cannot change data in one process and affect another one. In one process, data memory is shared. However, all physical data memory for the code is always reused, even between different processes.

Do you think your concern is different? Yes, you load the same DLL more than once, due to indirection. And it has nothing to do with C# and managed code (or unmanaged, for this matter). But the code memory is always allocated once, system-wide.

By the way, this is just one of the reasons why all code memory is always read-only and, moreover, all executable modules presently loaded for execution are locked on the disk.

—SA


这篇关于C ++ Dll在C#中导入多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆