Unmanaged code and NullReference Exception problem (PLEASE HELP)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SpookyET

    #1

    Unmanaged code and NullReference Exception problem (PLEASE HELP)

    I want to create a wrapper for SQLite and I'm having a problem with getting
    NullReferenceEx ception when calling the unmanaged function. I do not wish to
    write in C++ since i do not have enough experience with it. The problem I'm
    having is that after some 17 rows I get NullReferenceEx ception while there
    are more than 200 rows in the table. SQLite is an embeddable C sql server,
    very fast and very small. www.sqlite.org
    I cant use MSDE do to licence issues and size issues. I can't put a 54MB
    database inside a 1mb app. SQLite is 250K.
    I didn't pate this code, i typed it so there might be some types of var
    names.

    Main()
    {
    ...
    IntPtr sqliteHandler = SQLiteWrapper.O pen(path, mode, out errorMessage)
    CallbackFunctio nHandler callBackFunctio nHandler = new (CallbackFuncti on);
    // I have even tried to put GC.KeepAlive(ca llBackFunctionH andler) here but
    doesn't work
    // it shouldn't go out of scope anyway
    SQLiteWrapper.E xecute(sqliteHa ndler, "SELECT * FROM Users",
    callBackFunctio nHandler, IntPtr.Zero, out errorMessage); // it does this 17
    times and cracks
    }

    unsafe public static int CallbackFunctio n(IntPtr userData, int
    columnsNumber, sbyte** columnsData, sbyte** columnsNames)
    {
    for (int i = 0; i < columnsNumber; i++)
    {
    string column = new String(columnsN ames[i]));
    string result = new String(columsnD ata[i]));
    Console.WriteLi ne("{0} = {1}", column, result);
    }
    }


    Original Function
    int sqlite_exec(
    sqlite*, /* An open database */
    const char *sql, /* SQL to be executed */
    sqlite_callback , /* Callback function */
    void *, /* 1st argument to callback function */
    char **errmsg /* Error msg written here */|
    );

    C# Wrapper
    [DllImport("sqli te.dll", EntryPoint="sql ite_exec",CharS et=CharSet.Ansi ,
    ExactSpelling=t rue)]
    public static extern int Execute(IntPtr sqliteHandler, string sqlCommand,
    CallbackFunctio nHandler callBack , IntPtr userData, out string
    errorMessage);

    C# CallBackFunctio n Pointer
    unsafe public delegate int CallbackFunctio nHandler(IntPtr userData, int
    argc, sbyte** argv, sbyte** colnames);

    Original Function Pointer implementation
    typedef int (*sqlite_callba ck)(void*,int,c har**, char**);

    There is another way to get records from SQLite but it returns char*** which
    is a pointer to an array of pointers to strings (C strings)

    i tried sbyte*** foo
    and new String(*foo[0]) but i get nothing, tried with char too instead of
    sbyte

    Please help.
    I thank you in advance.


  • Mattias Sjögren

    #2
    Re: Unmanaged code and NullReference Exception problem (PLEASE HELP)

    >Main()[color=blue]
    >{
    > ...
    > IntPtr sqliteHandler = SQLiteWrapper.O pen(path, mode, out errorMessage)
    > CallbackFunctio nHandler callBackFunctio nHandler = new (CallbackFuncti on);
    > // I have even tried to put GC.KeepAlive(ca llBackFunctionH andler) here but
    >doesn't work
    >// it shouldn't go out of scope anyway
    > SQLiteWrapper.E xecute(sqliteHa ndler, "SELECT * FROM Users",
    >callBackFuncti onHandler, IntPtr.Zero, out errorMessage); // it does this 17
    >times and cracks
    >}[/color]


    You should put the GC.KeepAlive call after the Execute call, not
    before. See if that helpst keeping the delegate valid.

    [color=blue]
    >C# Wrapper
    >[DllImport("sqli te.dll", EntryPoint="sql ite_exec",CharS et=CharSet.Ansi ,
    >ExactSpelling= true)]
    >public static extern int Execute(IntPtr sqliteHandler, string sqlCommand,
    >CallbackFuncti onHandler callBack , IntPtr userData, out string
    >errorMessage );[/color]

    Also, I think it would be better if you changed the last parameter to

    out char* errorMessage

    or

    out IntPtr errorMessage



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    Working...