??xml version="1.0" encoding="utf-8" standalone="yes"?>蜜桃臀av在线,精品婷婷伊人一区三区三,99热这里只有精品首页http://www.aygfsteel.com/huyi2006/category/27011.html 做一个有思想的hzh-cnSun, 11 May 2008 23:30:22 GMTSun, 11 May 2008 23:30:22 GMT60在VC6.0中用sqlite数据?/title><link>http://www.aygfsteel.com/huyi2006/articles/199720.html</link><dc:creator>胡意</dc:creator><author>胡意</author><pubDate>Sat, 10 May 2008 08:21:00 GMT</pubDate><guid>http://www.aygfsteel.com/huyi2006/articles/199720.html</guid><wfw:comment>http://www.aygfsteel.com/huyi2006/comments/199720.html</wfw:comment><comments>http://www.aygfsteel.com/huyi2006/articles/199720.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huyi2006/comments/commentRss/199720.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huyi2006/services/trackbacks/199720.html</trackback:ping><description><![CDATA[ <p>在sqlite.org 上下载得到Windows版本的sqlite,它是以sqlitedll.zip文g提供?其中有sqlite3.def?sqlite3.dll文g,当然可以直接通过LoadLibrary{WIN32API来操作dll,查找其中包含的函?q用这些函?但是一般都 不这么做,原因很简?q样太麻?所以一般先使用LIB命o生成用于链接的lib,然后把sqlite头文件sqlite3.h包含q程序中,<br />q样直接调用 sqlite的API方便多?当然sqlite3.h文g得从sqlite源代码(以sqlite-source-3_3_4.zip文g提供Q中搞到,在源码中sqlite3.hq个头文件是sqlite3.h.in存在的,解压出来Ҏsqlite.h可以了. <br />使用VC++的LIB命o有以下步骤: <br />Q?Q设|VC98中LIB.exe所在的路径Q?<br />D:\MyDoc\db\capi>set path=%path%;"D:\Program Files\Microsoft Visual Studio\VC98\Bin" <br />(2)生成SQLite的lib文gQ?<br />D:\MyDoc\db\capi>LIB /DEF:SQLITE3.DEF /MACHINE:IX86 </p> <p>Microsoft (R) Library Manager Version 6.00.8168 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. Creating library SQLITE.lib and object SQLITE.exp <br />  q样成功地创徏了在WIN32E序中访问sqlite所需要的?可以用于链接WIN32E序. <br />    到此所有用sqlite的准备工作已告罄.现在在MSVC6中新Z个Win32 Console Application工程,把sqlite.dll,sqlite.h和sqlite.lib文g复制到工E文件夹?把sqlite.h文g加入到项 目中,然后在Project Setting的Link中的对象库模块中增加sqlite.lib文g. 或者project->add to project->filesQ选择q个lib文g<br />然后修改加入如下代码卛_: </p> <p>#include <IOSTREAM><br />#include <STRING><br />#include <SSTREAM><br />#include <stdio.h><br />#include "sqlite3.h"</p> <p>using namespace std;</p> <p>sqlite3* pDB;</p> <p>int createTable()<br />{<br />  char* errMsg;<br />  std::string dropTab="drop table test_tab;";<br />  string strSQL= "create table test_tab (f1 int, f2 long, f3 varchar(20));";<br />  int res = sqlite3_exec(pDB,dropTab.c_str(),0,0, &errMsg);<br />  if (res != SQLITE_OK)<br />  {<br />   std::cout << "执行SQL 出错." << errMsg << std::endl;<br />   //return -1;<br />  } </p> <p>  res = sqlite3_exec(pDB,strSQL.c_str(),0,0, &errMsg);</p> <p>  if (res != SQLITE_OK)<br />  {<br />    std::cout << "执行创徏table的SQL 出错." << errMsg << std::endl;<br />    return -1;<br />  }<br />  else<br />  {<br />      std::cout << "创徏table的SQL成功执行."<< std::endl;<br />  }</p> <p>  return 0;<br />}</p> <p>int insert1()<br />{<br />  char* errMsg;</p> <p>  int res = sqlite3_exec(pDB,"begin transaction;",0,0, &errMsg);</p> <p>  for (int i= 1; i < 100; ++i)<br />  {<br />    std::stringstream strsql;<br />    strsql << "insert into test_tab  values(";<br />    strsql  << i << ","<< (i+10) <<",'huyi'"<< ");";<br />    std::string str = strsql.str();<br />    cout << str <<endl;<br />    res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);<br />    if (res != SQLITE_OK)<br />    {<br />      std::cout << "执行SQL 出错." << errMsg << std::endl;<br />      return -1;<br />    }<br />  }<br />  res = sqlite3_exec(pDB,"commit transaction;",0,0, &errMsg);<br />  std::cout << "SQL成功执行."<< std::endl;<br />  return 0; <br />}</p> <p> <br />int select1()<br />{<br />  char* errMsg;<br /> int nrow = 0, ncolumn = 0;<br /> char **azResult; //二维数组存放l果<br />  string strSQL= "select * from test_tab;";<br />     /*<br />  int res = sqlite3_exec(pDB,strSQL.c_str(),0,0, &errMsg);</p> <p>  if (res != SQLITE_OK)<br />  {<br />    std::cout << "执行SQL 出错." << errMsg << std::endl;<br />    return -1;<br />  }<br />  else<br />  {<br />       std::cout << "SQL成功执行."<< std::endl;<br />  }<br />  */<br />  sqlite3_get_table(pDB, strSQL.c_str(), &azResult, &nrow, &ncolumn, &errMsg);<br />     int i = 0;<br />  for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )<br />  {<br />   if (i>0 && i%ncolumn==0)<br />    printf("\n");<br />      printf( "%s ",azResult[i]);<br />  }<br />         printf("\n");<br />  //释放?azResult 的内存空?br />  sqlite3_free_table( azResult );<br />  sqlite3_close(pDB); //关闭数据?br />  return 0;<br />}</p> <p>int main(int argc, char* argv[])<br />{<br />  if (argc < 2)<br />  {<br />    std::cout << "误入命令行参数Qsqlite数据库名." << std::endl;<br />    return 0;<br />  }</p> <p>  int res = sqlite3_open(argv[1], &pDB);</p> <p>  if( res ){<br />    std::cout << "Can't open database: "<< sqlite3_errmsg(pDB);<br />    sqlite3_close(pDB);<br />    return -1;<br />  }<br />  res = createTable();<br />  if (res != 0)<br />  {<br />      return 0;<br />  }<br />  res = insert1();<br />  if (res != 0)<br />  {<br />      return 0;<br />  }<br />  select1();<br />   return 0;<br />}<br /></p> <img src ="http://www.aygfsteel.com/huyi2006/aggbug/199720.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huyi2006/" target="_blank">胡意</a> 2008-05-10 16:21 <a href="http://www.aygfsteel.com/huyi2006/articles/199720.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>dll基础http://www.aygfsteel.com/huyi2006/articles/180170.html胡意胡意Sat, 16 Feb 2008 04:06:00 GMThttp://www.aygfsteel.com/huyi2006/articles/180170.htmlhttp://www.aygfsteel.com/huyi2006/comments/180170.htmlhttp://www.aygfsteel.com/huyi2006/articles/180170.html#Feedback0http://www.aygfsteel.com/huyi2006/comments/commentRss/180170.htmlhttp://www.aygfsteel.com/huyi2006/services/trackbacks/180170.html自从微Y推出W一个版本的Windows操作pȝ以来Q动态链接库QDLLQ一直是Wind
ows操作pȝ的基?
动态链接库通常都不能直接运行,也不能接收消息。它们是一些独立的文gQ其?
包含能被可执行程序或其它DLL调用来完成某工作的函数。只有在其它模块调用?
态链接库中的函数Ӟ它才发挥作用?
Windows API中的所有函数都包含在DLL中。其中有3个最重要的DLLQKernel32.dll
Q它包含用于理内存、进E和U程的各个函敎ͼUser32.dllQ它包含用于执行?
L面Q务(如窗口的创徏和消息的传送)的各个函敎ͼGDI32.dllQ它包含用于?
囑֒昄文本的各个函数?
           静态库和动态库
