I have a problem passing a struct with an enum field.
I have this c++ code:
typedef struct st_mysql_bind
{
....
my_bool *error;
enum enum_field_types buffer_type;
....
}MYSQL_BIND;
enum enum_field_types { MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,
MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
......};
my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT * stmt, MYSQL_BIND *
bnd);
I declared the following in c#:
[StructLayout(LayoutKind.Sequential)]
public unsafe class MYSQL_BIND
{
.....
public sbyte error;
public enum_field_types buffer_type;
.....
}
public enum enum_field_types
{
MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,
MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
.......
}
[ DllImport( "libmySQL.dll", EntryPoint="mysql_stmt_bind_result" )]
unsafe public static extern sbyte mysql_stmt_bind_result(IntPtr stmt,
ref MYSQL_BIND[] result);
When It calls mysql_stmt_bind_result it execute something like this:
bind = new MYSQL_BIND[4];
bind[0] = new MYSQL_BIND();
bind[0].buffer_type = enum_field_types.MYSQL_TYPE_LONG;
mysql_stmt_bind_result(stmt, ref bind);
The problem is that the buffer_type of MYSQL_BIND type is not a valid
type (it means a value from the enum values) when executing
mysql_stmt_bind_result (I get an error from mysql_stmt_bind_result
because of the invalid number of buffer_type).
Is it the correct way to pass enum fields in structs?.
Any help would be appreciated.
Mattias Sjögren - 06 Jul 2006 06:27 GMT
> [ DllImport( "libmySQL.dll", EntryPoint="mysql_stmt_bind_result" )]
> unsafe public static extern sbyte mysql_stmt_bind_result(IntPtr stmt,
>ref MYSQL_BIND[] result);
^^^
Remove the ref keyword. Since arrays are reference types you
automatically have a level of indirection so ref isn't needed (and in
fact incorrect).
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.