473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

VC2005 C++/CLI VB.NET byref unmanaged interop

Hi. Just joined this group and I don't think this had been addressed
before..

I have a VB.NET class library assembly "xyz.dll" that I want to access
from some unmanaged code in an MFC DLL (contained in a cpp file
compiled with /clr) via:

#include <vcclr.h>
#using "xyz.dll"
using namespace System;
using namespace xyz;
...
gcroot<Moo^boo = gcnew Moo;

My VB.NET assembly contains:

Public Class Moo
Public Sub Foo(ByRef s as String)
End Class

If I try to get to it using...

CString& mfcstr;
gcroot<String^str = gcnew String(mfcstr);
boo->Foo(str);
mfcstr = str;

... the compiler complains that gcroot can't supply the reference
parameter (%).

However, if I just use:

CString& mfcstr;
String^ str = gcnew String(mfcstr);
boo->Foo(str);
mfcstr = str;

... then it works fine, but I'm concerned that by not using gcroot to
wrap the String^, that the String^ will not be garbage collected. The
working code does not result in a memory leak, but I'm not quite
satisfied with my understanding. Can anyone enlighten me as to what is
the correct way to handle my reference parameter.

Thanks,
M

Feb 21 '07 #1
3 2335
CString& mfcstr;
String^ str = gcnew String(mfcstr);
boo->Foo(str);
mfcstr = str;

.. then it works fine, but I'm concerned that by not using gcroot to
wrap the String^, that the String^ will not be garbage collected. The
working code does not result in a memory leak, but I'm not quite
satisfied with my understanding. Can anyone enlighten me as to what is
the correct way to handle my reference parameter.
The latter way is correct.

gcroot has nothing to do with causing a ref class to be garbage collected,
rather quite the opposite, it prevents the object from being collected too
soon (and also updates whenever the object moves). Normally the garbage
collector searches for all references to an object before eliminating it,
including the managed heap and the stack (hence the latter way is correct,
with the reference on the stack the collector sees it). For a purely
managed program, that's enough. But using OS memory allocation primitives
(C malloc/free, C++ new/delete, Win32 HeapAlloc, VirtualAlloc, etc) you can
have memory that the collector isn't aware of. Then you need GCHandle,
which gcroot wraps conveniently for C++ programmers.
>
Thanks,
M

Feb 21 '07 #2
Ben, many thanks for your input.

Strange... I sent a much longer reply yesterday but it never appeared.

Feb 22 '07 #3

"mclp" <my****@hungleng.co.ukwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
Ben, many thanks for your input.

Strange... I sent a much longer reply yesterday but it never appeared.
No worries. Hope that helped you.

The important thing with garbage collection is that you don't need to ensure
that stuff is freed exactly when the last user stops referencing it, you
just need to make sure the garbage collector knows what you're using,
because it already knows what other modules are using, and works on the
basis of reachability (so that cycles aren't such a problem as with
refcounts).
Feb 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
5
by: GeRmIc | last post by:
Hi, I am doing an interop from unmanaged code to C#. How do i pass an ArrayList pointer from an unmanaged code, (structres are easily passed by between C# and C). //This is the C code ...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
1
by: Zapbbx | last post by:
I have a 3rd party application that can reference external dll's. The dll's have to be written in unmanaged code with an exported function I can reference and call. I would like it to call a C# dll...
15
by: Chetan Raj | last post by:
Hi All, We have a web-application in asp.net that interacts with legacy code written in COM. The memory usage in aspnet_wp.exe increases every sec and never reduces. Using the .NET performance...
6
by: VJ | last post by:
Does ByRef work when calling a Method in DLL from a Executable? VJ
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
1
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
1
by: Art | last post by:
I have some Static libs which were intially implemented in VC6 and then converted to VC2005. When I try to use these in my managed C++ application, I am getting the following exception: An...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.