Hi, Meng.

The problem is that you are passing a C++ class method to sqlite3_exec.
sqlite3_exec expects a callback function using the CDECL calling
convention, and by default class methods do not use that calling
convention when compiled under Visual C++ (they use thiscall, IIRC.)

The reason it worked in your console app was most likely because the
callback was not a class member there, and you were compiling it in C
compatibility mode.

To fix this, either declare that class member as using the CDECL calling
convention, or use a top level function (also using the CDECL calling
convention) and use the extra pointer parameter to sqlite3_exec (the
"notused" one in your code) to pass the pointer to your CMainView
instance. Inside the callback you can cast that void pointer back to a
CMainView and invoke the "real" callback function which is a member of
your class.

HTH,
Mihai Limbasan

On 09/28/2009 08:55 AM, meng wei wrote:
> Hi everyone!
> I met no problem with win32 console application,but when it turn to MFC
> in Visual C++,the same code dosen't work.
> Anyone can give me some suggestion?
> Thanks in advance!
>
> *The error is:*
> Unhandeled exception in xxx.exe(sqlite3.dll)0xC0000005 Access Violation
> *More output:*
> Loaded 'ntdll.dll', no matching symbolic information found.
> Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic
> information found.
> Loaded symbols for 'C:\WINDOWS\system32\MFC42D.DLL'
> Loaded symbols for 'C:\WINDOWS\system32\MSVCRTD.DLL'
> Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information
> found.
> Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic
> information found.
> Loaded symbols for 'C:\WINDOWS\system32\MFCO42D.DLL'
> Loaded 'G:\MFC\复件 SenateSystem\Debug\sqlite3.dll', no matching
> symbolic information found.
> Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\imm32.dll', no matching symbolic information
> found.
> Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\secur32.dll', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\lpk.dll', no matching symbolic information
> found.
> Loaded 'C:\WINDOWS\system32\usp10.dll', no matching symbolic information
> found.
> Loaded 'C:\WINDOWS\system32\mfc42loc.dll', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\uxtheme.dll', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\MSCTF.dll', no matching symbolic information
> found.
> Loaded 'C:\WINDOWS\system32\version.dll', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\msctfime.ime', no matching symbolic
> information found.
> Loaded 'C:\WINDOWS\system32\ole32.dll', no matching symbolic information
> found.
> Loaded 'C:\WINDOWS\system32\comctl32.dll', no matching symbolic
> information found.
> First-chance exception in SenateSystem.exe (SQLITE3.DLL): 0xC0000005:
> Access Violation.
>
>
> *The following is my code:
>
> //access violation happened at here
> *void CMainView::myUpdateView()
> {
> CSenateSystemDoc* pDoc = GetDocument();
> sqlite3_exec(pDoc->*m_db*,"select * from table1",
> sqlCallback_UpdateList, 0, &(pDoc->*m_pErrMsg*));
> //The database *has been connected* before this line runs
> }
> *//declaration of m_db and m_pErrMsg
> *class CSenateSystemDoc : public CDocument
> {
> ...
> ...
> public:
> bool bLoggedIn;
> bool m_dbConnected;
> sqlite3 * m_db;
> char * m_pErrMsg;
> CString m_strSQL;
> ...
> ...
> *//CALL BACK FUNCTION*
> int CMainView::sqlCallback_UpdateList(void * notused, int argc, char **
> argv, char ** szColName)
> {
> ... //These lines have nothing to do with access violation
> ... //,since my program crashed before it went to the breakpoint I set here
> return 0;
> }
> *//declaration of the callback function*
> class CMainView : public CListView
> {
> protected:
> ...
> ...
> static int sqlCallback_UpdateList(void * notused, int argc, char **
> argv, char ** szColName);
> ...
> ...
>
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>   
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to