Hi,

I need to adapt a snippet of code interfacing a low-level driver
written in C++ so as to make it work under C#. I can compile and make
it work without any problem in C++, but I can't guess how to make it
work in C#.

Broadly speaking, the difficulty finds itself in the passing of a
struct as variable to a DLL function in C#.

The struct (itself containing another struct) looks like this:

In C++
=====

struct {
        short s11;
        short s12;
        short s13;
        short s14;
        struct2 sa[20]
} struct1

struct{
        long l21;
        long l22;
        long l23;
        long l24;
        short s21;
        short s22;
        short s23;
        short s24;
} struct2

In C# (Adapting the long to int type so as to occupy 4 bytes)
====

struct {
        short s11;
        short s12;
        short s13;
        short s14;
        struct2 sa[20]
} struct1

struct{
        int l21;                // long in C++ --> int in C# (4 bytes)
        int l22;
        int l23;
        int l24;
        short s21;
        short s22;
        short s23;
        short s24;
} struct2

and the function looks like this:

In C++
=====

Declaration...
-----------------
int function1 (struct1* pStruct1);
Call...
--------
int1 = function1 (&pStruct1);           // Once pStruct1 filled with data

In C# (Here come the problems):
========================

Declaration...
-----------------
[DllImport(_dllLocation)]
public static extern int function1(
            struct1 pStruct1,
);

Call...
-------
int1 = StaticClass.function1(pStruct1); // Once pStruct1 filled with
data

***********************

I've tried different alternatives, but with no result at all:
1) Declaring...
Before the struct... [StructLayout(LayoutKind.Sequential, Pack=1,
Size=488)] / Size=24)]
Between the fields in the struct... [FieldOffset(0)] / [FieldOffset
(2)] / ...
I receive an error HRESULT "(HRESULT Exception: 0x80070057
(E_INVALIDARG))"
2) Declaring...
Before the struct array... [MarshalAs
(UnmanagedType.SafeArray,SizeConst=20)
I receive the same error
3) Letting pStruct only declared, but no filled with data, the
function is allowed to be called, but no processing is observed.

Could any help / recommendation be given, I would be really grateful,
since I am at a standstill at this moment and I don't know how to go
on.

Thanks a lot.

Reply via email to