静态库Q函数和数据被编译进一个二q制文g(通常扩展名ؓ.LIB)。在使用静态库
的情况下Q在~译链接可执行文件时Q链接器从库中复制这些函数和数据q把它们
和应用程序的其它模块l合h创徏最l的可执行文?.EXE文g)?
在用动态库的时候,往往提供两个文gQ一个引入库和一个DLL。引入库包含被D
LL导出的函数和变量的符号名QDLL包含实际的函数和数据。在~译链接可执行文?
Ӟ只需要链接引入库QDLL中的函数代码和数据ƈ不复制到可执行文件中Q在q行
的时候,再去加蝲DLLQ访问DLL中导出的函数?
使用动态链接库的好?
可以采用多种~程语言来编写?
增强产品的功能?
提供二次开发的q_?
化项目管理?
可以节省盘I间和内存?
有助于资源的׃n?
有助于实现应用程序的本地化?
动态链接库被多个进E访?
动态链接库加蝲的两U方?
预备知识
Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:
Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:
Q:Q:
q程的虚拟地址I间Q?
 ?2为系l中Q系lؓ没个q程分配2^32的地址I间
具体可参看《windows核心~程?
预处理命令:
没有什么可以说的了
看看代码明白了Q!
环境变量Q(环境变量的概忉|׃介绍了,具体的可以参看windows 核心~程Q?
上面有很详细的说明)把DLL攑ֈ当前L的环境变量中可以加?
定义函数指针
格式Qtypedef  int (*proc)(int a,int b);
注意Qproc是一个函数指针类型而不是一个变?
然后我们可以用这个指针类型去定义变量
Proc myproc;q里的myproc是一个指针变?
要是实在无聊 内心I 没事可做的话 可以去这个网站看看函数的指针http://uf
o.tyedu.com/study/programmer/language_C/200412/1472.html  
函数的调用约定(可以不必了解Q但是理解后可以让你理解DLL的调用更为深刻)
函数的调用约定:函数调用U定是函数调用者和被调用的函数体之间关于参C?
、返回g递、堆栈清除、寄存器使用的一U约?
   它是需要二q制U别兼容的强U定,函数调用者和函数体如果用不同的调用U?
