2

Cを使用してvs2012でSQLite Prepare-Statementを実装しています.

このリンクをチュートリアルとして使用して、コードを実行するたびにマシンで実行されているプロセスをキャプチャします。準備ステートメントで do-while ループの外に置くことができないことを除いて、すべてが正常に機能しています。do-while ループの中に入れた場合、db-insertion ごとに準備ステートメントが再度実行されるため、実際には何もしていませんが、これは実用的ではありません。ただし、準備ステートメントを do-while ループ内に配置してコードを実行すると、問題なく動作します (ただし、挿入する必要がある数の挿入を実行します)。do-while ループの外に置くと、メモリの警告とブレークが発生します。

これが私がSQLiteを実装する方法です:

BOOL GetProcessList(sqlite3 *db)
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;
  sqlite3_stmt* stmt;
  char *errorMessage;  
  char query[80] = "INSERT INTO Process_list VALUES (?1, ?2, ?3, ?4, ?5);";

  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

  if( hProcessSnap == INVALID_HANDLE_VALUE )
  {
    printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
    return( FALSE );
  }

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {
    printError( TEXT("Process32First") ); // show cause of failure
    CloseHandle( hProcessSnap );          // clean the snapshot object
    return( FALSE );
  }

  // Now walk the snapshot of processes, and
  // display information about each process in turn

  sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Process_list("  
    "Process_ID     INT PRIMARY KEY     , " 
    "Thread_count       INT     ,   "
    "Parent_PID     INT     ,   "
    "Priority_Base      INT     ,   " 
    "Priority_class INT     );", NULL, NULL, &errorMessage);

    sqlite3_prepare_v2(db, query,strlen(query), &stmt,NULL); // can't put it here

  do
  {
    _tprintf( TEXT("\n\n=====================================================" ));
    _tprintf( TEXT("\nPROCESS NAME:  %s"), pe32.szExeFile );
    _tprintf( TEXT("\n-------------------------------------------------------" ));

    // Retrieve the priority class.
    dwPriorityClass = 0;
    hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
    if( hProcess == NULL )
    {
      printError( TEXT("OpenProcess") );
    } else {
        dwPriorityClass = GetPriorityClass( hProcess );
        if( !dwPriorityClass )
        {
            printError( TEXT("GetPriorityClass") );
        }
        CloseHandle( hProcess );
    }

    _tprintf( TEXT("\n  Process ID        = 0x%08X"), pe32.th32ProcessID );
    _tprintf( TEXT("\n  Thread count      = %d"),   pe32.cntThreads );
    _tprintf( TEXT("\n  Parent process ID = 0x%08X"), pe32.th32ParentProcessID );
    _tprintf( TEXT("\n  Priority base     = %d"), pe32.pcPriClassBase );
    if( dwPriorityClass )
      _tprintf( TEXT("\n  Priority class    = %d"), dwPriorityClass );
    _tprintf( TEXT("\n"));

    //-------------------------------------------------------------------------------
    // database insertion of the process information    
    sqlite3_bind_int64(stmt,1,pe32.th32ProcessID);
    sqlite3_bind_int64(stmt,2,pe32.cntThreads);
    sqlite3_bind_int64(stmt,3,pe32.th32ParentProcessID);
    sqlite3_bind_int64(stmt,4,pe32.pcPriClassBase);
    sqlite3_bind_int64(stmt,5,dwPriorityClass); 
    if(sqlite3_step(stmt) != SQLITE_DONE)
        printf("\nPrepare statement failed!\n");
    sqlite3_reset(stmt);                        // the error appears here
    sqlite3_finalize(stmt);
    //-------------------------------------------------------------------------------

    // List the modules and threads associated with this process
    ListProcessModules( pe32.th32ProcessID,db );
    ListProcessThreads( pe32.th32ProcessID,db );

  } while( Process32Next( hProcessSnap, &pe32 ) );

  CloseHandle( hProcessSnap );
  return( TRUE );
}

コードを実行すると、次のように表示されます。

メッセージ

では、なぜこれが起こっているのか、このメモリ例外を回避する方法は? ありがとう

4

1 に答える 1