VC study (2005-01)

xiaoxiao2021-03-06  37

l Define your own API

#ifdef transclt_exports

#define CLT_API EXTERN "C" __DECLSPEC (DLLEXPORT)

#ELSE

#define CLT_API EXTERN "C" __DECLSPEC (DLLIMPORT)

#ENDIF

Typedef void (* reccCallback) (PCMD_HEAD PCMD, UINT UHANDLE, PIP_ADDR PIP, DWORD DWPARAM = 0);

Typedef void (* conncallback) (uint uHandle, Pip_addr PIP, DWORD DWPARAM = 0);

/ * Interface of transclt.dll * /

Class ItransCLT

{

PUBLIC:

Virtual Bool Initialize (DWORD DWSVRIP,

Word WSVRPORT,

DWORD DWLOCALIP = 0,

Word WlocalPort = 0) = 0;

Virtual void release () = 0;

Virtual Void RegonReceive (Recvcallback Onreceive, DWORD DWPARAM = 0) = 0;

Virtual Void Regonnect (CONNCALLBACK ONCONNECT, DWORD DWPARAM = 0) = 0;

Virtual Void Regontisconnect (Conncallback OnDisconnect, DWORD DWPARAM = 0) = 0;

Virtual Bool SendtoServer (LPVOID PBUFFER, UINT USIZE) = 0;

}

/ * API Interface of TransClt.dll * /

CLT_API LPVOID TRANSCLTCREATEOBJECT ();

CLT_API DWORD TRANSCLTGETHOSTIP (INT NIPIndex);

#ENDIF / / _YZ_TCPTRANS_CLIENT_MODULE_INTERFACE_INCLUDE_

l calls the DLL with parameters

Typedef void (WinAPI * TestDLL) (CSTRING)

Hinstance hmod;

HMOD = :: LoadLibrary ("Ecpmail.dll");

AFXSetResourceHandle (HMOD);

IF (hmod == null)

{

AfxMessageBox ("fail");

}

TestDLL LPPROC;

LPPROC = (TESTDLL) GetProcaddress (HMOD, "Show");

IF (LPPROC! = (TestDLL) NULL)

{CSTRING STR = "SSS";

(* LPPROC) (STR);

}

Freelibrary (HMOD);

}

L DLL application

M_PTRANSCLT = (iTransClt *) TransclTcreateObject ();

IF (null == m_ptransclt)

Return False;

IF (false == m_ptransclt-> initialize (INET_ADDR ("192.168.0.137"), 45876))

{

m_ptransclt-> release ();

M_PTRANSCLT = NULL; RETURN FALSE;

}

Return True;

l Modeless Dialog

For a modeless dialog box, you must provide your own public constructor in your dialog class. To create a modeless dialog box, call your public constructor and then call the dialog object's Create member function to load the dialog resource. You can call Create either during ORAFTER THE CONSTRUCTOR CALL. IF The Dialog Resource Has The Property WS_Visible, The Dialog Box Appears IMMEDITELY. IF NOT, You Must Call ITS ShowWindow Member Function.

l About DLL

Typedef lpvoid (WinAPI * TestDLL) ();

Hinstance hmod;

HMOD = :: LoadLibrary ("Ecpmail.dll");

AFXSetResourceHandle (HMOD); // is very important

IF (hmod == null)

{

AfxMessageBox ("fail");

}

TestDLL CREATMAILOBJECT;

CreatMailObject = (TestDLL) GetProcaddress (HMOD, "ECPMAilCreateObject");

IF (CreatMailObject! = (TestDLL) NULL)

{

IECPMAIL * m_pecpmail = null;

M_pecpmail = (IECPMAIL *) CreatmailObject ();

M_pecpmail-> initialize (m_dwsvrip, 45877, m_dwbindip, 45876, afxgetmainwnd ());

/ *

IF (false == m_pecpmail-> initialize (m_dwsvrip, 45877, m_dwbindip, 45876))

{

m_pecpmail-> release ();

m_pecpmail = NULL;

}

}

Else AfxMessageBox ("Load Function Faild");

Freelibrary (HMOD);

l Set the timeout response of an event

IF (wait_timeout == WaitForsingleObject (g_eventquerydnused, timeout_wait_svr))

{

Dwerror = err_wait_svr_timeout;

}

Else

{

Dwerror = g_dwretquuerydnused;

g_dwretquerydnused = 0;

g_eventquerydnused.resetevent ();

}

l How to call a dynamic function of your class in a static function

first step:

Static void OnRecEive (PCMD_HEAD PCMD, UINT UHANDLE, PIP_ADDR PIP, DWORD DWPARAM);

