SQLite Forum

sqlite3.dll is not returning control to my program
Login
I do not see anything definitely wrong with what you have shown. This makes me think something you're doing not yet shown is wrong. It would be a tedious guessing game to venture all the ways you might be erring.

Instead, I created this silly little C++ish program:<code>
\#include \<iostream\>
\#include \<string\>
\#include \<assert.h\>
\#include "sqlite3.h"
//
using std::cout;
using std::cerr;
using std::endl;
using std::string;
//
int execCallback(void\*,int nc,char\*\* pzColValues, char\*\* pzColNames){
  cout \<\< "Result row:" \<\< endl;
  for( int ic = 0; ic \< nc; ++ic ){
    char * zValue = (pzColValues[ic])? pzColValues[ic] : "NULL";
    cout \<\< "  " \<\< pzColNames[ic] \<\< ": " \<\< zValue \<\< endl;
  }
  return 0;
}
//
int main(int an, char \*ap[]){
  if( an < 2 ){
    cerr \<\< "Usage: sdb_futz \<dbName\> [\<query1\> ...]" << endl;
    return 1;
  }
  string dbName(ap[1]);
  sqlite3 \* db = 0;
  int rc = sqlite3_open( dbName.c_str(), &db );
  if( rc != SQLITE_OK ){
    cerr \<\< "Cannot open " << dbName << " as DB." << endl;
    return 2;
  }
  int nFlubs = 0;
  for( int na = 2; na < an; ++na ){
    char \* zErr = 0;
    string query(ap[na]);
    cout \<\< "Running query: " \<\< query \<\< endl;
    rc = sqlite3_exec(db, query.c_str(), execCallback, 0, & zErr);
    if( rc != SQLITE_OK ){
      cerr \<\< "Exec failed with error " \<\< rc
           \<\< " (" \<\< (zErr? zErr : "?") \<\< ")" \<\< endl;
      ++nFlubs;
      sqlite3_free(zErr);
    }
  }
  rc = sqlite3_close(db);
  return ( rc == SQLITE_OK && nFlubs == 0 )? 0 : 2+nFlubs;
}
//
</code>. This is what I did to build it:<code>
REM Prior to this, get 64-bit sqlite3.dll, sqlite3.def and
REM sqlite3.h downloaded and extracted to this directory.
REM Then, in a shell setup for MSVC 64-bit build tools, run:
lib /def:sqlite3.def /out:sqlite3.lib /machine:amd64
REM Next, create something like above source:
emacs_edit sdb_futz.cpp
REM Then build and run it:
cl -DDEBUG -EHsc sdb_futz.cpp /link sqlite3.lib
sdb_futz furd.sdb "create table Silly(why text)"
sdb_futz furd.sdb "insert into Silly values ('No reason'),('Why am I silly?')"
sdb_futz furd.sdb "select * from Silly" "select count(*) from Silly"
</code>.

This ran as I would expect, so I'll not bore anybody with output.

I propose that you figure out how your code and build differ from the above. You can replicate my build, then use DUMPBIN to examine our objects and image to perhaps discover if linkage or calling convention has gone awry. (Naming conventions should prevent such problems, but they may have been circumvented.)

Your observation, "problem temporarily goes away if I restart VS2019", is not much to go on. But it does suggest you are causing the dreaded C/C++ undefined behavior to occur, just as your other symptoms suggest. (Failure of return from a SQLite API means something fundamental is wrong.)

You may notice that my build does virtually nothing to establish DLL linkage or set calling conventions. You may want to try that among your other explorations.

If you are serious about getting help with your problem here, you will need to simplify your code and build to something others here can reasonably hope to replicate. The Visual Studio IDE is nice, but it hides a tremendous amount of detail in its project files.

Obviously, you are building something which plays poorly with the sqlite3.dll published by the SQLite project. Yet, as you can see, I can build something that plays well with that DLL with little trouble.