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

I'm trying to turn the phone device on a windows mobile 5/6 PPC on and off using this code:

PhoneDevice.h

#pragma once

#include <tapi.h>
#include <tsp.h>
#include <extapi.h>

#define TAPI_API_LOW_VERSION	0x00020000
#define TAPI_API_HIGH_VERSION	0x00020000
#define EXT_API_LOW_VERSION	0x00010000
#define EXT_API_HIGH_VERSION	0x00010000

#define PHONEDEVICE_ERROR	0x00000000
#define PHONEDEVICE_ERROR_SUCCESS	0x00000001
#define PHONEDEVICE_ERROR_LINEOPEN	0x00000002
#define PHONEDEVICE_ERROR_LINECLOSE	0x00000004
#define PHONEDEVICE_ERROR_GETSTATE	0x00000008
#define PHONEDEVICE_ERROR_SETSTATE	0x00000010
#define PHONEDEVICE_ERROR_UNREGISTER	0x00000020
#define PHONEDEVICE_ERROR_REGISTER	0x00000040

DWORD setPhoneStateEx(DWORD state);
DWORD GetTSPLineDeviceID(const HLINEAPP hLineApp, const DWORD dwNumberDevices, const DWORD dwAPIVersionLow, const DWORD dwAPIVersionHigh, const TCHAR* const psTSPLineName);

PhoneDevice.cpp

#include "PhoneDevice.h"

DWORD setPhoneStateEx(DWORD state)
{
DWORD dwNumDevs;
LINEINITIALIZEEXPARAMS liep;
liep.dwTotalSize = sizeof(liep);
liep.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT;
HLINEAPP hLineApp = 0;
HLINE hLine = 0;
DWORD dwAPIVersion = TAPI_API_HIGH_VERSION;
LONG lRC = 0;
DWORD dwState, dwRadioSupport;
TCHAR gszFriendlyAppName[160];

if(lineInitializeEx(&hLineApp, 0, 0, gszFriendlyAppName, &dwNumDevs, &dwAPIVersion, &liep)) {
return PHONEDEVICE_ERROR;
}

DWORD dwDeviceID = GetTSPLineDeviceID(hLineApp, dwNumDevs, TAPI_API_LOW_VERSION, TAPI_API_HIGH_VERSION, CELLTSP_LINENAME_STRING);

lRC = lineOpen(	hLineApp, dwDeviceID, &hLine, dwAPIVersion, 0, NULL, LINECALLPRIVILEGE_OWNER, LINEMEDIAMODE_DATAMODEM, NULL);

if(lRC < 0) {
if(hLine != NULL) {
lineClose(hLine);
hLine = NULL;
}
return PHONEDEVICE_ERROR_LINEOPEN;
}

lRC = lineGetEquipmentState(hLine, &dwState, &dwRadioSupport);

if(lRC < 0) {
lineClose(hLine);
return PHONEDEVICE_ERROR_GETSTATE;
}

if((LINEEQUIPSTATE_MINIMUM == state) || (LINEEQUIPSTATE_NOTXRX == state)) {
lRC = lineUnregister(hLine);
if(lRC < 0) {
lineClose(hLine);
return PHONEDEVICE_ERROR_UNREGISTER;
}
}

if(dwState != state) {
lRC = lineSetEquipmentState(hLine, state);
}

if(lRC < 0) {
lineClose(hLine);
return PHONEDEVICE_ERROR_SETSTATE;
}

if((LINEEQUIPSTATE_FULL == state) || (LINEEQUIPSTATE_RXONLY == state) || (LINEEQUIPSTATE_TXONLY == state)) {
lRC = lineRegister(hLine, LINEREGMODE_AUTOMATIC, NULL, LINEOPFORMAT_NONE);
if(lRC < 0) {
lineClose(hLine);
return PHONEDEVICE_ERROR_REGISTER;
}
}

lRC = lineClose(hLine);

return PHONEDEVICE_ERROR_SUCCESS;
}

DWORD GetTSPLineDeviceID(const HLINEAPP hLineApp, const DWORD dwNumberDevices, const DWORD dwAPIVersionLow, const DWORD dwAPIVersionHigh, const TCHAR* const psTSPLineName)
{
DWORD dwReturn = 0xffffffff;
for(DWORD dwCurrentDevID = 0 ; dwCurrentDevID < dwNumberDevices ; dwCurrentDevID++)
{
DWORD dwAPIVersion;
LINEEXTENSIONID LineExtensionID;
if(0 == lineNegotiateAPIVersion(hLineApp, dwCurrentDevID,
dwAPIVersionLow, dwAPIVersionHigh,
&dwAPIVersion, &LineExtensionID))
{
LINEDEVCAPS LineDevCaps;
LineDevCaps.dwTotalSize = sizeof(LineDevCaps);
if(0 == lineGetDevCaps(hLineApp, dwCurrentDevID,
dwAPIVersion, 0, &LineDevCaps))
{
BYTE* pLineDevCapsBytes = new BYTE[LineDevCaps.dwNeededSize];
if(0 != pLineDevCapsBytes)
{
LINEDEVCAPS* pLineDevCaps = (LINEDEVCAPS*)pLineDevCapsBytes;
pLineDevCaps->dwTotalSize = LineDevCaps.dwNeededSize;
if(0 == lineGetDevCaps(hLineApp, dwCurrentDevID,
dwAPIVersion, 0, pLineDevCaps))
{
if(0 == _tcscmp((TCHAR*)((BYTE*)pLineDevCaps+pLineDevCaps->dwLineNameOffset),
psTSPLineName))
{
dwReturn = dwCurrentDevID;
}
}
delete[] pLineDevCapsBytes;
}
}
}
}
return dwReturn;
}

The functions are called like this:

switch (state) {
case ON:
return setPhoneStateEx(LINEEQUIPSTATE_FULL);
break;
case OFF:
return setPhoneStateEx(LINEEQUIPSTATE_MINIMUM);
break;
}

But on the Device emulator this doesn't work. It allways returns PHONEDEVICE_ERROR.

Could someone please tell me what i'm doing wrong?

I've been trying to get this to work for 2 months now, but still no luck :(

Thanks in advanced,

Smokeman