A
Andy
I'm having trouble accessing an unmanaged long from a managed class in
VC++.NET
When I do, the contents of the variable seem to be mangled. If I
access the same variable byte-by-byte, I get the correct value.
Regardless what I set the variable to, the value that is returned for a
long is always the same value. What's going on...can anyone help me?
A short version of the code follows:
//HEADER
namespace MyProgram
{
#pragma unmanaged
__nogc class unmanagedClass
{
public:unmanagedClass(); //CONSTRUCTOR
public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};
#pragma managed
public __gc class managedClass
{
public:managedClass(); //CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedClass; //PTR TO UNMANAGED
CLASS
public:System::String* Get_unmanagedLong(); //METHOD TO GET
UNMANAGED VARIAHBLE
};
}
//CPP LISTING
//CONSTRUCTORS
MyProgram::unmanagedClass::unmanagedClass(){
unmanagedLong=1536; //HEX #0600
}
MyProgram::managedClass::managedClass(){
ptrUnmanagedClass=new unmanagedClass();
}
System::String* MyProgram::managedClass::Get_unmanagedLong(){
System::String *result;
//NEXT RETURNS CORRECT VALUE OF #0600
result=System::String::Concat(
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[0])),
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[1])),
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[2])),
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[3])));
//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
result=System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.myData.unmanagedLong));
return(result);
}
VC++.NET
When I do, the contents of the variable seem to be mangled. If I
access the same variable byte-by-byte, I get the correct value.
Regardless what I set the variable to, the value that is returned for a
long is always the same value. What's going on...can anyone help me?
A short version of the code follows:
//HEADER
namespace MyProgram
{
#pragma unmanaged
__nogc class unmanagedClass
{
public:unmanagedClass(); //CONSTRUCTOR
public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};
#pragma managed
public __gc class managedClass
{
public:managedClass(); //CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedClass; //PTR TO UNMANAGED
CLASS
public:System::String* Get_unmanagedLong(); //METHOD TO GET
UNMANAGED VARIAHBLE
};
}
//CPP LISTING
//CONSTRUCTORS
MyProgram::unmanagedClass::unmanagedClass(){
unmanagedLong=1536; //HEX #0600
}
MyProgram::managedClass::managedClass(){
ptrUnmanagedClass=new unmanagedClass();
}
System::String* MyProgram::managedClass::Get_unmanagedLong(){
System::String *result;
//NEXT RETURNS CORRECT VALUE OF #0600
result=System::String::Concat(
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[0])),
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[1])),
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[2])),
System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.b_myData.bytes[3])));
//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
result=System::String::Format("{0:x}",__box(ptrUnmanagedClass->myUnion.myData.unmanagedLong));
return(result);
}