C++动态库中调用sqlcipherbd.dll

C++代码

sqlite.h

extern "C" __declspec(dllexport)int  sqlite3_open(const char *zFilename, void **ppDb);
extern "C" __declspec(dllexport)int sqlite3_close(void *db);
extern "C" __declspec(dllexport)int sqlite3_key(void *db, const char *pKey, int nKey);
extern "C" __declspec(dllexport)int sqlite3_prepare_v2(void *db, const char *zSql, int nBytes, void **ppStmt, const char **pzTail);
extern "C" __declspec(dllexport)int sqlite3_step(void *pStmt);
extern "C" __declspec(dllexport)int sqlite3_finalize(void *pStmt);
extern "C" __declspec(dllexport)const char *sqlite3_errmsg(void *db);
extern "C" __declspec(dllexport)int sqlite3_column_count(void *pStmt);
extern "C" __declspec(dllexport)const char *sqlite3_column_name(void *pStmt, int N);
extern "C" __declspec(dllexport)int sqlite3_column_type(void *pStmt, int i);
extern "C" __declspec(dllexport)int sqlite3_column_int(void *pStmt, int i);
extern "C" __declspec(dllexport)char *sqlite3_column_text(void *pStmt, int i);
extern "C" __declspec(dllexport)double sqlite3_column_double(void *pStmt, int i);
extern "C" __declspec(dllexport)void *sqlite3_column_blob(void *pStmt, int i);
extern "C" __declspec(dllexport)int sqlite3_column_bytes(void *pStmt, int i);
extern "C" __declspec(dllexport)int sqlite3_bind_double(void *pStmt, int i, double rValue);
extern "C" __declspec(dllexport)int sqlite3_bind_int(void *p, int i, int iValue);
extern "C" __declspec(dllexport)int sqlite3_bind_text(void *pStmt, int i, const char *zData, int nData, void *xDel);
extern "C" __declspec(dllexport)int sqlite3_bind_blob(void *pStmt, int i, const void *zData, int nData, void *xDel);
extern "C" __declspec(dllexport) int  Add(int a, int b);

sqlite.cpp

#include<string.h> 
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <Windows.h>
#include "sqlite.h"



HINSTANCE handle;
const char *IKey = "[加密的密码]";

int  Add(int a, int b)
{

	return a + b;
}
int IndexOf(char *strSource, char *strFind)
{
	char *p = strSource;
	int i = 0;
	p = strstr(strSource, strFind);
	if (p == NULL)
		return -1;
	else {
		while (strSource != p)
		{
			strSource++;
			i++;
		}
	}
	return i;

}
char*subString(char *someString, int n)
{
	char *new1 = (char *)malloc(sizeof(char)*n + 1);
	strncpy(new1, someString, n);
	new1[n] = '\0';
	return new1;
}

char* joinstr(char *s1, char *s2)
{
	char *result = (char *)malloc(strlen(s1) + strlen(s2) + 1);
	if (result == NULL) exit(1);

	strcpy(result, s1);
	strcat(result, s2);

	return result;
}

//参数,src 字符串源,sub想要替换的字符串,dst,用来替换的字符串
char*replaceFirst(char*src, char*sub, char*dst)
{
	//记录当前指针位置
	int pos = 0;
	//记录偏移

	int offset = 0;
	//字符串长度
	int srcLen, subLen, dstLen;
	//返回内容

	char*pRet = NULL;

	//求得各字符串长度

	srcLen = strlen(src);
	subLen = strlen(sub);
	dstLen = strlen(dst);
	//申请替换后的字符串缓冲区。用dst替换sub,所以应该是srclen-sublen+dstlen,+1流出'\0'位置
	pRet = (char*)malloc(srcLen + dstLen - subLen + 1);//(外部是否该空间)if (NULL != pRet)
	{
		//strstr查找sub字符串出现的指针。该指针减去src地址。得到相对位置
		pos = strstr(src, sub) - src;
		//拷贝src字符串,从首地址开始,pos个字符。
		memcpy(pRet, src, pos);
		//增加偏移位置到pos
		offset += pos;
		//拷贝dst到返回内容中。
		memcpy(pRet + offset, dst, dstLen);
		//重新定位偏移
		offset += dstLen;
		//拷贝src中,sub字符串后面的字符串到pRet中
		memcpy(pRet + offset, src + pos + subLen, srcLen - pos - subLen);
		//重新定位偏移
		offset += srcLen - pos - subLen;
		//最后添加字符串结尾标记'\0'
		*(pRet + offset) = '\0';
	}
	//返回新构造的字符串
	return pRet;
}
/*
** Open a new database handle.
*/
int sqlite3_open(const char *zFilename, void **ppDb) {

	typedef int(*fun)(const char *zFilename1, void **ppDb1);
	int s = 0;
	handle = LoadLibrary("sqlcipherbd.dll");// dlopen("libsqlcipher_android.so", RTLD_NOW);
	if (NULL == handle)
		return -2;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_open");
	s = dllFunc(zFilename, ppDb);
	return s;

}

