C
C#4mo ago
Vasoli

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
reflectronic
reflectronic4mo ago
if you are using bool in C++ neither of those are correct
Vasoli
VasoliOP4mo ago
BOOL Actually VARIANT_BOOL isShared
reflectronic
reflectronic4mo ago
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
Vasoli
VasoliOP4mo ago
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?
reflectronic
reflectronic4mo ago
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
Vasoli
VasoliOP4mo ago
Can i use 2 delegates asigned to same value. Where delegates tell have marshaling and that variable dont?
reflectronic
reflectronic4mo ago
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
Vasoli
VasoliOP4mo ago
I saw (after decompiling assembly) that for linux it is c style and on windows fast So for x64 I can just keep StdCall?
reflectronic
reflectronic4mo ago
for x64 everything you specify is ignored because there are no choices
Vasoli
VasoliOP4mo ago
Thatbis cool
reflectronic
reflectronic4mo ago
for x86 if it's really fastcall then .NET doesn't support fastcall so you can't really call it
Vasoli
VasoliOP4mo ago
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?
reflectronic
reflectronic4mo ago
no, there's only "mixed" debugging on Windows
Vasoli
VasoliOP4mo ago
Damn it
reflectronic
reflectronic4mo ago
if GC.Collect causes a problem then it could be a sign of some kind of heap corruption or something like this yes
Vasoli
VasoliOP4mo ago
Is managed and unmanaged heap shared? Can I just debbug native part? in same application?
reflectronic
reflectronic4mo ago
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
Want results from more Discord servers?
Add your server