473,498 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exporting a __gc class from mC++ to C++

[Crossposted to <m.p.dn.framework.interop> and
<m.p.dn.languages.vc>.]

I created the following DLL in managed C++:

-------- export.cpp -----------
/// cl /clr /LD export.cpp /link /NOENTRY nochkclr.obj

#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

[StructLayout(LayoutKind::Sequential)]
public __gc class T
{
private:
int x;
GCHandle gch;
public:
T(int x)
{
Console::WriteLine("### T.<ctor>({0})",__box(x));
this->x = x;
this->gch = GCHandle::Alloc(this,GCHandleType::Pinned);
}
~T(void)
{
gch.Free();
}
int get_x(void)
{
Console::WriteLine("### T.get_x() >>> {0}",__box(x));
return x;
}
void put_x(int x)
{
Console::WriteLine("### T.put_x({0})",__box(x));
this->x = x;
}
String* ToString(void)
{
return String::Format(
"${0:X8}",
__box(gch.AddrOfPinnedObject().ToInt32()));
}
};

extern "C" __declspec(dllexport) T* newT(int x)
{
T* obj = new T(x);
Console::WriteLine("*** newT({0}) >>> {1}",__box(x),obj);
return obj;
}
extern "C" __declspec(dllexport) void deleteT(T* obj)
{
Console::WriteLine("*** deleteT({0})",obj);
delete obj;
}
extern "C" __declspec(dllexport) int GetX(T* obj)
{
Console::WriteLine("*** GetX({0})",obj);
return obj->get_x();
}
extern "C" __declspec(dllexport) void SetX(T* obj, int x)
{
Console::WriteLine("*** SetX({0},{1})",obj,__box(x));
obj->put_x(x);
}
-------------------------------

And the following test application in unmanaged C++:

--------- test.cpp ------------
/// cl /EHsc test.cpp

#include <iostream>
using namespace std;

#pragma comment(lib,"export.lib")

class T;

extern "C" __declspec(dllimport) T* newT(int x);
extern "C" __declspec(dllimport) int GetX(T* obj);
extern "C" __declspec(dllimport) void SetX(T* obj, int x);

int main(int argc, char* argv[])
{
T* obj = newT(9);
cout << ">>> newT() >>> " << obj << endl;
cout << ">>> GetX() >>> " << GetX(obj) << endl;
SetX(obj,13);
cout << ">>> GetX() >>> " << GetX(obj) << endl;
}
-------------------------------

Both files are compiled as stated in each one's first
comment line (I am using the VC++ 7.1 compiler in VS.NET2003
from the command line).

This is the output:

-------------------------------
### T.<ctor>(9)
*** newT(9) >>> $00AA29D4
newT() >>> 00167CA0 *** GetX($00AA29D4)
### T.get_x() >>> 9 GetX() >>> 9 *** SetX($00AA29D4,13)
### T.put_x(13)
*** GetX($00AA29D4)
### T.get_x() >>> 9 GetX() >>> 9 -------------------------------

(
### lines are printed by the managed class,
*** lines are printed by the managed exports, lines are printed by the unmanaged code,

)

I would expect the value of T::x to be 13 when the second
GetX() is called, istead it still is 9, as if the this->x =
x; assignement in T::put_x() had no effect. I tried with and
without GCHandle and both GCHandleType::Pinned and
GCHandleType::Normal. In the case of GCHandleType::Pinned, I
print the object address and it is always the same, so the
call is being made on the correct object.

However, the assignement is still ineffective, as if the
object were marshalled by value and thus only the copy were
used.

What am I missing here to make it work as I expected?

TIA
--

// Alessandro Angeli
// MVP :: Digital Media
// a dot angeli at psynet dot net

Nov 17 '05 #1
0 1073

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

Similar topics

0
1470
by: Niall | last post by:
I'm attempting to put a C# front end on my unmanaged C++ ray tracer code. It's still in experimental stage, as I have never done anything with exporting types from an unmanaged dll through to a...
3
3369
by: NigelW | last post by:
Clarification needed please. If I compile a C++ program with the /clr option inpsection of the resulting assembly with ILDASM shows MSIL even for methods in classes for which I have not...
6
3298
by: Lupina | last post by:
#pragma once using namespace System; __gc class CWords { public:
1
820
by: EP | last post by:
I have a simple question but I can't find it on any newsgroups. I have a MC++ class that contains a private field of native type private: HTTP_RESPONSE _httpResponse. I need to get it's...
2
2545
by: microsoft | last post by:
Hi All, I am fairly new to Managed C++ extensions. I started trying to implement a interface defined in a C# project, in C++. The problem was with passing value arrays from c# to c++. I found...
5
1089
by: TT (Tom Tempelaere) | last post by:
Hey everyone, If I am writing a .NET library using managed C++, when exactly do I have to decorate a pointer with the __gc decorator? Thanks, -- Tom Tempelaere.
8
1191
by: Peter Oliphant | last post by:
While it does look like 2005 does use a better syntax in general for garbage collection than 2003, here is something I think went the other way. arrays. Do people really think that: ...
0
3545
by: sschus1 | last post by:
I keep getting a "Missing ; from statement" Also how do you reference the linkage id? I'm completely stuck. What i am trying ot do is write a script that selects a movieclip from the library,...
0
1887
by: Saad | last post by:
Hi, I have a MC++dll, whose functions i want to use in unmanaged c++. I read that one of the ways to use the managed functions in unmanaged world is by exposing the managed api as flat api....
0
7125
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
7002
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...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.