Windows Mobile 6 Professional C++ [20.02.2012]

Windows Mobile 6 Professional, , . PocketOutlook, WAV , .

, , , , - . , . .

. . , . , , . RingTone , . , , .

IContact . , RingTone ( Framework.NET , RingTone - ), , IContact, get_RingTone put_RingTone.

.

 
#include "stdafx.h"
#include "initguid.h"
#include "pimstore.h"
 
 
//
#define SOUND_FOLDER L"\\My Documents\\ \\"
#define MAX_ANY_BUFFER_SIZE 1024
 
// ,
#define THROW_IF_ZERO(x) if(!(x))throw "THROW_IF_FALSE ["#x"]";
#define THROW_IF_INVALID(x) if(((unsigned long)(x)) >> 31)throw "THROW_IF_INVALID ["#x"]";
#define THROW_IF_INVALID_FILE(x) if(INVALID_HANDLE_VALUE == (x))throw "THROW_IF_INVALID_FILE ["#x"]";
 
//
wchar_t* morse[] = {L".-", L"-...", L".--", L"--.", L"-..", L".", L"...-", L"--..", L"..", L".---"
, L"-.-", L".-..", L"--", L"-.", L"---", L".--.", L".-.", L"...", L"-", L"..-", L"..-.", L"...."
, L"-.-.", L"---.", L"----", L"--.-", L"-..-", L"-.--", L"-..-", L"..-..", L"..--", L".-.-"};
 
BOOL CreateMorseSound(const wchar_t*, const wchar_t*);
void SelectData(BYTE*, DWORD*);
 
// Framework.NET RingTone ,
// IContact .
// IContact
//
interface IMyContact : public IContact
{
void get_RingTone(wchar_t* pwch)
{
IItem * pItem = NULL;
THROW_IF_INVALID(this->QueryInterface(IID_IItem, (void**)&pItem));
 
CEPROPID propId = PIMPR_RINGTONE;
 
ULONG buff_size = 0;
CEPROPVAL* ppropVal = NULL;
HANDLE hHeap = GetProcessHeap();
 
THROW_IF_INVALID(pItem->GetProps(&propId, CEDB_ALLOWREALLOC, 1, &ppropVal, &buff_size, hHeap));
 
if(ppropVal->wFlags == 0) wcscpy(pwch, ppropVal->val.lpwstr);
else pwch[0] = '\0';
 
HeapFree(hHeap, 0, ppropVal);
pItem->Release();
}
 
void put_RingTone(wchar_t* pwch)
{
IItem * pItem = NULL;
THROW_IF_INVALID(this->QueryInterface(IID_IItem, (void**)&pItem));
 
CEPROPVAL propVal;
propVal.propid = PIMPR_RINGTONE;
propVal.wLenData = 0;
propVal.val.lpwstr = pwch;
propVal.wFlags = (pwch[0] == '\0') ? CEDB_PROPDELETE : 0;
 
THROW_IF_INVALID(pItem->SetProps(0, 1, &propVal));
THROW_IF_INVALID(pItem->Save());
 
pItem->Release();
}
};
 
int _tmain(int argc, _TCHAR* argv[])
{
IPOutlookApp * polApp = NULL;
IFolder * pFolder = NULL;
IPOutlookItemCollection * polItems = NULL;
IMyContact * pContact = NULL;
 
BSTR pwsz;
int count;
 
try
{
CoInitializeEx(NULL, 0);
 
// Outlook,
THROW_IF_INVALID(CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER, IID_IPOutlookApp, (LPVOID*)&polApp));
 
THROW_IF_INVALID(polApp->Logon(NULL));
 
polApp->GetDefaultFolder(olFolderContacts, &pFolder);
pFolder->get_Items(&polItems);
polItems->get_Count(&count);
 
wchar_t wchRingtone[MAX_ANY_BUFFER_SIZE];
wchar_t wchNewRingtone[MAX_ANY_BUFFER_SIZE];
 
//
// . ,
// RingTone
for(int i = 1; i <= count; i++)
{
polItems->Item(i, (IDispatch**)&pContact);
pContact->get_CompanyName(&pwsz);
 
pContact->get_RingTone(wchRingtone);
 
if(wchRingtone[0] == '\0' || 0xffffffff == GetFileAttributes(wchRingtone))
{
wcscpy_s(wchNewRingtone, sizeof(wchNewRingtone), SOUND_FOLDER);
wcscat_s(wchNewRingtone, sizeof(wchNewRingtone), pwsz);
wcscat_s(wchNewRingtone, sizeof(wchNewRingtone), L".wav");
 
if(0 != wcscmp(wchRingtone, wchNewRingtone))
pContact->put_RingTone(wchNewRingtone);
 
// ,
if(0xffffffff == GetFileAttributes(wchNewRingtone))
CreateMorseSound(pwsz, SOUND_FOLDER);
}
 
pContact->Release();
}
 
polItems->Release();
pFolder->Release();
polApp->Logoff();
polApp->Release();
}
catch(...)
{
int err = GetLastError();
}
 
CoUninitialize();
 
return 0;
}
 
BOOL CreateMorseSound(const wchar_t* pWord, const wchar_t* pPath)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
DWORD len, sz;
wchar_t file_name[128];
void* pdot = NULL;
DWORD dot_size = 0;
void* pdash = NULL;
DWORD dash_size = 0;
void* psilent = NULL;
DWORD silent_size = 0;
 
