I have a c++ code that needs to be translated to c#, almost done (I think) but need help with P/Invoke to use an unmanaged dll
<p>I'm having a hard time with a project (in c++.net) that needs to be converted to c#. This project uses an API written in unmanaged c++. I have the API´s .lib, .dll and .h, and want to call the dll functions from inside .net using p/invoke. Ok, here's the code:
First, there is this c++ struct</p>

<pre><code>typedef struct {
    struct Item {
        int  condition;
        char name[80];

        Item::Item()
        {
            reset();
        }

        void Item::reset()
        {
            condition = 0;
            memset(name, 0, sizeof(name));
        }
    };

    bool    channel[32];
    int     itemCount;
    Item    item[5];
} TEXTIN_QUERY_CONDITION, *LPTEXTIN_QUERY_CONDITION;
</code></pre>

<p>which translated to c# would have been something like this (I guess, correct me if I'm wrong):</p>

<pre><code>[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct TEXTIN_QUERY_CONDITION
{
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, 
    SizeConst = 32, ArraySubType = System.Runtime.InteropServices.UnmanagedType.Bool)]
    public bool[] channel;
    public int itemCount;
    public ItemClass[] item; //how define the size (5)???
}
</code></pre>

<p>and my ItemClass looks like this:</p>

<pre><code>class ItemClass
{
    public Item item = new Item();

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, 
    CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
    public struct Item
    {
        public int condition;
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, 
        SizeConst = 80)]
        public string name;
    }

    public ItemClass()
    {
        reset();
    }

    public void reset()
    {
        item.condition = 0;
        item.name = new string('0', 80);
    }
}
</code></pre>

<p>In .h file there's a definition for this function:</p>

<pre><code>bool search_queryTextIn(const LPBYTE condition, bool queryNext);
</code></pre>

<p>which I think translates to c# like this:</p>

<pre><code>[DllImport("search.dll")]
protected static extern bool search_queryTextIn( 
    [MarshalAs(UnmanagedType.LPArray)] byte[] condition, 
    [MarshalAs(UnmanagedType.I1)] bool queryNext);
</code></pre>

<p>Then, inside the c++ code, the function is called using this:</p>

<pre><code>void queryTextIn(TEXTIN_QUERY_CONDITION&amp; condition, bool queryNext)
{
    _searchCore.queryTextIn(&amp;condition, queryNext);
}
</code></pre>

<p>and the _searchCore.queryTextIn method is defined like this:</p>

<pre><code>bool SearchSDK_Class::queryTextIn(const LPTEXTIN_QUERY_CONDITION condition, bool queryNext)
{
    return ::search_queryTextIn((LPBYTE)condition, queryNext);
}
</code></pre>

<p>which calls the function defined in .h above. My major problem is how to call this function from c#. First, I created an instance of the TEXTIN_QUERY_CONDITION struct, but I don't know how to pass a pointer to this struct as a parameter, and how convert it to a byte array to finally call the function... Please, please, are there some kind soul that help me figure out that last bit to call the unmanaged function? Thanks a million.</p>