?可能造成E序执行错误,必须把它看作是函数声明的一部分;
常见的函数调用约定:
VC6中的函数调用U定Q?
       调用U定        堆栈清除    参数传?
       __cdecl         调用?     从右到左,通过堆栈传?
       __stdcall       函数?     从右到左,通过堆栈传?
       __fastcall      函数?     从右到左,优先使用寄存?ECX,EDX),然后
使用堆栈
       thiscall        函数?     this指针默认通过ECX传?其它参数从右
到左入栈
__cdecl是C\C++的默认调用约? VC的调用约定中q没有thiscallq个关键?它是
cL员函数默认调用约?
C\C++中的main(或wmain)函数的调用约定必L__cdecl,不允许更?
默认调用U定一般能够通过~译器设|进行更?如果你的代码依赖于调用约??
明确指出需要用的调用U定;
常见的函数调用约定中,只有cdeclU定需要调用者来清除堆栈;
C\C++中的函数支持参数数目不定的参数列?比如printf函数;׃函数体不知道?
用者在堆栈中压入了多少参数,
所以函C不能方便的知道应该怎样清除堆栈,那么最好的办法是把清除堆栈的?
Ml调用?
q应该就是cdecl调用U定存在的原因吧;
VB一般用的是stdcall调用U定;(psQ有更强的保证吗)
Windows的API?一般用的是stdcallU定;(ps: 有更强的保证?
在不同语a间的调用?如DLL)最好采用stdcall调用U定,因ؓ它在语言间兼?
性支持最?
?函数q回g递方?
  其实Q返回值的传递从处理上也可以惌为函数调用的一个out形参敎ͼ 函数q?
