Code Snippet
//
// 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>
#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)
IObexDevice *FindDevice (IObex *pObex,
int deviceIndex) {
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(IrdaTransport));
}
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();
}
}
wprintf (L
"Selecting device %d: ", deviceIndex);
--deviceIndex;
for (int i = 0 ; i < (int)num ; ++i) {
if (i != deviceIndex)
pDeviceArray[i]->Release ();
}
return ((deviceIndex >= 0) && (deviceIndex < (int)num)) pDeviceArray[deviceIndex] : NULL;
}
IObexDevice *DoConnect (IObex *pObex) {
// Hard code this to decide to beam to the first device found
IObexDevice *pObexDevice = FindDevice (pObex, 1);
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;
}
//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;
}
int
WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd) {
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;
// Connect
pObexDevice = DoConnect (pObex);
// Send a file
SendFile (pObexDevice, L
"\\test.txt");
// Disconnect
if (pObexDevice) {
pObexDevice->Release ();
pObexDevice = NULL;
fputws (L
"Successfull...\n", stdout);
}
pObex->Shutdown ();
CoUninitialize ();
return 0;
}