c++ cli - Resolving error C3821: managed type or function cannot be used in an unmanaged function -
i'm writing c++/cli layer handle interop.
the native api populates complex construction involving fixed arrays, unions, anonymous structures, etc:
typedef struct declspec_align(16) _foo { union { bar bar; struct { pop array[8]; dword more; }; }; } foo, *pfoo; i'm trying translate info construction more "sane" .net class, consumption c#. problem is, can't utilize legacy structure, , gcnew new class in same function:
foo^ test::getfoo(handle h) { foo foo; // unmanaged if (!::getfoo(h, &foo)) throw gcnew exception("getfoo failed"); foo^ result = gcnew foo(); // managed // populate result homecoming result; } doing gives error:
error 2 error c3821: 'void test::getfoo(handle)': managed type or function cannot used in unmanaged function
if native construction , gcnew cannot exist in same function, how can 1 ever hope (even manually) marshall info between two?
many q/a here involve wrapping unmanaged classes, seems irrelevant here.
aligned info types not supported in managed code
that's real error message, unfortunately not show in error list window. can see in output window. maintain in mind when compiler error messages bizarre.
and yes, accurate, managed code doesn't run stack alignment guarantees needed construction aligned. 32-bit code runs alignment of 4, 64-bit code can provide 8. not plenty 16. nil compiler can either, usual stack pointer manipulations not available in il, screws metadata jitter generates tells garbage collector object references when walks stack.
so, no can do, cannot create local variable. you've got choices, straight-forward way allocate it:
#include <malloc.h> .... foo* value = (foo*)_aligned_malloc(sizeof(foo), __alignof(foo)); seek { // etc... } { _aligned_free(value); } c++-cli
No comments:
Post a Comment