Hello, 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
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;
which translated to c# would have been something like this (I guess, correct me if I'm wrong):
[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)???
}
and my ItemClass looks like this:
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);
}
}
In .h file there's a definition for this function:
bool search_queryTextIn(const LPBYTE condition, bool queryNext);
which I think translates to c# like this:
[DllImport("search.dll")]
protected static extern bool search_queryTextIn(
[MarshalAs(UnmanagedType.LPArray)] byte[] condition,
[MarshalAs(UnmanagedType.I1)] bool queryNext);
Then, inside the c++ code, the function is called using this:
void queryTextIn(TEXTIN_QUERY_CONDITION& condition, bool queryNext)
{
_searchCore.queryTextIn(&condition, queryNext);
}
and the _searchCore.queryTextIn method is defined like this:
bool SearchSDK_Class::queryTextIn(const LPTEXTIN_QUERY_CONDITION condition, bool queryNext)
{
return ::search_queryTextIn((LPBYTE)condition, queryNext);
}
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.