[Windows Mobile Development and Hacking General] ghazanfarali17:
Hi All,

I am developing a VOIP application for Windows Mobile. I want the user to switch the between the Speaker and Earpiece of the Mobile Phone. I have searched the Forum and found the following link

http://teksoftco.com/forum/viewtopic.php?p=1909

As you know we have to use LoadLibrary and GetProcAddress to access the RIL APIs in Windows Mobile. I have written following piece of code for this i.e.

HRIL hRIL;
bool speakerphone = true; // false;
HRESULT result = RIL_Initialize(1, ResultCallback, NotifyCallback, RIL_NCLASS_MISC, 0, &hRIL);
if (result != S_OK) {
wchar_t buffer[200];
wsprintf(buffer, L"Could not open RIL, error=0x%x", result);
AfxMessageBox(buffer);
return;
}

RILAUDIODEVICEINFO info;
info.cbSize = sizeof(info);
info.dwParams = RIL_PARAM_ADI_ALL; // RIL_PARAM_ADI_TXDEVICE;
info.dwRxDevice = RIL_AUDIO_HANDSET;
info.dwTxDevice = speakerphone ? RIL_AUDIO_NONE : RIL_AUDIO_HANDSET;

result = RIL_SetAudioDevices(hRIL, &info);
if (result != S_OK) {
wchar_t buffer[200];
wsprintf(buffer, L"Could not open RIL, error=0x%x", result);
AfxMessageBox(buffer);
}

RIL_Deinitialize(hRIL);

But this doesn't seems to work, i.e. some time it causes both the audio input and output to halt, sometime audio input is halted (i.e. no voice is heard on the other end).

I have also tried another techniques to accomplish this task, like:

Technique 2

--------------

#define MM_WOM_FORCESPEAKER (WM_USER+2)

static void SetSpeakerMode(bool speakerphone)
{
MMRESULT result = waveOutMessage((HWAVEOUT)0, MM_WOM_FORCESPEAKER, speakerphone, 0);
if (result != MMSYSERR_NOERROR) {
wchar_t buffer[200];
wsprintf(buffer, L"Could not do speakerphone switch, error=%u", result);
AfxMessageBox(buffer);
}
}

Technique 3

--------------

/* Stuff from Wavedev.h */
#define IOCTL_WAV_MESSAGE 0x001d000c
typedef struct {
UINT uDeviceId;
UINT uMsg;
DWORD dwUser;
DWORD dwParam1;
DWORD dwParam2;
} MMDRV_MESSAGE_PARAMS;
/* End of Wavedev.h extract */

class WavDevice
{
private:
HANDLE hWavDev;

public:
WavDevice()
{
hWavDev = CreateFile(TEXT("WAV1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hWavDev == INVALID_HANDLE_VALUE) {
wchar_t buffer[200];
wsprintf(buffer, L"Could not open device, error=%u", GetLastError());
AfxMessageBox(buffer);
}
}

~WavDevice()
{
if (hWavDev != INVALID_HANDLE_VALUE)
CloseHandle(hWavDev);
}

bool SendMessage(UINT uMsg, DWORD dwParam1 = 0, DWORD dwParam2 = 0)
{
if (hWavDev == INVALID_HANDLE_VALUE)
return false;

DWORD dwRet = 0, dwOut;
MMDRV_MESSAGE_PARAMS mp;
memset(&mp,0,sizeof(mp));
mp.uMsg = uMsg;
mp.dwParam1 = dwParam1;
mp.dwParam2 = dwParam2;
if (DeviceIoControl(hWavDev, IOCTL_WAV_MESSAGE, &mp, sizeof(mp), &dwOut, sizeof(dwOut), &dwRet, 0))
return true;

wchar_t buffer[200];
wsprintf(buffer, L"Could not do speakerphone switch, error=%u", GetLastError());
AfxMessageBox(buffer);
return false;
}
};

static void SetSpeakerMode(bool speakerphone)
{
WavDevice wd;
if (!wd.SendMessage(1002, speakerphone ? 0 : 2))
return;
if (!wd.SendMessage(1012, speakerphone ? 0 : 1))
return;
if (speakerphone && !wd.SendMessage(1013))
return;
wd.SendMessage(1000, speakerphone ? 2 : 4, speakerphone ? 0 : 7);
}

Technique 4

--------------

class OsSvcsDll
{
private:
HMODULE hDLL;
typedef HRESULT (* SetSpeakerModeFn)(DWORD mode);
SetSpeakerModeFn pfnSetSpeakerMode;

public:
OsSvcsDll()
{
hDLL = LoadLibrary(L"\\windows\\ossvcs.dll");
if (hDLL == NULL) {
wchar_t buffer[200];
wsprintf(buffer, L"Could not open DLL, error=%u", GetLastError());
AfxMessageBox(buffer);
}
pfnSetSpeakerMode = (SetSpeakerModeFn)GetProcAddress(hDLL, (LPCTSTR)218);
if (pfnSetSpeakerMode == NULL) {
wchar_t buffer[200];
wsprintf(buffer, L"Could not open DLL, error=%u", GetLastError());
AfxMessageBox(buffer);
}
}

~OsSvcsDll()
{
if (hDLL != NULL)
FreeLibrary(hDLL);
}

bool SetSpeakerMode(DWORD speakerphone)
{
if (pfnSetSpeakerMode == NULL)
return false;

HRESULT result = pfnSetSpeakerMode(speakerphone);
if (result == 0)
return true;

wchar_t buffer[200];
wsprintf(buffer, L"Could not do speakerphone switch, error=0x%x", result);
AfxMessageBox(buffer);
return false;
}
};

static void SetSpeakerMode(bool speakerphone)
{
OsSvcsDll dll;
dll.SetSpeakerMode(speakerphone ? 1 : 0);
}

But no procedure seems to work properly.

Can you please guide me where i am going wrong or some other way of achieving this.

I am using

Visual C++ MFC Dialog based Application

Visual Studio 2008

Windows Mobile 6 SDK For PocketPC

I have tested the above mentioned sample on

HTC Touch

Sony Erricsson XPeria

Waiting for response.

Regards,

Ghazanfar Ali

Regards,

Ghazanfar Ali