回g递方式也是函数调用约定的一部分Q?
  有返回值的函数q回Ӟ一般int、指针等32bit数据?包括32bitl构)通过ea
x传递,(bool,char通过al传递,short通过 ax传?,特别的__int64{?4bitl构(
struct) 通过edx,eax两个寄存器来传?同理Q?2bit整Ş?6bit环境中通过dx,a
x传?; 其他大小的结?struct)q回时把其地址通过eaxq回Q?所以返回值类?
不是1,2,4,8byteӞ效率可能比较?
  参数和返回g递中Q引用方式的cd可以看作与传递指针方式相同;
  float\double(包括Delphi中的extended)都是通过点寄存器st(0)q回Q?

具体的分析参看:http://blog.csdn.net/avalonbbs/archive/2004/12/25/229300
.aspx
Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:
Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:Q:
Q:Q:Q:Q?




隐式链接
本文现在寚w式链接不作具体的说明Q只是做一个大概的介绍Q下ơ再做具体的?
明).当进E运行的时候,所有的相关的DLL都被加蝲到内存,然后映射到进E的?
址I间Q当一个进E要调用很多个DLL的时候,q种Ҏ显得特别浪费内存,所?
在加载很多个DLL的时候,最好用昄加蝲的方?
在调用DLL里面的函数时?
要用extern 声明是外部变?
比如 extern int add(int num1,int num2);
但是q应该注意的是Q在~译DLLӞ要把~译的LIB文g攑ֈ执行文g的目录下
Qƈ且在~译执行文g的时候要q接LIB文g?
在写DLL的时候要导出的函C是在能被外部程序调用的函数前面加上
一般大型项目在开发DLL中,要进行预定义声明?

Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=
Q=
在定义DLL的时候要定义导出函数p在该函数前面加__declspec(DLLexport)Ӟ
C++~译器ؓ了支持函数的重蝲会进行函数名字改~,当可执行模块执行该函数时?
于找不到该函数的名字Q于是调用就会出现错误!当用extern “C”时可以告
诉编译器不必修改函数名和变量名。这样就可以用C++或C~译器程序调用该函数?

以上操作只有在VC++创徏的的可执行模块来调用该DLLQ如果用其他的~译器的?
块来调用该DLLQ就需要执行一些额外的操作?
C~译在进行编译的时候也会进行名字的改编Q当函数使用_stdcall(WINAPI)调用
规则ӞMS~译器就会改~函数的名称?
比如Q__declspec(DLLexport)  LONG __stdcall  Proc(int a ,int b);
~译器会改编为__Proc@8
因此 当非C++或非C~译器调用该DLL中的函数ProcӞ׃出现找不到函数的情况
?
q样我们可以定义DEF文g来解冻Iq且在DEF文g加上下面的EXPORTSQ?
EXPORTS
       Proc
Def模块执行原理Q当q接E序分析q个DEF文gӞ它发现Proc和__Proc@8都被?
出,׃q两个函数是怺匚w的,因此q接E序使用Proc来输函数Q根本不
使用__Proc@8来输出函数名
Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=Q=
Q=Q?

                




下面是def的具体用方?