DWORD DWPARAM ------- Pointer to the class

Void MailManage :: OnRecEive (PCMD_HEAD PCMD, UINT UHANDLE, PIP_ADDR PIP, DWORD DWPARAM) {

IF (0! = isbadwriteptr (PCMD-> PBUF, PCMD-> NBUFSIZE)))

Return;

// cwnd * pwnd = afxgetmainwnd ();

MailManage * pManage = (Mailmanage *) dwparam;

Switch (PCMD-> DWCMD)

{

Case cmd_mail_update_ret:

SetEvent (pManage-> g_heventupdatemaillist);

Break;

DEFAULT:

Break;

}

}

WriteDebuginfo (DBG_THREAD, "/ N DistributeRecvdata Begin Thread Count =% D Max_thread =% D / N", PTHIS-> M_DWDistribThreadCount, pthis-> m_dwwtlethread;

CString strDebug = "";

StrDebug.format ("/ N DistributeRecvdata () Begin! DWTHREADCOUNT =% D ThreadId = 0x% x", pthis-> m_dwdistribethreadcount, getCurrentThreadId ());

Outputdebugstring (strDebug);

SetParentWnd (CWND * PParent)

AFXGETMAINWND ()

Database programming - DAO

CHAR SQL [100];

Sprintf (SQL, "DeviceId = '% s'", m_deviceid);

Rs.FindFirst ((LPCTSTR) SQL);

Some experiences about DAO database programming

Author: Guangdong Nanhai Zhao Technology Co., Ltd. king_koo

Foreword

This article is an experience that accumulates in DAO database programming, and I hope to help with friends developed using DAO.

First, how to join the database support in a program that does not choose database support when new construction

Take the dialog box DAO-Access as an example:

1.1 Using the Class Wizard New Class .Name: "MyDB", Base Class: "DaorecordSet", select the correct data source and table.

Note: VC6 cannot directly support Access2000 directly, and if you want to use it first to 97 version.

1.2 Add #include "afxdao.h" in the MyDB header. Add #include "mydb.h" on the dialog class header.

1.3 Test: Add a button in the dialog box, add the following code within its response function:

MYDB DB;

db.open ();

MessageBox (db.m_answera);

db.close ();

Here, I have an Answera field in my database table.

Second, how to manually add static binding in the database without static binding other controls.

Still taking above as an example. First delete the previous buttons and their message processing functions. Add an Edit control.

2.1 Declaration: Add: MyDB * Rec; as follows:

// {{AFX_DATA (CAAADLG)

ENUM {IDD = IDD_AAA_DIALOG};

MYDB * REC;

// Note: The Classwizard Will Add Data MEMBERS Here

//}} AFX_DATA

2.2 Binding: Add member variables m_amswera for the class wizard to the EDIT control. (Select) in the wizard)

2.3 Initialization: Join within the dialog configuration function

Rec = new mydb;

REC-> open (); 2.4 Destruction: Response dialog WM_CLOSE message, join message processing code:

Rec-> close ();

DELETE REC;

2.5 Test: Compile operation, you can see the content of the database in the Edit box. The only unique is not automatically updated. Need manual updatedata (0);

Third, how to ensure that the above procedures can still be running. (Database is also copying the same directory)

Change the contents of the MyDB class getDefaultdbname () function to:

Char Str [255];

GetCurrentDirectory (255, STR);

STRCAT (STR, "// my.mdb");

Return_T (STR);

postscript

Although DAO gradually replaced by ADO, there are still many netizens to learn the database programming under VC through DAO, and the above is the experience of their own exploration, I hope to help beginners. If you have anything, please ask your prawn to correct!

10.8 DAO

10.8.1 What is DAO

DAO (Database Access Object) uses the Microsoft Jet Database Engine to access the database. Microsoft Jet provides a data engine with products such as Access and Visual Basic.

Like ODBC, DAO provides a set of API for programming. The MFC also provides a set of DAO classes that encapsulate the underlying API, which greatly simplifies the development of procedures. With the MFC's DAO class, users can write applications independent of DBMS.

DAO is introduced from Visual C 4.0. Generally speaking, the DAO class provides a broader support than ODBC classes. On the one hand, as long as there is an ODBC driver, use the Microsoft Jet DAO to access the ODBC data source. On the other hand, since the DAO is based on the Microsoft Jet engine, it has good performance when accessing the Access database (ie * .mdb file).

10.8.2 Similarity of DAO and ODBC

The DAO class has a lot of similarities compared to the ODBC class, which mainly has the following:

