Supports MFC CSocket in ATL

zhaozj2021-02-11  198

Sendfile control

1. Function:

SendFile can be transmitted (arbitrary form) single file (file), if you are a web page, you can transfer it with all the files in the Files directory.

2, design ideas:

¨¨ Depending on the user to select the file you need to transfer, some related information (mainly the file name) is obtained, and then the file to the server or the client;

¨ Judging whether there is the same name. Files directory, if there is, traverse the directory, each of the file names, will be sent;

¨ The end of the traversal, release all resources.

3, control usage:

The control has a unique DAUL interface Filework, which is not visible when running; the interface contains a unique method. Transdoc.

TRANSDOC parameter description:

¨ BSTR Servername: For the Domino server name;

¨ BSTR Hostname: For the host name or host IP address;

¨ uint port: Host port number;

¨ BSTR User: Current user name;

¨ BSTR Password: current user password;

¨ BSTR DOCFILENAME: Full name;

¨ BSTR UDATABASE: Domino database name;

¨ BSTR UFORM: Domino form;

Note: BSTR is a Basic String / Binary String, which behaves in the VB as a String type.

4, sample:

Below I give an example of using this space in VB:

¨ Newly built a standard EXE project;

¨ Add a TextBox on Form, two Command Button, a CommON Dialog. The property is as follows:

Control Name Property

Textbox TxtFileName

Command Button1 Caption ...

Command Button2 CAPTION Send File

Code:

Private submmand1_click ()

CommonDialog1.showopen

TXTFILENAME.TEXT = CommonDialog1.FileName

End Sub

Private sub fascist2_click ()

Filework1.transdoc "Domserver", "10.128.0.3", 1028, "Zhangchunye", "Zhangchunye", TXTFileName.Text, "Test", "TEST"

End Sub

Modern code:

// Filework.cpp: Implementation of cfilework # include "stdafx.h"

#include "sendfile.h"

#include "filework.h"

#include

/

// cfilework

Stdmethodimp Cfilework :: Transdoc (BSTR Servername, BSTR Hostname, Int Port, BSTR USER, BSTR Password, BSTR Docfilename, BSTR UDatabase, BSTR UFORM)