int sqlite3_close(void *db) {

	typedef int(*fun)(void *db1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_close");
	s = dllFunc(db);
	FreeLibrary(handle);
	return s;

}

int sqlite3_key(void *db, const char *pKey, int nKey) {

	int IKeyLen = 12;
	typedef int(*fun)(void *db1, const char *pKey1, int nKey1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_key");
	s = dllFunc(db, IKey, IKeyLen);
	return s;
}

int sqlite3_prepare_v2(void *db, const char *zSql, int nBytes, void **ppStmt, const char **pzTail) {

	typedef int(*fun)(void *db1, const char *zSql1, int nBytes1, void **ppStmt1, const char **pzTail1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_prepare_v2");
	char *find = "ATTACH ";
	int pos = IndexOf((char *)zSql, find);
	if (pos == 0) {
		find = " KEY ";
		pos = IndexOf((char *)zSql, find);
		char *temp = subString((char *)zSql, pos);
		char*newSql = joinstr(temp, find);
		char*newPwd = joinstr(find, " '");
		newPwd = joinstr(newPwd, (char *)IKey);
		newPwd = joinstr(newPwd, "'");
		newSql = replaceFirst(newSql, find, newPwd);
		s = dllFunc(db, newSql, strlen(newSql), ppStmt, pzTail);
	}
	else {
		s = dllFunc(db, zSql, nBytes, ppStmt, pzTail);
	}



	return s;
}

int sqlite3_step(void *pStmt) {
	typedef int(*fun)(void *pStmt1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_step");
	s = dllFunc(pStmt);

	return s;
}

int sqlite3_finalize(void *pStmt) {
	typedef int(*fun)(void *pStmt1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_finalize");
	s = dllFunc(pStmt);
	return s;
}

const char *sqlite3_errmsg(void *db) {
	typedef const char *(*fun)(void *db1);
	const char *s;
	if (NULL == handle)
		return (char*)(-1);
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_errmsg");
	s = dllFunc(db);
	return s;
}

int sqlite3_column_count(void *pStmt) {
	typedef int(*fun)(void *pStmt1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_count");
	s = dllFunc(pStmt);
	return s;
}

const char *sqlite3_column_name(void *pStmt, int N) {
	typedef const char *(*fun)(void *pStmt1, int N1);
	const char *s;
	if (NULL == handle)
		return "";
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_name");
	s = dllFunc(pStmt, N);
	return s;
}

int sqlite3_column_type(void *pStmt, int i) {
	typedef int(*fun)(void *pStmt1, int i1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_type");
	s = dllFunc(pStmt, i);
	return s;
}

int sqlite3_column_int(void *pStmt, int i) {
	typedef int(*fun)(void *pStmt1, int i1);
	int s = 0;
	if (NULL == handle)
		return -1;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_int");
	s = dllFunc(pStmt, i);
	return s;
}

char *sqlite3_column_text(void *pStmt, int i) {
	typedef char *(*fun)(void *pStmt1, int i1);
	char *s;
	if (NULL == handle)
		return "";
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_text");
	s = dllFunc(pStmt, i);
	return s;
}

double sqlite3_column_double(void *pStmt, int i) {
	typedef double(*fun)(void *pStmt1, int i1);
	double s;
	if (NULL == handle)
		return 0;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_double");
	s = dllFunc(pStmt, i);
	return s;
}

void *sqlite3_column_blob(void *pStmt, int i) {
	typedef void *(*fun)(void *pStmt1, int i1);
	void *s;
	if (NULL == handle)
		return 0;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_blob");
	s = dllFunc(pStmt, i);
	return s;
}

int sqlite3_column_bytes(void *pStmt, int i) {
	typedef int(*fun)(void *pStmt1, int i1);
	int s;
	if (NULL == handle)
		return 0;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_column_bytes");
	s = dllFunc(pStmt, i);
	return s;
}

int sqlite3_bind_double(void *pStmt, int i, double rValue) {
	typedef int(*fun)(void *pStmt1, int i1, double rValue1);
	int s;
	if (NULL == handle)
		return 0;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_bind_double");
	s = dllFunc(pStmt, i, rValue);
	return s;
}

int sqlite3_bind_int(void *p, int i, int iValue) {
	typedef int(*fun)(void *pStmt1, int i1, int rValue1);
	int s;
	if (NULL == handle)
		return 0;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_bind_int");
	s = dllFunc(p, i, iValue);
	return s;
}

int sqlite3_bind_text(void *pStmt, int i, const char *zData, int nData, void *xDel) {
	typedef int(*fun)(void *pStmt1, int i1, const char *zData1, int nData1, void *xDel1);
	int s;
	if (NULL == handle)
		return 0;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_bind_text");
	s = dllFunc(pStmt, i, zData, nData, xDel);
	return s;
}

int sqlite3_bind_blob(void *pStmt, int i, const void *zData, int nData, void *xDel) {
	typedef int(*fun)(void *pStmt1, int i1, const char *zData1, int nData1, void *xDel1);
	int s;
	if (NULL == handle)
		return 0;
	fun dllFunc = (fun)GetProcAddress(handle, "sqlite3_bind_blob");
	s = dllFunc(pStmt, i, (char *)zData, nData, xDel);
	return s;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lixiaozhong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值