📄 obexftp.cxx
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include <windows.h>
#include <stdio.h>
#include <obex.h>
#include "..\parser\parser.h"
#define OBFTP_MAX_STRING 256
#define OBEX_TYPE_UNICODE 0x00
#define OBEX_TYPE_BYTESEQ 0x40
#define OBEX_TYPE_BYTE 0x80
#define OBEX_TYPE_DWORD 0xc0
#define OBEX_HID_CONNECTIONID (0x0b | OBEX_TYPE_DWORD)
GUID CLSID_FileExchange_NetOrder = // {F9ec7bc4-953c-11d2-984e-525400dc9e09}
{ 0xc47becf9, 0x3c95, 0xd211, {0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09}};
IObexDevice *FindDevice (IObex *pObex) {
IDeviceEnum *pDeviceEnum = 0;
HRESULT hRes = E_FAIL;
if (ERROR_SUCCESS == pObex->StartDeviceEnum())
{
DEBUGMSG(1,(L"[OBEXSTRESSTEST] Device enumeration started\n"));
Sleep(15000);
//enumerate through the devices
pObex->StopDeviceEnum();
DEBUGMSG(1,(L"[OBEXSTRESSTEST] Device enumeration stopped"));
DEBUGMSG(1,(L"[OBEXSTRESSTEST] Enumerating devices...\n"));
hRes = pObex->EnumDevices(&pDeviceEnum, __uuidof(BthTransport));
}
if(! (SUCCEEDED(hRes) && pDeviceEnum))
return NULL;
IObexDevice *pDeviceArray[100];
DWORD num = 0;
//release the device (if it exists)
hRes = pDeviceEnum->Next(sizeof(pDeviceArray) / sizeof(pDeviceArray[0]), pDeviceArray, &num);
pDeviceEnum->Release ();
if(! SUCCEEDED(hRes)) {
wprintf (L"No devices found!\n");
return NULL;
}
IPropertyBag *propBag;
for (int i = 0; i < (int)num; i++) {
if (SUCCEEDED(pDeviceArray[i]->EnumProperties (IID_IPropertyBag, (LPVOID *)&propBag))) {
//print the name out
VARIANT v;
VariantInit(&v);
if(SUCCEEDED(propBag->Read(L"Name", &v, NULL)))
wprintf (L"%d. %s\n", i + 1, v.bstrVal);
VariantClear(&v);
propBag->Release();
}
}
int isel = 0;
wprintf (L"Select the device (0 = abort): ");
wscanf (L"%d", &isel);
--isel;
for (i = 0 ; i < (int)num ; ++i) {
if (i != isel)
pDeviceArray[i]->Release ();
}
return ((isel >= 0) && (isel < (int)num)) ? pDeviceArray[isel] : NULL;
}
IObexDevice *DoConnect (IObex *pObex) {
IObexDevice *pObexDevice = FindDevice (pObex);
if (! pObexDevice)
return NULL;
IHeaderCollection *pHeaderCollection = 0;
//get a header collection
HRESULT hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IHeaderCollection), (void **)&pHeaderCollection);
if (FAILED(hr) || (! pHeaderCollection)) {
wprintf (L"Out of memory...\n");
pObexDevice->Release ();
return NULL;
}
pHeaderCollection->AddTarget(sizeof (CLSID_FileExchange_NetOrder),(UCHAR *)&CLSID_FileExchange_NetOrder);
//now, using the object, connect up
hr = pObexDevice->Connect(0,0,pHeaderCollection);
//loop through the headers looking for info
if(SUCCEEDED(hr)) {
IHeaderEnum *pHeaderEnum = 0;
hr = pHeaderCollection->EnumHeaders(&pHeaderEnum);
if(SUCCEEDED(hr)) {
ULONG ulHeadersFetched = 0;
do {
OBEX_HEADER *obHeader;
hr = pHeaderEnum->Next(1, &obHeader, &ulHeadersFetched);
if(SUCCEEDED(hr)) {
if(obHeader->bId == OBEX_HID_CONNECTIONID) {
hr = S_OK;
break;
}
}
} while(SUCCEEDED(hr) && ulHeadersFetched);
pHeaderEnum->Release();
}
}
pHeaderCollection->Release ();
if(FAILED(hr)) {
wprintf (L"Could not connect!\n");
pObexDevice->Release();
return NULL;
}
return pObexDevice;
}
int SendFile(IObexDevice *pObexDevice, WCHAR *pszFileName) {
WCHAR *name = wcsrchr (pszFileName, '\\');
if (! name)
name = pszFileName;
else
++name;
HANDLE hFile = CreateFile (pszFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
wprintf (L"Can't open %s\n", pszFileName);
return FALSE;
}
BY_HANDLE_FILE_INFORMATION fileInfo;
GetFileInformationByHandle(hFile, &fileInfo);
DWORD dwBytesTotal = fileInfo.nFileSizeLow;
DWORD dwBytesSent = 0;
IStream *myStream = 0;
IHeaderCollection *pHeaderCollection = 0;
//get a header collection
HRESULT hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IHeaderCollection), (void **)&pHeaderCollection);
if(SUCCEEDED(hr))
hr = pHeaderCollection->AddName(name);
if(SUCCEEDED(hr))
hr = pObexDevice->Put(pHeaderCollection, &myStream);
if (SUCCEEDED(hr)) {
char inBuf[5000];
DWORD written;
ULONG cbJustRead;
do {
if(!ReadFile(hFile, inBuf, sizeof(inBuf), &cbJustRead, 0))
break;
hr = myStream->Write(inBuf, cbJustRead, &written);
dwBytesSent += written;
fputws (L".", stdout);
} while (SUCCEEDED(hr) && (cbJustRead == sizeof(inBuf)));
hr = S_OK;
}
fputws (L"\n", stdout);
if (myStream)
myStream->Release();
CloseHandle(hFile);
if(pHeaderCollection)
pHeaderCollection->Release();
return 0;
}
HRESULT FetchFile(IObexDevice *pObexDevice, WCHAR *pszFileName) {
IHeaderCollection *pHeaderCollection = NULL;
//get a header collection
HRESULT hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IHeaderCollection), (void **)&pHeaderCollection);
if(FAILED(hr))
return E_FAIL;
//put in the name
pHeaderCollection->AddName(pszFileName);
IStream *myStream = NULL;
hr = pObexDevice->Get(pHeaderCollection, &myStream);
if(SUCCEEDED(hr))
{
//create a file with this name
HANDLE hFile = CreateFile (pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if(INVALID_HANDLE_VALUE != hFile) {
char *pBuffer = 0;
UINT pDirSize = 0;
char inBuf[100];
do
{
ULONG cbJustRead;
hr = myStream->Read(inBuf, sizeof(inBuf), &cbJustRead);
if (SUCCEEDED(hr)) {
DWORD written;
if (!WriteFile(hFile, inBuf, cbJustRead, &written, 0))
break;
}
} while (SUCCEEDED(hr));
hr = S_OK;
CloseHandle(hFile);
}
myStream->Release();
}
pHeaderCollection->Release();
return hr;
}
HRESULT Dir (IObexDevice *pObexDevice, WCHAR *szDir) {
IHeaderCollection *pHeaderCollection = NULL;
//get a header collection
HRESULT hr = CoCreateInstance(__uuidof(HeaderCollection), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IHeaderCollection), (void **)&pHeaderCollection);
if (FAILED(hr) || (! pHeaderCollection)) {
wprintf (L"Out of memory : error 0x%08x\n", hr);
return hr;
}
//put a name of "" so we get the directory listing
pHeaderCollection->AddName(szDir);
IStream *myStream;
hr = pObexDevice->Get(pHeaderCollection, &myStream);
WIN32_FIND_DATA wfd;
ObexFileSearch *pSearch = ObexFindFileFirst (myStream, L"*.*", &wfd);
if (pSearch) {
do {
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
wprintf (L"[%s]\n", wfd.cFileName);
else
wprintf (L"%s\n", wfd.cFileName);
} while (ObexFindFileNext (pSearch, &wfd));
ObexCloseSearch (pSearch);
}
myStream->Release();
pHeaderCollection->Release();
return 0;
}
int wmain (int argc, WCHAR **argv) {
CoInitializeEx (NULL, COINIT_MULTITHREADED);
IObex *pObex = NULL;
HRESULT hr = CoCreateInstance(__uuidof(Obex), NULL, CLSCTX_INPROC_SERVER, __uuidof(IObex), (void **)&pObex);
if(FAILED(hr)) {
fputws (L"Obex initialization failed!\n", stdout);
exit (1);
}
if (pObex != NULL)
pObex->Initialize ();
else
{
fputws (L"pObex->Initialize failed!\n", stdout);
exit (1);
}
IObexDevice *pObexDevice = NULL;
fputws (L"Type \"help\" for command list...\n", stdout);
for ( ; ; ) {
WCHAR sz[OBFTP_MAX_STRING];
fputws (L"Command> ", stdout);
fgetws (sz, sizeof(sz) / sizeof(sz[0]), stdin);
WCHAR *p = wcschr (sz, L'\n');
if (p)
*p = '\0';
if (sz[0] == '\0')
continue;
if (wcsicmp (sz, L"exit") == 0)
break;
if (wcsicmp (sz, L"disconnect") == 0) {
if (pObexDevice) {
pObexDevice->Release ();
pObexDevice = NULL;
fputws (L"Successfull...\n", stdout);
} else
fputws (L"Not connected!\n", stdout);
continue;
}
if (wcsicmp (sz, L"connect") == 0) {
if (pObexDevice)
fputws (L"Connected already, disconnect first!\n", stdout);
else
pObexDevice = DoConnect (pObex);
continue;
}
if (wcsnicmp (sz, L"put ", 4) == 0) {
SendFile (pObexDevice, sz + 4);
continue;
}
if (wcsnicmp (sz, L"get ", 4) == 0) {
FetchFile (pObexDevice, sz + 4);
continue;
}
if (wcsnicmp (sz, L"cd ", 3) == 0) {
HRESULT hr = pObexDevice->SetPath (sz + 3, 0, 0);
if (! SUCCEEDED(hr))
wprintf (L"Failed, error 0x%08x\n", hr);
continue;
}
if (wcsnicmp (sz, L"dir ", 4) == 0) {
Dir (pObexDevice, sz + 4);
continue;
}
fputws (L"Supported commands:\n", stdout);
fputws (L"\tconnect connect to a device from the list\n", stdout);
fputws (L"\tdisconnect terminate current connection\n", stdout);
fputws (L"\tput <file name> send file from remote source\n", stdout);
fputws (L"\tget <file name> get file from remote source\n", stdout);
fputws (L"\tcd <dir name> change current remote directory\n", stdout);
fputws (L"\tdir get current remote directory listing\n", stdout);
}
pObex->Shutdown ();
CoUninitialize ();
return 0;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd) {
WCHAR *argv[2];
argv[0] = L"\\windows\\obexftp.exe";
argv[1] = NULL;
return wmain (1, argv);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -