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:unmanage dClass(); //CONSTRUCTOR
public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};
#pragma managed
public __gc class managedClass
{
public:managedC lass(); //CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedCl ass; //PTR TO UNMANAGED
CLASS
public:System:: String* Get_unmanagedLo ng(); //METHOD TO GET
UNMANAGED VARIAHBLE
};
}
//CPP LISTING
//CONSTRUCTORS
MyProgram::unma nagedClass::unm anagedClass(){
unmanagedLong=1 536; //HEX #0600
}
MyProgram::mana gedClass::manag edClass(){
ptrUnmanagedCla ss=new unmanagedClass( );
}
System::String* MyProgram::mana gedClass::Get_u nmanagedLong(){
System::String *result;
//NEXT RETURNS CORRECT VALUE OF #0600
result=System:: String::Concat(
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[0])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[1])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[2])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[3])));
//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
result=System:: String::Format( "{0:x}",__box(p trUnmanagedClas s->myUnion.myData .unmanagedLong) );
return(result);
} 5 2403
Andy wrote:
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:unmanage dClass(); //CONSTRUCTOR
public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};
#pragma managed
public __gc class managedClass
{
public:managedC lass(); //CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedCl ass; //PTR TO UNMANAGED
CLASS
public:System:: String* Get_unmanagedLo ng(); //METHOD TO GET
UNMANAGED VARIAHBLE
};
}
//CPP LISTING
//CONSTRUCTORS
MyProgram::unma nagedClass::unm anagedClass(){
unmanagedLong=1 536; //HEX #0600
}
MyProgram::mana gedClass::manag edClass(){
ptrUnmanagedCla ss=new unmanagedClass( );
}
System::String* MyProgram::mana gedClass::Get_u nmanagedLong(){
System::String *result;
//NEXT RETURNS CORRECT VALUE OF #0600
result=System:: String::Concat(
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[0])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[1])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[2])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[3])));
//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
result=System:: String::Format( "{0:x}",__box(p trUnmanagedClas s->myUnion.myData .unmanagedLong) );
return(result);
}
Did not reproduce your problem in VC8.0
// VC8.0 Common Language Runtime Support, Old Syntax (/clr:oldSyntax)
#include "stdafx.h"
using namespace System;
#pragma unmanaged
__nogc class unmanagedClass
{
public:unmanage dClass()
{
myUnion.myData. unmanagedLong=1 536; //HEX #0600
}
public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};
#pragma managed
public __gc class managedClass
{
public:managedC lass()
{
ptrUnmanagedCla ss=new unmanagedClass( );
}//CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedCl ass; //PTR TO UNMANAGED CLASS
public:System:: String* Get_unmanagedLo ng()
{
System::String *result;
//NEXT RETURNS CORRECT VALUE OF #0600
String* result1=System: :String::Concat (
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[0])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[1])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[2])),
System::String: :Format("{0:x}" ,__box(ptrUnman agedClass->myUnion.b_myDa ta.bytes[3])));
//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
String* result2
=System::String ::Format("{0:x} ",__box(ptrUnma nagedClass->myUnion.myData .unmanagedLong) );
result = new String(",");
result = result->Concat(result1 ,result);
result = result->Concat(result, result2);
return(result);
}
//METHOD TO GET UNMANAGED VARIAHBLE
};
int main()
{
Console::WriteL ine(L"access unmanaged from managed");
managedClass* m = new managedClass;
Console::WriteL ine(m->Get_unmanagedL ong());
return 0;
}
hmmmm.....
Perhaps I should provide the context in which this unmanaged/managed
variable reading is being applied.
I am trying to read an MS Word 2003 document from Visual BASIC.NET 1.1,
and I don't want to use office automation to do this. The process
should work regardless if MS Word is available or not.
MSWord documents use Microsoft's structured storage API for their
read/write operations, which was written before .NET ever existed as
C++ unmanaged code (Structured storage is a file format Microsoft
developed for OLE2 that treats a file as a virtual mini-disk drive).
The Structured Storage API makes heavy use of pre-defined C++
structures, so it cannot be directly used from Visual Basic. And,
because you have to supply variables to hold the results of the calls
to the structured storage API (the MSWord FIB, structured storage
directories, etc), your program holding these variables must also be
written in unmanaged code as well.
A solution to this is to write a callable VC.NET .dll that can "wrap"
the structured storage API and pass/convert the values between the
Visual Basic program and the API itself.
The Visual C++ wrapper needs to have both an unmanaged (to hold
variables used by the sturctured storage API) and managed (for calls to
the structured storage API and conversion of values to standardized
System:: typedefs) sections.
In the MSWord FIB (this is a kind of header record in an MSWord
document that describes the contents of the document; its comparible to
a directory), there is a field called fcMin. This field contains the
offset in bytes of where the ascii text of the document begins (text is
kept separate from its formating inside of MSWord files).
fcMin is defined as a long (4 bytes). When I read the contents of
fcMin in Visual Basic, I get a non-sensical result of an offset
pointing beyond the size of the document file itself. But, if I
inspect the contents of this field byte-by-byte, I find a different
value which I have verified to be the accurate offset to the beginning
of the text in the document.
When I read the field from VC++ managed code, I get the correct value.
But, as soon as I equate it to a System::Int32, I get the wrong value.
I assumed that I get the wrong value in VC++, however, I am actually
inspecting the contents of the field from Visual Basic by calling my
wrapper's get method for this field (I capture the returned value in a
Visual Basic System::Int32 variable). This problem doesn't happen if I
read the field inside of VC++ byte-by-byte, concatenate the byte values
and return a System::String. In this case, the correct value appears
in both the VC wrapper and the calling VB program.
I don't think this problem is caused by the value crossing boundaries
between VC++ to VB because I am using the .NET standardized system
typedefs which are supposed to be treated the same regardless of what
language they are used in. And, the fact that the byte-by-byte value
doesn't get distorted when it is passed supports this.
There must be some problem in setting a System::Int32 value to a C++
long value in Visual C++. System::Int32 (and all other System::
typedefs) are actually wrappers for C++ primitives. I believe the
wrong value that I get (which doesn't change) is actually the value of
a pointer inside of the System::Int32 object, or that the System::Int32
object is not being correctly initialized or set when it is equated to
a C++ unmanaged long.
thanks for looking at my sample code,
Andy
After further testing, and borrowing your technique of combining
strings to simultaneously report on the same variable from different
views, I've determined that my field structure is off.
Reading the value by using byte offsets works because I am accessing
the correct location. But, reading the variable name for the same
memory area shows that the contents actually come from a location 12
bytes further into the structure.
Thanks for your help, Andy
"Andy" <an****@infot ek-consulting.comw rote in message
news:11******** **************@ e63g2000cwd.goo glegroups.com.. .
>
After further testing, and borrowing your technique of combining
strings to simultaneously report on the same variable from different
views, I've determined that my field structure is off.
Reading the value by using byte offsets works because I am accessing
the correct location. But, reading the variable name for the same
memory area shows that the contents actually come from a location 12
bytes further into the structure.
#pragma pack
>
Thanks for your help, Andy
#pragma pack
Thanks for your suggestion. I've added #pragma pack() to my unmanaged
code as you have suggested.
For those reading this post in the future:
pragma pack is a MS VC++ compiler directive on how the compiler should
align structure fields on memory boundaries. Typically, multi-byte
fields such as longs and shorts have to begin on an even memory
address. If the compiler finds a field that doesn't do this, it shifts
it to the next available even address boundary; pack() cancels this
behavior.
The MSWord 97 file structure layout has a long field called lKey (which
contains an encryption key) that begins on an odd address boundary
because of a char defined before it. Without pragma pack, the compiler
will shift this field along with all the others that follow it up
several bytes. This impacts the value you get in fcMin because this
field is defined after lKey.
Other reasons you may not get the correct fcMin value are the two two
bit field structures doctype and savetype are being expanded to more
bytes than the spec perscribes by the compiler. Solution is to union
these structures with a field that is of the perscribed size to ensure
that this expansion doesn't occur.
Andy This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Weston Fryatt |
last post by:
(Sorry for spamming multiple groups, But I need a solution to this problem)
I think this should be a simple question on Memory Allocation in a managed DLL and passing a memory pointer over to an unmanaged DLL.
I have a "Unmanaged" Client DLL that I'm creating a Managed "wrapper" to be used in VB.Net and/or C#..
In the my Client DLL (unmanaged), There are several functions where I need to allocate a "client side" memory buffer to...
|
by: Francis Urquhart |
last post by:
I am trying to find the simplest possible way of writing a
GUI to run on top of a standard C++ library written in
VC++. I would like to avoid MFCs, COM, and if
possible .Net wrappers. The goal is just to be able to
demonstrate the capabilities of the library, rather than
create a finished product.
I have read things here and there about "managed code",
where you can write a GUI in C# to interface with
C++ "unmanaged" code. However,...
|
by: Ben Terry |
last post by:
Hello,
I have a VS 2003.NET solution which consists of four c++ unmanaged legacy
projects. I am adding a new project to the solution which will be in c#.
What do I need to do to my c++ projects in order to call my c# modules from
within c++? From what I understand, I need to convert my c++ code from
unmanaged to managed using the /clr switch (IJW). Is this correct? Are
there any other considerations I need to be aware of?
Ben
|
by: Steve |
last post by:
We are considering the ways to migrate our VC++ 6.0 applications to .NET
platform.
It would be nice to rewrite them completely in C#, but due to the time
constraints
this option is out of question. We started from rewriting some dll in C# in
Visual
Studio .NET. Now we face chalenges of interoperation between old VC++ 6.0
apps and new .NET C# components. To solve this problem we considered the
following options:
|
by: The unProfessional |
last post by:
To the VC .Net'ers out there...
I noticed alot of strange behavior in the way VC .Net apps behave in the
IDE. It's a bit odd, so maybe people have workarounds. I'm worried to
devote my project to the managed/unmanaged heuristic because these early
issues make me feel VC .net just isn't mainstream (or complete) enough yet
to depend on.
Anyway, one of the things is the code formatting. When adding event
handlers and such, the code is...
| |
by: Maansi Sanghi |
last post by:
Hi,
Is there a way to use the System.Xml.Schema classes in VC++ 6.0?
Regards,
Maansi
|
by: vemulakiran |
last post by:
Hi all,
I have doubt regarding .NET.
I have a tool which was developed on VC++ 6.0(Win32 Application ).
The application communicates with library (API) which was developed on
VC++ 6.0 called core client.
I am planning to develop the tool or Win32 application on .NET
platform and I dont' want to migrate the VC++ library (API) to .NET.
The new .NET tool should use the previous VC++ library API ( core
client ).
Is there any possibility to...
|
by: Dave |
last post by:
I've seen at least one article on this: How to access classes, etc.
managed-to-unmanaged. I can't find it. Any tricks or pointers?
I guess the class name won't get munged but I'm not clear about
necessary class declarations.
|
by: Matyi |
last post by:
Hello everybody!
I've this problem:
I'm working on a download meter, based on wpcap. As wpcap code is
unmanaged, I decided to create the needed functions in a unmanaged
project (one for opening an an adapter and capturing the packets (and
thus counting the data downloaded), one for getting the byte-count and
one for stopping the packet capturing), and place them in a dll, which
exports functions
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |