📄 findsrv.cpp
字号:
//====================================================================
// FindSrv - A RAPI stream server DLL
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//====================================================================
#include <windows.h> // For all that Windows stuff
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))
//-----------------------------------------------------------------------
// Add if not defined.
#ifndef RAPISTREAMFLAG
typedef enum tagRAPISTREAMFLAG {
STREAM_TIMEOUT_READ
} RAPISTREAMFLAG;
DECLARE_INTERFACE_ (IRAPIStream, IStream)
{
STDMETHOD(SetRapiStat)(THIS_ RAPISTREAMFLAG Flag,
DWORD dwValue) PURE;
STDMETHOD(GetRapiStat)(THIS_ RAPISTREAMFLAG Flag,
DWORD *pdwValue) PURE;
};
#endif
//-----------------------------------------------------------------------
// Function prototypes declared as exports from the DLL.
// Bracket so that function name won't be mangled by C++.
extern "C" {
__declspec(dllexport) INT RAPIFindFile (DWORD cbInput, BYTE *pInput,
DWORD *pcbOutput, BYTE **ppOutput,
IRAPIStream *pIRAPIStream);
}
//====================================================================
// DllMain - DLL initialization entry point
//
BOOL WINAPI DllMain (HANDLE hinstDLL, DWORD dwReason,
LPVOID lpvReserved) {
return TRUE;
}
//-----------------------------------------------------------------------
// WriteToClient - Writes a command and optional string to the client
//
int WriteToClient (INT nCmd, INT nSize, LPTSTR pszStr,
IRAPIStream *pIRAPIStream) {
INT nBuff;
DWORD cbBytes;
HRESULT hr;
// Write command code.
hr = pIRAPIStream->Write (&nCmd, sizeof (nCmd), &cbBytes);
// Write size value.
hr = pIRAPIStream->Write (&nSize, sizeof (nSize), &cbBytes);
// Write length of string.
nBuff = (lstrlen (pszStr) + 1) * sizeof (TCHAR);
hr = pIRAPIStream->Write (&nBuff, sizeof (nBuff), &cbBytes);
// Write string.
hr = pIRAPIStream->Write (pszStr, nBuff, &cbBytes);
return 0;
}
int nFlag;
//-----------------------------------------------------------------------
// SrchDirectory - Recursive routine that searches a directory and all
// child dirs for matching files
//
int SrchDirectory (LPTSTR pszDir, IRAPIStream *pIRAPIStream) {
WIN32_FIND_DATA fd;
TCHAR szNew[MAX_PATH];
INT i, rc, nErr = 0;
HANDLE hFind;
TCHAR *pPtr, *pSrcSpec;
// Separate subdirectory from search specification.
for (pSrcSpec = pszDir + lstrlen (pszDir); pSrcSpec >= pszDir;
pSrcSpec--)
if (*pSrcSpec == TEXT ('\\'))
break;
// Copy the search specification up to the last directory sep char.
if (pSrcSpec <= pszDir)
lstrcpy (szNew, TEXT ("\\"));
else {
for (i = 0; (i < dim(szNew)-10) &&
((pszDir+i) <= pSrcSpec); i++)
szNew[i] = *(pszDir+i);
szNew[i] = TEXT ('\0');
}
pPtr = szNew + lstrlen (szNew);
// Report directory we're searching.
WriteToClient (2, 0, szNew, pIRAPIStream);
// Find matching files.
hFind = FindFirstFile (pszDir, &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
// Report all matching files.
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
WriteToClient (1, fd.nFileSizeLow, fd.cFileName,
pIRAPIStream);
rc = FindNextFile (hFind, &fd);
} while (rc);
FindClose (hFind);
} else {
rc = GetLastError();
if ((rc != ERROR_FILE_NOT_FOUND) &&
(rc != ERROR_NO_MORE_FILES)) {
TCHAR szDbg[64];
wsprintf (szDbg, TEXT ("1Find Error:%d"), rc);
WriteToClient (99, 0, szDbg, pIRAPIStream);
return -1;
}
}
// Create generic search string for all directories.
lstrcat (szNew, TEXT ("*.*"));
hFind = FindFirstFile (szNew, &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// Recurse to the lower directory.
lstrcpy (pPtr, fd.cFileName);
lstrcat (pPtr, pSrcSpec);
nErr = SrchDirectory (szNew, pIRAPIStream);
if (nErr) break;
*pPtr = TEXT ('\0');
}
rc = FindNextFile (hFind, &fd);
} while (rc);
FindClose (hFind);
} else {
rc = GetLastError();
if ((rc != ERROR_FILE_NOT_FOUND) &&
(rc != ERROR_NO_MORE_FILES)) {
TCHAR szDbg[64];
wsprintf (szDbg, TEXT ("2Find Error:%d"), rc);
WriteToClient (99, 0, szDbg, pIRAPIStream);
return -1;
}
}
return nErr;
}
//======================================================================
// RAPIFindFile - Searches the device for matching files. Called from
// PC application using RAPI.
//
INT RAPIFindFile (DWORD cbInput, BYTE *pInput, DWORD *pcbOutput,
BYTE **ppOutput, IRAPIStream *pIRAPIStream) {
INT nBuff;
DWORD i, cbBytes;
TCHAR *pPtr;
HRESULT hr;
*pcbOutput = 0;
// See if proper zero-terminated string.
pPtr = (LPTSTR)pInput;
for (i = 0; i < cbInput / 2; i++)
if (!*pPtr++)
break;
// If not zero terminated or if zero length, return error.
if ((i >= cbInput / 2) || (i == 0))
return -2;
nFlag = 0;
// Search for files.
SrchDirectory ((LPTSTR) pInput, pIRAPIStream);
// Write end code. Cmd 0 -> end of search
nBuff = 0;
hr = pIRAPIStream->Write (&nBuff, sizeof (nBuff), &cbBytes);
// Release the interface.
pIRAPIStream->Release ();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -