Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / July 2006

Tip: Looking for answers? Try searching our database.

P/invoke: passing a struct parm with enum field from c++ to c#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cmurialdo@gmail.com - 06 Jul 2006 04:16 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.