473,387 Members | 1,721 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,387 software developers and data experts.

new to managed c++, interop questions

Hello,

I'm starting to investigate cs, managed c++ and interoperating with a very
large unmanaged code base. We are going to use Windows Forms (written in
cs) to replace our old fashioned GUI. The guts will remain in unmanaged
c++. So I need to write public managed c++ wrapper classes for the couple
of interfaces the unmanaged stuff needs to expose for the GUI.

Questions:

1) I export a function, say CreateFooInterface, from my unmanaged code
(extern "C"). It my managed wrapper class I have a private pointer object
declared as IFoo* m_pFoo. Call the wrapper class tMgdFoo. In tMgdFoo ctor
I need to create an m_pFoo. If CreateFooInterface returns a pointer to
IFoo, I can successfully write:

m_pFoo = CreateFooInterface();

However, if I declare CreateFooInterface as: long CreateFooInterface( IFoo**
ppFoo ), and attempt to write:

CreateFooInterface( &m_pFoo );

in my tMgdFoo ctor, then I get a compiler error saying it can't convert from
IFoo * __gc * to IFoo**. Why does the former work, but the latter does not?
Is it because I can't take the address of a member variable of a __gc class?
Or is there a mechanism to allow me to do that? I guess I don't understand
the rules about passing around (or back) pointers to memory allocated by the
c run time.

2) In my test app to try all this out I have a cs exe, a dll in which
managed c++ code only resides, and the original unmanaged dll. If I debug
the cs exe and turn on unmanaged code debugging I can place break points in
my unmanaged dll code and I will hit them. The problem is that doing any
debugging there takes a very very long time as the IDE is extremely slow to
respond. Obviously something is going on under the covers when the outer
layer (app) is running in the clr but one is debugging the unmanaged stuff.
So I turned it around and specified that the project I was debugging was the
unmanaged dll and then I specified the cs exe as the app to execute to start
the debugging session. When I did this I was not able to hit breakpoints I
had set in my unmanaged code.

So the question is: How can I reasonably debug unmanaged code when the app
that eventually causes that dll to load is a cs app? It is not really
usable as it is, for instance one line of code of the sort std::cout <<
"line of text" << std::endl; when executed in this manner results in the
characters displaying on the console one by one, extremely slowly. There
must be a better way.

Thanks for the help,

Eric Twietmeyer
Nov 16 '05 #1
1 2089
Hi,
Answer to first part of your question
This is because m_pFoo is a member of a gc class , so it exists on the
GC(Managed) heap. Taking its address gives a managed pointer..so the
compiler is complaining (valid)
Imagine u pass this pointer to a unmanaged function, and in the meantime
Garbage collection happens..this may move the object.. and so the address
is no longer valid..

So u need to pin the object..before u can pass the managed pointer to a
unmanaged func..
look up __pin* in MSDN..
--------------------
From: "Eric Twietmeyer" <no******@noisp.com>
Subject: new to managed c++, interop questions
Date: Fri, 3 Oct 2003 13:30:20 -0700
Lines: 54
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <ep**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vc
NNTP-Posting-Host: s235.207-182-32-net.desert.net 207.182.32.235
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:29008
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hello,

I'm starting to investigate cs, managed c++ and interoperating with a very
large unmanaged code base. We are going to use Windows Forms (written in
cs) to replace our old fashioned GUI. The guts will remain in unmanaged
c++. So I need to write public managed c++ wrapper classes for the couple
of interfaces the unmanaged stuff needs to expose for the GUI.

Questions:

1) I export a function, say CreateFooInterface, from my unmanaged code
(extern "C"). It my managed wrapper class I have a private pointer object
declared as IFoo* m_pFoo. Call the wrapper class tMgdFoo. In tMgdFoo ctor
I need to create an m_pFoo. If CreateFooInterface returns a pointer to
IFoo, I can successfully write:

m_pFoo = CreateFooInterface();

However, if I declare CreateFooInterface as: long CreateFooInterface( IFoo**ppFoo ), and attempt to write:

CreateFooInterface( &m_pFoo );

in my tMgdFoo ctor, then I get a compiler error saying it can't convert fromIFoo * __gc * to IFoo**. Why does the former work, but the latter does not?Is it because I can't take the address of a member variable of a __gc class?Or is there a mechanism to allow me to do that? I guess I don't understand
the rules about passing around (or back) pointers to memory allocated by thec run time.

2) In my test app to try all this out I have a cs exe, a dll in which
managed c++ code only resides, and the original unmanaged dll. If I debug
the cs exe and turn on unmanaged code debugging I can place break points in
my unmanaged dll code and I will hit them. The problem is that doing any
debugging there takes a very very long time as the IDE is extremely slow to
respond. Obviously something is going on under the covers when the outer
layer (app) is running in the clr but one is debugging the unmanaged stuff.
So I turned it around and specified that the project I was debugging was theunmanaged dll and then I specified the cs exe as the app to execute to startthe debugging session. When I did this I was not able to hit breakpoints I
had set in my unmanaged code.

So the question is: How can I reasonably debug unmanaged code when the app
that eventually causes that dll to load is a cs app? It is not really
usable as it is, for instance one line of code of the sort std::cout <<
"line of text" << std::endl; when executed in this manner results in the
characters displaying on the console one by one, extremely slowly. There
must be a better way.

Thanks for the help,

Eric Twietmeyer


Nov 16 '05 #2

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

Similar topics

16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
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...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
0
by: Gerhard Menzl | last post by:
I got no response to this in microsoft.public.dotnet.framework.interop, and since it is a C++-related problem, I am now trying it here: I am using an Interop assembly for communication between my...
3
by: Tommy Svensson \(InfoGrafix\) | last post by:
I've been instructed to work againt a huge unmanaged C++ API from a C# application. Now, the only way, as I've understood it, is to go the Managed Extensions for C++ way. This means I have to...
1
by: Ryan Melville | last post by:
Hi, I need to use WIA (Windows Image Acquisition) from managed C++. Is there a "new and improved" way to access WIA from managed C++ (i.e., through .net)? Or, is it the same COM calls as from...
2
by: Hans Wien | last post by:
I have to write a lot of data to excel for analysts who use it further from there but would like to use managed C++ and not C# since a lot of code is involved which already exists now in managed...
6
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hello gurus, I have the following C# function signature: void TheFunction(Dictionary<string, MyClassObjdicName, out int nOne, out int nTwo, out int nThree, out int nFour); And it works fine....
20
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Sorry to bring up a topic that is just flogging a dead horse.... but... On the topic of memory management.... I am doing some file parcing that has to be done as quick as posible, but what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.