Both support access to various ODBC data sources. Although the data engine used by the two is different, it can meet the requirements of users who write independent of DBMS applications.

DAO provides an MFC class similar to ODBC. For example, DAO's CDAODatabase class corresponds to the CDATABASE class of ODBC, CDAORECORDSET corresponds to CRecordView, CDAOException, corresponding CDBEXCeption. These corresponding class functions are similar, and most of their member functions are the same.

AppWizard and ClassWizard provide similar support to applications that use DAO and ODBC objects.

Due to many aspects of DAO and ODBC classes, it is easy to learn to use DAO as long as the user has mastered ODBC. In fact, users can easily transplant the database application from ODBC to DAO.

Visual C provides an example called DaoEnrol, which is actually a DAO version of Enroll. The reader can open the DAOENROL project to see, and its source code is extremely similar to Enroll. The reader can establish DAOENROL according to the steps to establish Enroll, only several places have a difference, which mainly has the following points:

The selected data source is different. When you create DaOenrol with AppWizard, and when you create a CDAORECORDSET class with ClassWizard, you should choose DAO instead of ODBC in the Database Options dialog. Moreover, DAO's data source is specified by selecting a .mdb file, ie, click "..." button, select the .mdb file you want to access in the File dialog box.

The default type of record set is different. The default type of the ODBC record is a snapshot, while DAO is a dynamic set (DYNASET). Parameterized approach. The parameter in the m_strfilter and m_strsort of the DAO record is not "?", But a meaningful parameter name. For example, there is a parameter called CourseIdParam in the filter below.

m_pset-> m_strfilter = "courseid = courseidparam";

In the DofieldExchange function, there are two lines below:

PFX-> setfieldType (CDAOFIELDEXCHANGE :: Param);