------------------------------------------------------------------------
----------------------------------------------模块定义文g(.DEF)是一个或
多个用于描述DLL属性的模块语句l成的文本文Ӟ每个DEF文g臛_必须包含以下
模块定义语句Q?
* W一个语句必LLIBRARY语句Q指出DLL的名字;
* EXPORTS语句列出被导出函数的名字Q将要输出的函数修饰名罗列在EXPORTS之下
Q这
个名字必M定义函数的名字完全一_如此得C个没有Q何修饰的函数名了
?
* 可以使用DESCRIPTION语句描述DLL的用?此句可?Q?
* ";"对一行进行注?可??
------------------------------------------------------------------------
----------------------------------------------


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
dlltest.h
#ifdef DLL1_API
#else
#define DLL1_API extern "C" _declspec(dllimport)
#endif

DLL1_API int _stdcall add(int a,int b);
DLL1_API int _stdcall subtract(int a,int b);
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////


\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\//
dlltest.cpp
#define DLL1_API extern "C" _declspec(dllexport)
#include "Dll1.h"
#include <stdio.h>
int _stdcall add(int a,int b)
{
    return a+b;
}

int _stdcall subtract(int a,int b)
{
    return a-b;
}
\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// def文g
LIBRARY dlltest
EXPORTS
add
subtract
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

有了上面的那些文件之后就可以在如何地方调用这些函C

void CDllTestDlg::OnBtnSubtract()
{
    // TODO: Add your control notification handler code here
    CString str;
    str.Format("5-3=%d",subtract(5,3));
    MessageBox(str);
}

void CDllTestDlg::OnBtnOutput()
{
    // TODO: Add your control notification handler code here
    Point pt;
    pt.output(5,3);
}下面具体介绍昄加蝲
昄加蝲
VC++~译器在~译DLL的时候函C发生名字改编Q主要在非C++环境中就不能识别
该函CQ所以这里应该定义模块文件类型DEFQ主要就方便了非C++E序可以调用
该DLL里面的函?
再用显C加载前必须要注意的是名字的改编问题Q因为再动态加载中名字改编?
在加载就得不原来的函数名字了Q这样加载就会失败。但是可以用另外一U方法加
载:MSDN上对GetProAddress中的W二个参数是q样说明的Pointer to a null-ter
minated string that specifies the function or variable name, or the func
tion's ordinal value.也就是说可以使用函数的序h调用该函敎ͼ具体使用Ҏ
是ProcAdd = (MYPROC) GetProcAddress(hinstLib, MakeIntResource(i)); (i代表
函数在DLL中的序号Q可以用DUMPBIN工具查看)Q但是一般的都不用这U{换序L
方式来取得函数的地址Q因样非常的不直观!下面q模块定义文gQDEFQ来
避免DLL中函数的名字的改~问?





昄加蝲DLL
//MSDN上的对DLLq程昄加蝲的DEMO
Using Run-Time Dynamic Linking
You can use the same DLL in both load-time and run-time dynamic linking.
The following example uses the LoadLibrary function to get a handle to
the Myputs DLL (see Creating a Simple Dynamic-Link Library). If LoadLibr
ary succeeds, the program uses the returned handle in the GetProcAddress
function to get the address of the DLL's myPuts function. After calling
the DLL function, the program calls the FreeLibrary function to unload
the DLL.
Because the program uses run-time dynamic linking, it is not necessary t
o link the module with an import library for the DLL.
This example illustrates an important difference between run-time and lo
ad-time dynamic linking. If the DLL is not available, the application us
ing load-time dynamic linking must simply terminate. The run-time dynami
c linking example, however, can respond to the error.
// A simple program that uses LoadLibrary and
// GetProcAddress to access myPuts from Myputs.dll.

#include <stdio.h>
#include <windows.h>
typedef int (*MYPROC)(LPTSTR);
VOID main(VOID)
{
   HINSTANCE hinstLib;
   MYPROC ProcAdd;
   BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
 // Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("DllTest"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
   {
       ProcAdd = (MYPROC) GetProcAddress(hinstLib, TEXT("Proc"));
// If the function address is valid, call the function.
 if (NULL != ProcAdd)
       {
           fRunTimeLinkSuccess = TRUE;
           (ProcAdd) (TEXT("Message via DLL function\n"));
       }
 // Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
   }
 // If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
      printf("Message via alternative method\n");
}

对以上的几个函数作一些必要的说明Q?
LoadLibraryQ加载指定的DLLQ加载方式是先在当前目录中查找,如果找不到再?
环境变量目录下查找;
q是看MSDN上的说明
The LoadLibrary function maps the specified executable module into the a
ddress space of the calling process.
HMODULE LoadLibrary(
 LPCTSTR lpFileName
);
Parameters
lpFileName
[in] Pointer to a null-terminated string that names the executable modul
e (either a .dll or .exe file). The name specified is the file name of t
he module and is not related to the name stored in the library module it
self, as specified by the LIBRARY keyword in the module-definition (.def
) file.

GetProcAddressQ是取得已知的DLL中的函数Q返回指定函数的地址
MSDN上的说明
This function returns the address of the specified exported DLL function
.
FARPROC GetProcAddress(
 HMODULE hModule,
 LPCWSTR lpProcName
);
Parameters
hModule
[in] Handle to the DLL module that contains the function.
The LoadLibrary or GetModuleHandle function returns this handle.
lpProcName
[out] Pointer to a null-terminated string containing the function name,
or specifies the function's ordinal value.
If this parameter is an ordinal value, it must be in the low-order word;
the high-order word must be zero.
The lpProcName parameter must be in Unicode.
Remark:
The GetProcAddress function is used to retrieve addresses of exported fu
nctions in DLLs.
The spelling and case of the function name pointed to by lpProcName must
be identical to that in the EXPORTS statement of the source DLL's modul
e-definition (.def) file.
The exported names of Win32 APIs might differ from the names you use whe
n calling these functions in your code. This difference is hidden by mac
ros used in the SDK header files.

胡意 2008-02-16 12:06 发表评论
]]>
删除windows服务http://www.aygfsteel.com/huyi2006/articles/168603.html胡意胡意Tue, 18 Dec 2007 14:45:00 GMThttp://www.aygfsteel.com/huyi2006/articles/168603.htmlhttp://www.aygfsteel.com/huyi2006/comments/168603.htmlhttp://www.aygfsteel.com/huyi2006/articles/168603.html#Feedback0http://www.aygfsteel.com/huyi2006/comments/commentRss/168603.htmlhttp://www.aygfsteel.com/huyi2006/services/trackbacks/168603.html

