I'm trying to marshal a struct that is returned by my native code but I get System.Runtime.InteropServices.MarshalDirectiveException
It is not a output argument that already answered in other posts, it is return type.
C++ code:
typedef struct
{
bool success;
ErrorCode error_code;
char error_path[1025];
} Result;
DLLEXPORT Result GetResult();
ErrorCode is an enum,
C# equivalent:
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
public byte success;
public ErrorCode error_code;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 1025)]
public char[] error_path;
}
[DllImport("shared", EntryPoint = "GetReult", CallingConvention = CallingConvention.Cdecl)]
public extern static Result GetResult();
I know the return stucts in C# p/invoke should be blitable type but I don't know if I can make my struct blittable using the Marshaling directives or not.
Is there any way do do that or is something other is wrong with my code?
If there is no way I need to change my API and make the return type as output argument.
Thanks.
You can make it blittable by using a fixed size buffer:
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Result
{
public byte success;
public ErrorCode error_code;
public fixed sbyte error_path[1025];
}
Note that I have used sbyte for the array element type. That's an 8 bit type that matches the unamanged char type which is also an 8 bit type. You used char in your C# which is a 16 bit type.
You may need to convert the fixed size buffer to a string, but exactly how to do that depends upon the encoding that you used. However, there are plenty of articles on that topic (converting fixed size buffer to string) that you can find by web search.
Related
I've written a dll in C which has functions I can call when referencing the DLL in C#. If I use a basic type like an int it works fine, but I have structs which are slightly different in C# than they are in C due to language differences. Here is an example. This is the function definition in C#:
[DllImport("hello_world_cuda.dll", CharSet = CharSet.Auto)]
public static extern Batch Cut();
And here is it in C:
extern "C" Batch __declspec(dllexport) __stdcall Cut()
You can see the return type Batch is the same, but here is its definition in C#
class Envelope
{
public byte[] Payload;
public byte[] Signature;
}
class Batch
{
public Envelope[] Messages;
public int MsgCount;
}
And here is the definition in C
struct Envelope
{
public:
char* Payload;
char* Signature;
};
struct Batch
{
public:
Envelope* Messages;
int MsgCount;
};
How do I overcome these language differences in order to successfully make the DLL call in C#?
You should define Envelope and Batch as structs in C# too, and apply the StructLaylout attribute:
e.g.:
[StructLayout(LayoutKind.Sequential, Pack=0)]
struct Envelope
{
...
}
Pointers in an unmanaged language do not map to managed arrays as you have done, this is why it is complaining. char is (almost always, with very limited exceptions) an 8 bit value that maps well to byte in C# as you've noticed, but you need to make them pointers in the managed struct as well:
unsafe struct Envelope
{
public byte* Payload;
public byte* Signature;
}
I am trying to access a Double Dummy Solver dll (http://privat.bahnhof.se/wb758135/bridge/dll.html ) of unmanaged C++ code from a C# project, but I get the following error message:
An unhandled exception of type 'System.AccessViolationException'
occurred in Dds.Net.dll
Additional information: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.
The error seems to be around calling the method
Par which takes the three arguments
struct ddTableResults *tablep, struct parResults *presp, int vulnerable
Specifically, related to passing in the 2nd parameter which is described to be:
struct parResults
char parScore[2][16];
char parContractsString [2][128];
Here is my code:
My c# struct:
using System.Runtime.InteropServices;
namespace Dds.Net.Integration
{
[StructLayout(LayoutKind.Sequential)]
internal struct ParResults
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst =32)]
public char[,] parScore;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public char[,] parContractString;
}
}
dllimport to call the function :
[DllImport("dds.dll")]
public static extern int Par(DdTableResults tablep, int vulnerable, ParResults parResults);
Any idea of what I can do to get this working?
Many thanks!
as far as I understand, you c++ signature is
int Par(struct ddTableResults *tablep, struct parResults *presp, int vulnerable)
the c# one maybe is
[DllImport("dds.dll")]
public static extern int Par(ref DdTableResults tablep, ref ParResults parResults, int vulnerable);
c++ wants a poitner to DdTableResults and ParResults, without ref c# will pass structure by value.
Quiz: what does the following program print?
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2 {
[StructLayout(LayoutKind.Sequential, Pack=1)]
struct Struct1 {
bool b;
int i;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
struct Struct2 {
byte b;
int i;
}
class Program {
static void Main(string[] args) {
Console.WriteLine(Marshal.SizeOf(typeof(Struct1)));
Console.WriteLine(Marshal.SizeOf(typeof(Struct2)));
Console.ReadKey();
}
}
}
Answer:
8
5
This is very confusing to me. Both bool and byte have a size of 1 byte, and specifying [StructLayout(LayoutKind.Sequential, Pack=1)] should nullify any padding issues. Both structs should be 5 bytes. So I have two questions:
Why does marshalling work this way?
Any workaround? I have 1-byte booleans in native structs I need to import. I can use byte instead of course but it's "messy".
Thanks.
By default, .NET type bool marshals to unmanaged type BOOL, which is typedefed to int. If you want to marshal to and from 1-byte unmanaged booleans, indicate this to the marshaler with an attribute:
[StructLayout (LayoutKind.Sequential, Pack=1)]
struct Struct3 {
[MarshalAs (UnmanagedType.I1)]
bool b;
int i;
}
Console.WriteLine (Marshal.SizeOf (typeof (Struct3))) ; // prints 5
bool gets Marshalled to an int32, for interoperability reasons (C/C++ programs usually use int as booleans, and in winapi BOOL is typedefed as an int as well), therefore it's converted to 4 bytes.
I am using API written in C++ in my code (writting in C#).
API requires a parameter as Pointer to Structure.
The Structure consists of "Int"s and Char Arrays:
for example
unsafe public struct ToBePassed
{
Int32 Num1;
Int32 Num2;
Char[] Data; // or fixed Char Data[255];
}
I can not directly pass the structure pointer to API because in that case, I am getting error as "Pointers cannot reference Marshaled structures". Code get compiled successfully but this Error comes when I execute (Debug) the code.
Now I have two options:
1st:- Passing Structure by Ref: I want to ask does an API requiring A Structure Pointer can receive the address when I pass the structure by ref. Note that API will return Data in "Char[] Data".
2nd:- Using Marshal.StructureToPtr: This will convert Structure Pointer to IntPtr. Again the Doubt is same, Will that API receive it correctly?
Thanks for Your Time!
Regards,
Swanand
If it only requires pointer, you can allocate some unmanaged memory, marshal the structure to the memory, and pass that pointer to your function. Then afterwards you could marshal back to the structure (if you wish) and free the memory. Before you marshal anything, you need to properly define the structure. Something like this:
[StructLayout(
LayoutKind.Sequential, //must specify a layout
CharSet = CharSet.Ansi)] //if you intend to use char
public struct ToBePassed
{
public Int32 Num1;
public Int32 Num2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
public Char[] Data; //specify the size using MarshalAs
}
[DllImport("...")]
public static extern void APICall(IntPtr argPtr);
public static void CallFunction(ToBePassed managedObj)
{
IntPtr unmanagedAddr = Marshal.AllocHGlobal(Marshal.SizeOf(managedObj));
Marshal.StructureToPtr(managedObj, unmanagedAddr, true);
APICall(unmanagedAddr);
Marshal.PtrToStructure(unmanagedAddr, managedObj);
Marshal.FreeHGlobal(unmanagedAddr);
unmanagedAddr = IntPtr.Zero;
}
[edit]
To simulate variable length arrays, allocate unmanaged memory within the structure and initialize as usual.
[StructLayout(LayoutKind.Sequential)]
public struct SomeStruct
{
public Int32 X;
public Int32 Y;
}
[StructLayout(LayoutKind.Sequential)]
public struct VLA
{
public Int32 intArrayLength;
public Int32 SomeStructArrayLength;
public IntPtr intArray;
public IntPtr SomeStructArray;
}
public static VLA CreateVLA(int[] intArray, SomeStruct[] SomeStructArray)
{
var vla = new VLA()
{
intArrayLength = intArray.Length,
SomeStructArrayLength = SomeStructArray.Length,
intArray = Marshal.AllocHGlobal(intArray.Length * Marshal.SizeOf(typeof(int))),
SomeStructArray = Marshal.AllocHGlobal(SomeStructArray.Length * Marshal.SizeOf(typeof(SomeStruct))),
};
Marshal.Copy(intArray, 0, vla.intArray, intArray.Length);
//there's no overload to copy arbitrary arrays, do it manually
for (int i = 0; i < SomeStructArray.Length; i++)
{
Marshal.StructureToPtr(
SomeStructArray[i],
vla.SomeStructArray + i * Marshal.SizeOf(typeof(SomeStruct)),
true);
}
return vla;
}
I have to call a C++ DLL from my C# program.
I'm trying to do it using PInvoke - everything works fine in VS2005\ 2008, but after migration to VS 2010, I get this exception:
PInvokeStackImbalance was detected
Message: A call to PInvoke function
'sampleFunc' has unbalanced the stack.
This is likely because the managed
PInvoke signature does not match the
unmanaged target signature. Check that
the calling convention and parameters
of the PInvoke signature match the
target unmanaged signature.
This is the original C++ prototype:
typedef struct {
unsigned short field1;
unsigned short field2;
} sInfo;
_declspec(dllexport) int sampleFunc(sInfo *info, char *txt);
and here is the C# code:
[StructLayout(LayoutKind.Sequential)]
struct SInfo
{
//[MarshalAs(UnmanagedType.U1)] //also tried with the MarshalAs attr. Didn't help.
public ushort field1;
//[MarshalAs(UnmanagedType.U1)]
public ushort field2;
};
[DllImport("sampleModule.dll", CharSet=CharSet.Ansi)]
public static extern int sampleFunc(ref SInfo info, [MarshalAs(UnmanagedType.LPStr)] string txt);
I've tried it also with IntPtr instead of the ref SInfo, but got the same result...
Any help will be appreciated,
Thank you all!
Hard to see how this could have worked before. The C++ declaration doesn't declare the calling convention, the default is __cdecl unless overridden in the C++ project with the /Gz compile option. You have to tell the P/Invoke marshaller:
[DllImport("sampleModule.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int sampleFunc(ref SInfo info, string txt);
This probably has to do with how your packing the struct. The default Pack size is 8, so its probably thinking you have too many bytes. Try setting the Pack size to 2 (16 bit aligned) and see if that helps:
[StructLayout(LayoutKind.Sequential, Pack=2)]
Alternatively you can specify the offsets like this:
[StructLayout(LayoutKind.Explicit)]
public struct struct1
{
[FieldOffset(0)]
public ushort a; // 2 bytes
[FieldOffset(2)]
public ushort b; // 2 bytes
}
Here is a good reference on packing