開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Eclipse+MinGW
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
sqlite
問題(Question):
請問C++在DLL內使用Sqlite,但產生出DLL後,再用C去CALL這個DLL會出有
No source available for "0x0" ,麻煩各位先進高手,謝謝~
餵入的資料(Input):
"20"
預期的正確結果(Expected Output):
add2(20):0.0036
錯誤結果(Wrong Output):
No source available for "0x0"
程式碼(Code):(請善用置底文網頁, 記得排版)
打包成DLL的指令
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o sqlite3.o sqlite3.c
g++ -DBUILD_DLL -O0 -g3 -Wall -c -fmessage-length=0 -o create_DLL.o create_DLL.cpp
g++ -shared -oDLL.dll create_DLL.o sqlite3.o
C++的DLL程式
/*
============================================================================
Name : create_DLL.cpp
Author : test
============================================================================
*/
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
int Callback_ShowList( void *context, int count, char **values, char ** columnName )
{
int i;
context = NULL;
for( i=0 ; i<count ; ++i )
printf( "\t\t%s = %s\n" , columnName[i] , values[i] ? values[i] : "NULL" );
printf( "\n" );
return SQLITE_OK;
}
__declspec( dllexport ) double add2(char *num)
{
int row, cols,rc=0;
sqlite3_stmt * stmt;
sqlite3 *db ;
char *errMsg = NULL;
/* 開啟 database 檔 */
sqlite3_initialize( );
rc = sqlite3_open("DLLDB.db", &db);
if ( rc != SQLITE_OK)
{
sqlite3_close( db );
return NULL;
}
const char *sql = "SELECT * FROM TABLE1 where column1=?";
sqlite3_prepare_v2(db, sql, strlen (sql) + 1, & stmt, NULL);
sqlite3_bind_text(stmt, 1, num, -1, SQLITE_TRANSIENT);
while (1) {
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW) {
int bytes;
const unsigned char * text;
bytes = sqlite3_column_bytes(stmt, 0);
text = sqlite3_column_text(stmt, 0);
double text2 = sqlite3_column_double(stmt, 1);
return text2;
}
else if (s == SQLITE_DONE) {
break;
}
else {
return NULL;
}
}
}
C去CALL這個DLL程式
/*
============================================================================
Name : call_DLL.c
Author : test
============================================================================
*/
#include<stdio.h>
#include<windows.h>
int main(){
HANDLE ldll;
char (*add2)(char);
ldll = LoadLibrary("DLL.dll");
if(ldll>(void*)HINSTANCE_ERROR){
add2 = GetProcAddress(ldll, "add2");
double x=add2("20");
printf("add2(20): %lf ", x);//, mul(4,5)
} else {
printf("ERROR.");
}
}
補充說明(Supplement):
如果直接在create_DLL.cpp另外加上main()去CALL add2()是正常的