删除的办法有两个Q?/p>

办法一Q?用sc.exeq个Windows命o
         开始——运行——cmd.exeQ然后输入sc可以看C。用办法很单:
         sc delete "服务?   (如果 ?屑溆锌崭?托枰 昂蠹??br />          如针对上面的Q?sc delete KSD2Service

Ҏ二:直接q行注册表编辑(不推荐)
       打开注册表编辑器Q找C面的键|
       HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services   一般服务会以相同的名字在这里显CZ个主健,直接删除相关的键g可?/p>

Ҏ情况

      1、如果服务显C的是rundll32.exeQƈ且这个文件是位于system32目录下,那么׃能删除这个rundll32.exe文gQ它是Windowspȝ的文件。这时只要清除相关的服务可以了

       2、如果一个服务删除了马上又自动徏立了Q说明后台有q程在监视、保护。需要先在进E管理器中杀掉相应的q程Q或者启动后按F8Q到安全模式下删?/p>



胡意 2007-12-18 22:45 发表评论
]]>
վ֩ģ壺 | ɳ| | Ϸ| | ԭ| | | ߰| ³ľ| ɽ| ͷ| | | ÷| ƽ| Ϲ| | | | | | | ͭ| ͸| ʡ| ͩ| ̨| | | ɽ| | ˹| | ɽ| | | | »| | ɽ|