{

AFX_MANAGE_STATE (AFXGETSTATICModuleState ())

// Todo: Add Your Implementation Code Here

Uses_Conversion;

AfxSocketinit (NULL);

Csocket sockclient;

SockClient.create ();

m_hostname = OLE2T (HostName);

M_Port = port;

// MessageBox (m_hostname, _t ("Hostname!"), MB_OK);

IF (! sockclient.connect (m_hostname, port))

{

AfxMessageBox ("Connect to the other machine failed!");

Return 0;

}

m_username = ole2t (user);

m_password = OLE2T (Password);

// Send username and password requirements to log in

// Username and password need to be encrypted. Then send it again.

SockClient.send (m_username, strlen);

SockClient.send (m_password, strlen (m_password));

SockClient.send (m_servername, strlen (m_servername));

SockClient.send (M_Database, Strlen (M_Database));

SockClient.send (M_Form, Strlen (M_Form));

User name and password can be sent by verifying

Char * filename = ole2t (docfilename);

Cfile myfile;

IF (! myfile.open (filename, / * conversion bstr to char * * /

Cfile :: ModeRead | CFILE :: TypeBinary))

{

Return 0;

}

Socket_stream_file_info streamfileinfo;

Win32_find_data findfiledata;

FindClose (MyFile.GetFilePath (), & FindFileData);

MEMSET (& Streamfileinfo, 0, SIZEOF (socket_stream_file_info);

Strcpy (streamfileinfo.szfileTitle, MyFile.GetFileName ());

Streetfileinfo.nfilesizelow = findfiledata.nfilesizer;

SockClient.send (& StreamFileInfo, Sizeof (socket_stream_file_info);

Uint dwread = 0;

While (dwread

BYTE * DATA = New byte [5120];

Uint dw = myfile.read (data, 5120);

SockClient.send (DATA, DW);

DWREAD = DW;

}

// The following code is set. HTM is the same .files directory.

Char szcurdir [256];

:: GetCurrentDirectory (Sizeof (Szcurdir), Szcurdir;

LPCSTR FileTitle;

FileTitle = (LPCTSTST) myfile.getfileTitle ();

Strcat (szcurdir, "//");

STRCAT (SZCURDIR, FILETITLE);

STRCAT (SZCURDIR, ". Files //");

// Set the end of the directory.

myfile.close ();

Recurse (T2OL (SZCurdir);

LPTSTR M_ENDCODE = "000";

SockClient.send (M_Endcode, Strlen (m_endcode));

MessageBox (_T ("Information release success!"), _ T ("information release", MB_ICONITIONFORMATION | MB_OK);

SockClient.Close ();

Return S_OK;

}

// stdafx.h: include File for Standard System Include Files,

// or Project Specific Include Files That Are Used Frequently,

// But Are Changed Infrequently

#if! defined (AFX_STDAFX_H__0AC71DA2_A0CD_4AA6_8E23_5C459FD52A59__INCluded_)

#define AFX_STDAFX_H__0AC71DA2_A0CD_4AA6_8E23_5C459FD52A59__included_

#iF _MSC_VER> 1000

#pragma overce

#ENDIF / / 100 m _ _

#define strict

#ifndef _win32_winnt

#define _win32_winnt 0x0400

#ENDIF

#define _atl_apartment_threaded

#include

#include

#include

// You May Derive a class from ccommodule and use it ing you want to override

// Something, But Do Not Change The Name of_Module

EXTERN CCOMMODULE _MODULE;

#include

#include

#include

#include

#include

#include

#include

// {{AFX_INSERT_LOCATION}}

// Microsoft Visual C Will Insert Additional Declarations Immediate Line. # Endif //! Defined (AFX_STDAFX_H__0AC71DA2_A0CD_4AA6_8E23_5C459FD52A59__INCLUDED)

// this file will be processed by the Mid Tool To

// Produce The Type Library (Sendfile.tlb) and Marshalling Code.

Import "OAIDL.IDL";

Import "OCIDL.IDL";

#include "olectl.h"

[

Object,

UUID (68C3C9C3-08CF-4B80-8C8A-1A1503EAF27B),

Dual,

Helpstring ("ifilework interface"),

Pointer_DEFAULT (UNIQUE)

]

Interface iFilework: idispatch

{

[ID (1), Helpstring ("Method Transdoc")] HRESULT TRANSDOC ([in] bstr servername, [in] bstr hostname, [in] int port, [in] bstr user, [in] BSTR Password, [in] BSTR docfilename, [in] BSTR UDATABASE, [IN] BSTR UFORM;

}

[

UUID (AACF2ED6-2C63-4A9F-B2DB-05FD8EBDDB7E),

Version (1.0),

Helpstring ("SendFile 1.0 Type Library)

]

Library SendFileLib

{

Importlib ("stdole32.tlb");

Importlib ("stdole2.tlb");

[

UUID (E3A8C76D-FD5C-4DF6-9D90-DDD47EC8567C),

Helpstring ("_ ifileworkevents interface")

]

Dispinterface_iFileWorKevents

{

Properties:

Methods:

}

[

UUID (4D8F2BCB-0546-4DFA-AC6E-C2C3BC26BE38),

Helpstring ("FileWork Class")

]

CoClass Filework

{

[default] interface meanswork;

[Default, Source] Dispinterface_iFileWorkevents;

}

}

// Filework.h: Declaration of the cfilework

#ifndef __filework_h_

#define __filework_h_

#include "resource.h" // main symbols

#include

#include "sendfilecp.h"

/ add class

Typedef struct _socket_stream_file_info {

Tchar SzfileTitle [128]; // The title name

DWORD dwreserved1; // Reserved, for 0

} Socket_Stream_File_info, * psocket_stream_file_info; /

// cfilework

Class ATL_NO_VTABLE CFILEWORK:

Public CComobjectrootex ,

Public IDispatchImpl ,

Public CCOMControl ,

Public IPersistStreamInitiMPL ,

Public IOLCONTROLIMPL ,

Public IoleObjectImpl ,

Public IoleinplaceActiveObjectImpl ,

Public iViewObjectExImpl ,

Public ioleinplaceObjectWindownessImpl ,

Public isupporterrorinfo,

Public iconnectionPointContainerImpl ,

Public CCOMCOCLASS ,

Public CPROXY_IFILEWORKEVENTS

{

PUBLIC:

Cfilework ()

{

M_PunkMarshaler = NULL;

}

Declare_get_controlling_unknown ()

DECLARE_REGISTRY_RESOURCEID (iDR_Filework)

Declare_protect_final_construct ()

Begin_COM_MAP (CFILEWORK)

COM_ITERFACE_ENTRY (ifilework)

COM_ITERFACE_ENTRY (Idispatch)

COM_ITERFACE_ENTRY (iViewObjectex)

COM_ITERFACE_ENTRY (IVIEWObject2)

COM_ITERFACE_ENTRY (iViewObject)

COM_IleinPlaceObjectWindowless

Com_interface_entry (ioleinplaceObject)

Com_interface_entry2 (IoleWindow, IoleinplaceObjectWindowless)

Com_interface_entry (IoleinplaceActiveObject)

COM_ILCONTROL (IOLCONTROL)

Com_interface_entry (ioleObject)

COM_Interface_entry (IPERSISTREAMINIT)

Com_interface_entry2 (IPERSIST, IPERSISTREAMINIT)

COM_Interface_entry (Isupp PorterrorInfo)

COM_Interface_entry (iconnectionPointContainer)

COM_INTERFACE_ENTRY_AGGREGATE (IID_IMARSHAL, M_PUNKMARSHAL.P)

COM_ITERFACE_ENTRY_IMPL (iconnectionPointContainer)

END_COM_MAP ()

Begin_prop_map (cfilework)

Prop_Data_ENTRY ("_ cx", m_sizeextent.cx, vt_ui4) prop_data_entry ("_ cy", m_sizeextent.cy, vt_ui4)

// EXAMPLE ENTRIES

// Prop_ENTRY ("Property Description", Dispid, CLSID)

// Prop_Page (CLSID_StockColorPage)

END_PROP_MAP ()

Begin_Connection_Point_map (CFilework)

Connection_Point_ENTRY (DIID__IFILEWORKEVENTS)

END_CONNECTION_POINT_MAP ()

Begin_msg_map (cfilework)

CHAIN_MSG_MAP (CCOMControl )

Default_reflection_handler ()

END_MSG_MAP ()

// Handler Prototypes:

// Lresult MessageHandler (Uint UMSG, WPARAM WPARAM, LPARAM LPARAM, BOOL & BHANDED);

// Lresult Commandrandler (Word Word WID, HWND HWNDCTL, BOOL & BHANDED);

// LResult NotifyHandler (int IDCTRL, LPNMHDR PNMH, Bool & Bhandled);

HRESULT FINALCONSTRUCT ()

{

Return CocreateFreethreadedMarshaler

GetControllingunkNown (), & M_PunkMarshaler.p);

}

Void FinalRelease ()

{

m_PunkMarshaler.Release ();

}

CComptr m_punkmarshaler;

// IsupportSerrorInfo

StdMethod (InterfaceSupportSerrorInfo) (Refiid Riid)

{

Static const Iid * arr [] =

{

& IID_IFILEWORK,

}

For (int i = 0; i

{

IF (Inlineisequalguid (* Arr [I], RIID))

Return S_OK;

}

Return S_FALSE;

}

// iViewObjectex

Declare_view_status (viewstatus_solidbkgn | viewstatus_opaque)

// ifilework

PUBLIC:

LPTSTR M_FORM;

LPTSTR M_DATABASE;

HRESULT Sendfile;

Void recurse (BSTR FilePath);

LPTSTR M_SERVERNAME;

LPTSTR M_HOSTNAME;

INT m_port;

LPTSTR M_USERNAME;

LPTSTR M_Password;

Csocket sockclient;

STDMETHOD (/ * [in] * / bstr servername, / * [in] * / bstr hostname, / * [in] * / int port, / * [in] * / bstr user, / * [in] * / Bstr password, / * [in] * / bstr docFileName, / * [in] * / bstr udatabase, / * [in] * / bstr uform; HRESULT OONDRAW (ATL_DRAWINFO & DI)

{

Rect & rc = * (Rect *) di.prcbounds;

Rectangle (di.hdcdraw, rc.left, rc.top, rc.right, rc.bottom);

SetTextAlign (Di.hdcdraw, Ta_Center | TA_BASELINE);

LPCTSTR PSZTEXT = _T ("ATL 3.0: Filework");

Textout (di.hdcdraw,

(rc.left rg.right) / 2,

(rc.top rc.bottom) / 2,

PSZText,

Lstrlen (psztext);

Return S_OK;

}

}

#ENDIF / / __ Filework_H_

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

New Post(0)