Marshalling bool
In my C++ file I have method which have Bool parameter. For Linux I need to have UnmanagedType.VariantBool marshaller and for windows (for some reason) UnmanagedType.Bool.
Is there a way to make custom marshaller what will just select and call any of those 2 marshallers based on RuntimeInformation.IsOSPlatform?
17 Replies
if you are using
bool
in C++ neither of those are correctBOOL
Actually VARIANT_BOOL isShared
well, UnmanagedType.Bool is not right for VARIANT_BOOL either
it is the wrong size and the wrong value for true
it may happen to work sometimes
Let's say that there are some preprocessor shananingens and that in one case I need Bool in other Variant bool
Can I create CustomMarshaller that will pick the right marshaling of those bools?
I konw it is not directly related, but does anyone know if select WinApi calling convention, will that select correct one on all platforms?
ICustomMarshaler only knows how to pass a pointer to native code, it can't be used to pass any other kind of data type
and the LibraryImport generator must know the exact unmanaged form of the type at compile time in order to generate the C#, it can't be switched around
so, you have to write two different P/Invokes in C# and put IsOSPlatform wrappers around all of the calls yourself
as for WinApi, it selects the calling convention that it selects, whether that's the right one depends on whether it's the one that you use
CallingConvention.WinApi is the same as the "default" which is __stdcall
Can i use 2 delegates asigned to same value.
Where delegates tell have marshaling and that variable dont?
that said, it only matters if you are running on x86, because on x64 each operating system has its own single calling convention that .NET always chooses
uh, you would have to use DynamicInvoke, because the delegate type determines the marshalling
I saw (after decompiling assembly) that for linux it is c style and on windows fast
So for x64 I can just keep StdCall?
for x64 everything you specify is ignored because there are no choices
Thatbis cool
for x86 if it's really fastcall then .NET doesn't support fastcall so you can't really call it
Tnx a lot for both answers
Two more question if I may ask. Is there a way to debug native part from managed on Linux (WLS).
And if applicattion abruptly stop on CG.Collect, is that a sign that I messed up the stack?
no, there's only "mixed" debugging on Windows
Damn it
if GC.Collect causes a problem then it could be a sign of some kind of heap corruption or something like this yes
Is managed and unmanaged heap shared?
Can I just debbug native part? in same application?
the address space is shared, so native code can corrupt managed code for sure
generally you get there by passing a pointer to a managed object to native code, and it doing something wrong with that object
but the unmanaged heap for malloc and free is separate and far apart in the address space from the GC heap
on linux, i am pretty sure you can, but it might be a little unpredictable