BLiu

Hi,

I am developing a middle bridge piece in managed c++, so c# code can

reuse many legacy unmanaged c++ functions.

all those unmanaged c++ function returns are structures, hundreds of

them.

my goal is a generic function, do memory copy of a void pointer

from unmanaged C++ to a void pointer in managed c++, then in c++ casts

the pointer back to structure so I can access each element. (hope that

sounds doable).

the Problem:
I do not know how to do it when the structure has array in it.

here is my working code for structure without array, I was able to see

the change in myS on the managed side.

I tried to do [FieldOffset(9)]int ints[3]; or
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 3)]array<int>^ ints;
it did not work.

please help. basically I need a continuous memory block of same order,

same size and pass its pointer to unmanaged code.

thanks

Belinda


================================
in managed c++:

[StructLayout(LayoutKind::Explicit, CharSet=CharSet::Ansi, Pack=1)]
value struct myStruct
{
public:
[FieldOffset(0)]double dbl;
[FieldOffset(8)]char ch;
};

ref class LibWrapper
{
public:
[DllImport("cmddll.dll", CallingConvention=CallingConvention:Tongue TiedtdCall)]
static bool test(void *r);
};

...

myStruct myS = myStruct( );
myS.dbl = 99.9;
myS.ch = 'L';
LibWrapper::test(&myS);

==================================
in unmanaged c++

typedef struct
{
double dbl;
char ch;

}myStruct;

...


bool test(void *r)
{
myStruct new_rec;

new_rec.ch = 'U';
new_rec.dbl= 33.3;

memcpy(r, &new_rec, sizeof(myStruct));

return true;
}




Re: Visual C++ General copy unmanaged memory block to managed memory pointer

Simple Samples

What you are doing is similar to what I am doing. I have a hundred C++ structures derived from one base structure that I write to and read from disk directly from memory. When I asked for assistance everyone said it is impossible or nearly impossible to do that. I am doing it but it is definitely not simple unless the structure or class is simple enough. If there are any virtual functions then that requires extra work. If a structure uses a class or structure for a member's type then as far as I know it is not possible to do a binary copy as you want to do. A binary copy is not possible if any member is not a basic type such as int or char. A binary copy is also not likely to be functional if there are any pointers, since usually when we copy a structure or class with a pointer, we want a new allocation of the data that is pointed to.

I don't have experience with managed code; my guess is that it is less likely you can do a binary copy to or from a class in managed code, especially since managed code is by definition independent of processor technology and there is no guarantee that a binary copy can work for all processors, even when the copy is being done using the same processor.

I am sorry, but you are likely to not get much help. Most other C++ experts seem to think that it is not possible to do binary copies, even within C++. Probably what you need to do is to create a COM object wrapper for the C++ code that will be easy to use from managed code. As I say, though, I am inexperienced with managed code so there might be an easier solution.






Re: Visual C++ General copy unmanaged memory block to managed memory pointer

Simple Samples

Is there a reason why this was asked in the VC general forum instead of the VC language forum






Re: Visual C++ General copy unmanaged memory block to managed memory pointer

BLiu

I posted my question in C# forum, hope some experts there can give me some help, if there is a solution in C#, I do not have to do it in the middle bridge piece in managed c++.

A moderator has moved the post from the Visual C# Language to the Visual C++ General.

there must be someone who is very good at structure layout or mashaling. if someone can tell me is absolutely not doable, it helps saving my time seaching for solutions.

so far I have one not very good solution, in unmanaged code, I flattened the structure to a string seperated by comma,

then on the C# or managed c++ side unpack the string back to structure. but I had to overloading the unpacking function many times.

thanks for your post

Belinda





Re: Visual C++ General copy unmanaged memory block to managed memory pointer

BLiu

the structures I have usually contains int, double, char, array of int, array of double, array of char, but no pointer. I am wondering if managed code can use FieldOffset to put 3 individual int into continuous memory block, why it can not put an array of integers int[3] into a continuous memory block.

I have a few union structures, but I am not to worry about those yet.

thanks

Belinda





Re: Visual C++ General copy unmanaged memory block to managed memory pointer

Simple Samples

I think you are getting a run-around. This is both a C# question and a C++ question and I think it is more of a C# question. It would have help if the moderator had posted a comment saying the thread was moved; most moderators do. The moderator was wrong to move this to the VC general forum; it should at least be in the VC langauge forum.

What you can do is to create another thread in the C# language forum that has a link to here and that emphasizes the C# questions. Then post a message here with a link to the other thread, so there are double links. Use of two threads such as that should be avoided of course and I am sure you would avoid doing it but in this situation I think it is appropriate.

See Moderator Bug.

I do definitely understand "helps saving my time seaching for solutions".