Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 25-Mar-15 19:36pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Mar-15 10:50am    
The whole idea is unclear. If dll1 is unmanaged, what do you mean by "importing"? The multiple use of DLLs in the same process is the common thing. How is that related to C#?
—SA
barneyman 26-Mar-15 1:46am    
with reference to your comment about mem-mapping ...

DLLs share the process memory *of the process they're loaded into* ... they do not have their own - so you can't share memory between processes and (similarly) you can't share memory between DLL's loaded in the different processes

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:
 
Share this answer
 
Comments
RajneeshSaysHello 26-Mar-15 1:10am    
in c++ _declspec(dllexport) char* abc(){ ... char* virusHexScanStatus(variable to return)}

inc# [DllImport("project3.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr abc();

IntPtr ptr1 = abc();
string result= Marshal.PtrToStringAnsi(ptr1);

nothing big, but i would like to look for a way to affect varible of my function in one class call to dll , and use the affected value of the vairbale in another class,,, thanks for ans
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
 
Share this answer
 
v2
Comments
RajneeshSaysHello 26-Mar-15 1:10am    
"you cannot change data in one process and affect another one. In one process, data memory is shared"
"all physical data memory for the code is always reused, even between different processes"

yes i verified your statement, i was not able to use data of one process into other process
Sergey Alexandrovich Kryukov 26-Mar-15 1:23am    
Very good. Any problems with that? Will you accept my answer formally?
—SA
RajneeshSaysHello 26-Mar-15 1:30am    
what about memory mapping techniques, it is possible with them, but not directly as i initially asked in the question, i am updating my question please see it.
Sergey Alexandrovich Kryukov 26-Mar-15 1:34am    
Which techniques? You can get memory-mapped file, you can have shared memory object, you can inject code in a process...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900