DWORD header_sz = 0;
DWORD data_sz = 0;
 
try
{
 
// ,
THROW_IF_INVALID_FILE(hFile = CreateFile(L"\\My Documents\\ \\dot.wav", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
dot_size = GetFileSize(hFile, NULL);
THROW_IF_ZERO(pdot = LocalAlloc(LPTR, dot_size));
THROW_IF_ZERO(ReadFile(hFile, pdot, dot_size, &len, NULL) && dot_size == len);
CloseHandle(hFile);
 
THROW_IF_INVALID_FILE(hFile = CreateFile(L"\\My Documents\\ \\dash.wav", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
dash_size = GetFileSize(hFile, NULL);
THROW_IF_ZERO(pdash = LocalAlloc(LPTR, dash_size));
THROW_IF_ZERO(ReadFile(hFile, pdash, dash_size, &len, NULL) && dash_size == len);
CloseHandle(hFile);
 
THROW_IF_INVALID_FILE(hFile = CreateFile(L"\\My Documents\\ \\silent.wav", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
silent_size = GetFileSize(hFile, NULL);
THROW_IF_ZERO(psilent = LocalAlloc(LPTR, silent_size));
THROW_IF_ZERO(ReadFile(hFile, psilent, silent_size, &len, NULL) && silent_size == len);
CloseHandle(hFile);
 
wcscpy_s(file_name, 128, pPath);
wcscat_s(file_name, 128, pWord);
wcscat_s(file_name, 128, L".wav");
 
// WAV
THROW_IF_INVALID_FILE(hFile = CreateFile(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL));
 
THROW_IF_ZERO(0 == memcmp((BYTE*)pdot + 12, "fmt ", 4));
 
BYTE* pb = (BYTE*)pdot + 16;
sz = 20 + (pb[0] | (pb[1]<<8) | (pb[2]<<16) | (pb[3]<<24));
THROW_IF_ZERO(WriteFile(hFile, pdot, sz, &len, NULL) && sz == len);
header_sz += sz;
 
THROW_IF_ZERO(WriteFile(hFile, "data\0\0\0\0", 8, &len, NULL) && 8 == len);
header_sz += 8;
// ,
SelectData((BYTE*)pdot, &dot_size);
SelectData((BYTE*)pdash, &dash_size);
SelectData((BYTE*)psilent, &silent_size);
 
//
for(const wchar_t* pw = pWord; *pw != '\0'; pw++)
{
int ind = *pw - 0x0410;
if(ind > 63)ind = 63;
if(ind > 31)ind -= 32;
for(const wchar_t* pc = morse[ind]; *pc != '\0'; pc++)
{
switch(*pc)
{
case '.':
THROW_IF_ZERO(WriteFile(hFile, pdot, dot_size, &len, NULL) && dot_size == len);
data_sz += dot_size;
break;
case '-':
THROW_IF_ZERO(WriteFile(hFile, pdash, dash_size, &len, NULL) && dash_size == len);
data_sz += dash_size;
break;
default:
break;
}
}
THROW_IF_ZERO(WriteFile(hFile, psilent, silent_size, &len, NULL) && silent_size == len);
data_sz += silent_size;
}
//
THROW_IF_ZERO(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, header_sz - 4, NULL, FILE_BEGIN));
THROW_IF_ZERO(WriteFile(hFile, (void*)&data_sz, 4, &len, NULL) && 4 == len);
 
THROW_IF_ZERO(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, 4, NULL, FILE_BEGIN));
sz = header_sz + data_sz - 8;
THROW_IF_ZERO(WriteFile(hFile, (void*)&sz, 4, &len, NULL) && 4 == len);
 
CloseHandle(hFile);
LocalFree(pdot);
LocalFree(pdash);
LocalFree(psilent);
}
catch(char*)
{
if(hFile != INVALID_HANDLE_VALUE)CloseHandle(hFile);
if(pdot)LocalFree(pdot);
if(pdash)LocalFree(pdash);
if(psilent)LocalFree(psilent);
return FALSE;
}
 
return TRUE;
}
 
// WAV ,
//
void SelectData(BYTE* pv, DWORD* psize)
{
THROW_IF_ZERO(*psize > 31);
THROW_IF_ZERO(0 == memcmp(pv, "RIFF", 4) && 0 == memcmp(pv + 8, "WAVE", 4));
 
for(DWORD dw = 12; dw < *psize - 8; dw += (8 + (pv[dw + 4] | (pv[dw + 5]<<8) | (pv[dw + 6]<<16) | (pv[dw + 7]<<24))))
if(0 == memcmp(pv + dw, "data", 4))
{
DWORD sz = (pv[dw + 4] | (pv[dw + 5]<<8) | (pv[dw + 6]<<16) | (pv[dw + 7]<<24));
THROW_IF_ZERO(dw + sz + 8 < *psize);
memmove_s(pv, *psize, pv + dw + 8, sz);
*psize = sz;
return;
}
THROW_IF_ZERO(FALSE);
}
 
 
 

 

 

www.alvis.com.ua
, ,

m-t.com.ua
, ,

www.rdsrv.org

 -  -  © kuzmin@it.kharkov.ua +380504010794