DFX_Text (PFX, _T ("CourseidParam"), M_STRCOURSEIDPARAM;

The second parameter of the DFX function is also CourseIdparam.

Different ways to deal with exceptions. For example, when deleting records, the processing of exceptions is as follows:

Try

{

m_pset-> delete ();

}

Catch (CDAOEXCEPTION * E)

{

AfxMessageBox (e->

M_PerrorInfo-> m_strdescription);

E-> delete ();

}

In addition to the above differences, AppWizard and ClassWizard have also hidden some subtle differences. For example, the DAO recordset is used as a DAO Record Field Exchange instead of RFX, which is used in DOFIELDEXCHANGE in the DAO record. DFX functions are not RFX functions.

10.8.3 DAO features

DAO can access the ODBC data source via the ODBC driver. But DAO is based on Microsoft Jet Engine, through the engine, DAO can directly access databases such as Access, FoxPro, DBASE, Paradox, Excel, and Lotus WK. The CDAODatabase class can be connected directly to these databases without having to register DSNs in the ODBC Manager. For example, the following code is used to open a FoxPro database:

Cdaodatabase daodb;

DAODB.Open ("", False, False, "FoxPro 2.5; Database = C: // Zyf");

CDAODATABASE :: Open function is used to connect a database, the declaration of the function is:

Virtual Void Open (LPCTSTR LPSZNAME, BOOL BEXCLUSIVE = FALSE, BOOL BREADOONLY = FALSE, LPCTSTR LPSZCONNECT = _T ("));

Throw (CDAOException, CMemoryException);

Parameters BexClusive If true, the function opens the database in exclusive way, otherwise you will use the sharing method. If BREADOONLY is TRUE, then open the database in a read-only mode. If you want to open an Access database, you can specify the MDB file name in the lpszName parameter. If you want to access a non-Access database, you should make this parameter "", and a connection string is explained in LPSZConnect. The form of the connection string is generally "database type; Database = path (file)", such as "DBASE III; Database = C: // mydir"

Open functions can also open an ODBC data source, but this requires the corresponding ODBC driver and needs to register DSN in the ODBC Manager. At this time, LPSZConnect is "ODBC; DSN = MyDataSource". Obviously, use DAO to access a database like FoxPro, directly open the more than to save it as an ODBC data source. Supporting DDL is an important embodiment of DAO's good support for database programming. DDL (Data Definition Language) is called "Data Definition Language" in SQL Term, which is used to complete the operation of generating, modify, and delete database structures. The ODBC class only supports DML (Data Manipulation Language, Data Operation Language), which does not support DDL, so you can only complete the data of the data with the ODBC class, and the structure of the database cannot be involved. To perform DDL operation, only through the ODBC API. The DAO class also provides support for DML and DDL, which means that programs can use the DAO class to easily create databases and modify the structure of the database.

Compared to ODBC, DAO provides some new classes to enhance its functions, including:

The CDaotableDef class provides the definition of the structure of the table. Call CDAOTABLEDEF :: Open can get the structure definition of the table. Call cdaotabledef :: Create can create a new table, call cdaotabledef :: CreateField to add a field to the table, call CDaOTablesDef :: CreateIndex to add an index to the table. Call CDAOTABEDEF :: Append can save the newly created table into the database.

The CDAOQueryDef class represents a query definition, which can be stored in the database.

CDaoWorkSpace provides a data work area (Workspace). A work area can include several databases, and the workspace can perform all or separate transactions to the database, and the workspace is also responsible for the security of the database. If necessary, the program can open multiple workspaces.

Another important feature of DAO is that it provides powerful support for the Access database. Since DAO is based on Microsoft Jet Engine, DAO must do more articles on the Access database. For example, calling cdaodatabase :: Create to create an MDB file directly, the code as follows:

m_db.create ("c: //mydir/Mydb.mdb");

With AppWizard and ClassWizard, users can easily develop DAO-based Access database applications with excellent performance.

10.8.4 ODBC or DAO

Since DAO can access the ODBC data source, the following can be replaced by the DAO replacement ODBC:

Better performance can be obtained in some cases, especially when accessing the Microsoft Jet (.mdb) database.

Compatible with ODBC

DAO allows data effective inspection

DAO allows users to explain the relationship between tables and tables

Of course, the emergence of DAO does not mean that ODBC is outdated. If the user's work must be strictly limited to the ODBC data source, especially when developing the application of the Client / Server structure, there is good performance with ODBC.

Read and write Access files directly through DAO

Author: Xu Jing Zhou

Download Try Source Code http://www.vckbase.com/code/downloadcode.asp?id=1643

Directly use DAO to create, read the Access file, always say, "Directly through ODBC reading, write Excel file", " In the following example, we will use two methods: SQL and DAO class functions to make them, doing so, I want to make everyone more convenient and flexible to use them to complete what you want to do. In the sample program, you can specify the database name: demo.mdb, the internal table name is: demotable, write two fields: name and age, use and the previous reading and writing Excel similar operations, you can also need it according to yourself To dynamically change them. The sample program running interface as follows: Let us briefly look at its implementation steps:

1. First, make sure that it contains an AFXDAO.H header file, you can include it in the stdafx.h file, as follows:

#include // Join DAO Database Support

2. Declare the DAO library and its recordset, you can add the following code in your implementation file: CDAODatabase DB; // Database

CDAORECORDSET Recset (& DB); // Record Set

3. Next, let's take our creation and write operation void crwaccessdlg :: OnWriteAccess ()

{

/ / Get the path where the main program is located, there is spath

Cstring spath;

GetModuleFileName (null, spath.getbuffersetlength); max_path), MAX_PATH

Spath.releaseBuffer ();

Int npos;

Npos = spath.reversefind ('' // '');

Spath = spath.Left (npos);

/ / Create a data name by default: demo.mdb, internal table name: Demotable, two fields in the table: Name, age

CSTRING LPSZFILE = Spath "//demo.mdb";

CFILEFIND FFIND;

Bool bsuCcess;

BSUCCESS = ffind.findfile (lpszfile);

FFIND.CLOSE ();

/ / Do you have created a good demo.mdb file, not creating it

IF (! BSUCCESS)

{

db.create (lpszfile);

CString Sqlcmd = "CREATE TABLE Demotable (Name Varchar (20), Age Varchar (3));";

Db.execute (SQLCMD);

// Open the created data sheet

Recset.open (AFX_DAO_USE_DEFAULT_TYPE,

"SELECT * from demotable", 0);

/ / Add first record, use SQL statement

Db.execute ("INSERT INTO DEMOTABLE (Name, Age) VALUES ('' Xu Jingze '', 26)");

// Add a second record, use DAO

RecSet.addnew ();

Recset.setfieldValue ("Name", "Xu Zhihui");

Recset.setfieldValue ("Age", "21");

Recset.Update (); // Add a third record, use DAO

RecSet.addnew ();

Recset.setfieldValue ("Name", "Guo Huo");

Recset.setfieldValue ("age", "27");

RECSET.UPDATE ();

// Close record set and library

RECSET.CLOSE ();

db.close ();

AfxMessageBox ("Access file is written in success!");

}

Else

AfxMessageBox ("Demo.mdb database has been created!");

}

4. Finally, let us implement its read operation. Void CRWACCESSDLG :: OnReadAccess ()

{

Colevariant var; // field type

Var.ChangeType (VT_BSTR, NULL);

CString Strname, StRAGE, STRFILE

// Clear list box

m_accessList.resetContent ();

/ / Get the path where the main program is located, there is spath

Cstring spath;

GetModuleFileName (null, spath.getbuffersetlength); max_path), MAX_PATH

Spath.releaseBuffer ();

Int npos;

Npos = spath.reversefind ('' // '');

Spath = spath.Left (npos);

Strfile = spath "//demo.mdb";

DB.Open (STRFILE); // Open the created Demo database and Damotable table

Recset.open (AFX_DAO_USE_DEFAULT_TYPE, "SELECT * from Demotable", NULL);

While (! recsets.iseof ()) // Is there a table end?

{

Recset.GetfieldValue ("Name", var);

Strname = (lpcstr) Var.pbstrval;

Recset.GetfieldValue ("Age", VAR);

Strage = (lpcstr) Var.pbstrval;

M_accessList.Addstring (Strname "->" strage);

Recset.movenext ();

}

// Close record set and library

RECSET.CLOSE ();

db.close ();

}

The details of the specific implementation of the above part of the code can be downloaded after downloading the instance code, and the source code can be carefully viewed (in detail).

If the SQL method is used in the DAO program, the location of the ABC123 uses% S, then use comma m_deviceid, for example: strsql = ("SELECT * FROM table name where field name = '% s'", m_strname)

m_pdaodatabase-> eXcute (strsql);

m_pdaodatabase is a database pointer.

m_pmainwnd-> setWindowText ("Receiver");

CString str = "receiver";

CWND * PWND = CWND :: FindWindow (Null, STR);

IF (PWND)

PWND-> SendMessage (WM_COMM, 0, 0); find Handle

Looking for Handle and Path and Name seems to be closed, there are several functions to find Handle.

HWND FINDWINDOW

LPCTSTR LPCLASSNAME, // Class Name

LPCTSTR LPWINDOWNAME / / WINDOW NAME

);

Bool Enumwindows

WndenumProc LpenumFunc, // Callback Function

LParam lParam // Application-Defined Value

);

Function prototype: hwnd getwindow (hwnd hwnd, unit ncmd);

parameter:

HWND: Window handle. The window handle to be obtained is a handle based on the NCMD parameter value relative to this window.

NCMD: Description The relationship between the specified window and the window to get the handle. This parameter value can be one of the following:

GW_CHILD: If the specified window is a parent window, it is obtained that the handle of the sub-window on the top of the z-order, otherwise null. The function only checks the sub-window of the specified parent window and does not check the inheritance window.

GW_ENABLEDPOUP: (WindowsNT 5.0) The handle is identified in the enabled state pop-up window belonging to the specified window (retrieves the first window to satisfy the aforementioned condition by gw_hwndnext); if there is no window, the handle is obtained The same as the specified window.

GW_HWNDFIRST: The returned handle identifies the same type of window in the top of the z-order. If the specified window is the highest end window, the handle identifies the highest end window in the top of the z-order; if the specified window is a top window, the handle identifies the top window at the highest end of the z-order: If the specified window is a sub-window, The handle identifies the homotheit window in the Z-order.

GW_HWNDLAST: The returned handle identifies the same type of window at the lowest end of Z. If the specified window is the highest end window, the handle identifies the highest end window at the lowest end of the z-order: If the specified window is a top window, the handle identifies the top window of the low-end end of the z-order; if the specified window is a sub-window, The handle identifies the same window at the lowest end of the Z.

GW_HWndNext: The returned handler identifies the same type of window in the specified window in the z-order. If the specified window is the highest window, the handle identifies the highest end window under the specified window: If the specified window is a top window, the handle identifies the top window under the specified window; if the specified window is a child window, The handle identifies the same window in the specified window.

GW HWNDPREV: The returned handler identifies the same type of window on the specified window in the z-order. If the specified window is the highest end window, the handle identifies the highest end window on the specified window; if the specified window is a top window, the handle identifies the top window on the specified window; if the specified window is a sub-window, The handle identifies the same window on the specified window.

GW_WNER: The returned handler identifies the owner window of the specified window (if present).

Return Value: If the function is successful, the return value is a window handle; if there is no window with a specified window, the return value is NULL.

To get more error messages, call the getLastError function.

Note: Calling a function enormChildWindow is reliable than calling the getWindow function in the cyclic body. Calling a getWindow function implementation This task application may fall into a dead loop or return a window handle that has been destroyed.

转载请注明原文地址:https://www.9cbs.com/read-66961.html

New Post(0)