⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simdlg.cpp

📁 实现了读SIM卡文件的功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				ReadSIM(
					m_hSim,
					m_dwAddress,
					m_dwRecordType,
					numSum,
					m_lpData,
					m_dwBufferSize
					);
				m_csFileStr = OutPutString_T(listBox, m_lpData, m_dwBufferSize);
				SetCursor(LoadCursor(NULL, IDC_NO));
			} 
			else
			{
				//Both the up bound and down bound are supplied
				numUp = StrToDec(up);
				numDown = StrToDec(down);
				//The item_count should between the he up bound and down bound
				if ( (numDown > numUp) || (m_dwItemCount < numUp) || (m_dwItemCount < numDown) ||
					 (numUp <= 0) || (numDown <= 0) )
				{
					MessageBoxW(_T("The wrong bound!"), _T("Error"),  MB_OK|MB_ICONERROR);
					return 0;
				}

				m_dwBufferSize = (numUp-numDown+1) * m_dwSize;
				m_lpData = (BYTE *)malloc(m_dwBufferSize);
				memset(m_lpData, 0, m_dwBufferSize);

				SetCursor(LoadCursor(NULL, IDC_WAIT));
				for (i = numDown, j = 0; i <= numUp; i++, j +=m_dwSize)
				{
					ReadSIM(
						m_hSim, 
						m_dwAddress, 
						m_dwRecordType, 
						i, 
						&m_lpData[j], 
						m_dwSize
						);
				}

				m_csFileStr = OutPutString_LC(listBox, m_lpData, numDown, numUp, m_dwSize);
				SetCursor(LoadCursor(NULL, IDC_NO));
			}
		}
	}
	m_csFileStr = ":" + CR + m_csFileStr +CR;
	m_dwFileSize = m_csFileStr.GetLength();
	m_predwAddress = m_dwAddress;
	myTag = 1;
	iTag = 0;
	return 0;
}

/*********************************************************************************************
* 名称:	OnBnClickedButtonSave()
* 功能:	当点击按钮“Save File”时,执行该函数
* 参数:	-- 无
*
* 返回:	-- 无
* 
*********************************************************************************************/

LRESULT CSIMDlg::OnBnClickedButtonSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add your control notification handler code here

	TCHAR tmp[1];
	DWORD cbMultiByte1 = 0;	//Number of bytes written
	DWORD cbMultiByte2 = 0;
	char *tmp_file1 = NULL;
	char *tmp_file2 = NULL;

	tmp_file1 = (char *)malloc(m_dwFileIDLen);
	memset(tmp_file1, 0, m_dwFileIDLen);

	tmp_file2 = (char *)malloc(m_dwFileSize);
	memset(tmp_file2, 0, m_dwFileSize);

	wsprintf(tmp, TEXT(""));

	memset(m_tcFileID, 0, sizeof(m_tcFileID));
	GetDlgItemText(
		IDC_EDIT_ID,
		m_tcFileID,
		5);

	if (!m_tcFileID[0])
	{
		SetDlgItemText(
			IDC_EDIT_ATTRIBUTE,
			tmp);
		SetDlgItemText(
			IDC_EDIT_FSIZE,
			tmp);
		SetDlgItemText(
			IDC_EDIT_COUNT,
			tmp);
		SetDlgItemText(
			IDC_EDIT_ISIZE,
			tmp);
		SetDlgItemText(
			IDC_EDIT_UP,
			tmp);
		SetDlgItemText(
			IDC_EDIT_DOWN,
			tmp);
		MessageBoxW(_T("Please input the file ID"), _T("Error"),  MB_OK|MB_ICONERROR);
		return 0;
	}

	if (!myTag)
	{
		return 0;
	}

	// Create a file
	m_hFile = CreateFile(
		m_lpFileID, 
		GENERIC_WRITE, 
		0, 
		NULL, 
		OPEN_ALWAYS, 
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
		NULL);

	if (INVALID_HANDLE_VALUE == m_hFile)
	{
		MessageBoxW(_T("Can not build the file"), _T("Error"),  MB_OK|MB_ICONERROR);
		return 0;
	}

	//Maps a wide-character string to a new character string(API)
	WideCharToMultiByte(CP_ACP, 0, m_csFileName, -1, tmp_file1, m_dwFileIDLen, NULL, NULL);
	WideCharToMultiByte(CP_ACP, 0, m_csFileStr, -1, tmp_file2, m_dwFileSize, NULL, NULL);

	//Writes data to a file
	SetFilePointer(m_hFile, 0, NULL, FILE_END);
	WriteFile(m_hFile, tmp_file1, m_dwFileIDLen, &cbMultiByte1, NULL);
	WriteFile(m_hFile, tmp_file2, m_dwFileSize, &cbMultiByte2, NULL);
	myTag = 0;

	m_csFileName = "";
	free(tmp_file1);
	tmp_file1 = NULL;

	m_csFileStr = "";
	free(tmp_file2);
	tmp_file2 = NULL;

	if (m_lpData)
	{
		free(m_lpData);
	}
	m_lpData = NULL;

	CloseHandle(m_hFile);
	return 0;
}

/*********************************************************************************************
* 名称:	OnBnClickedButtonInfo()
* 功能:	当点击按钮“Get Info”时,执行该函数
* 参数:	-- result:各API函数执行返回值
*
* 返回:	-- 无
* 
*********************************************************************************************/

LRESULT CSIMDlg::OnBnClickedButtonInfo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add your control notification handler code here
	TCHAR tmp[1];
	memset(tmp, 0, sizeof(tmp));
	memset(m_tcFileID, 0, sizeof(m_tcFileID));

	//Retrieves the file ID associated with a control in a dialog box(API)
	GetDlgItemText(
		IDC_EDIT_ID,
		m_tcFileID,
		5);

	//To check whether the file ID is correct or not
	for(int i = 0; i < 4; i++)
	{
		if( !(( (m_tcFileID[i] >= '0') && (m_tcFileID[i] <= '9') ) || 
			( (m_tcFileID[i] >= 'A') && (m_tcFileID[i] <= 'F') ) ) )

		{
			MessageBoxW(_T("The file name is not correct!"), _T("Error"),  MB_OK|MB_ICONERROR);
			return 0;
		}
	}

	//Transform the HEX string to DEC
	m_dwAddress = HexStrToDec(m_tcFileID);

	memset(&m_SimRecordInfo, 0, sizeof(m_SimRecordInfo));
	m_SimRecordInfo.cbSize = sizeof(SIMRECORDINFO);

	//Get information about a particular record(API)
	m_result = SimGetRecordInfo(
		m_hSim,
		m_dwAddress,
		&m_SimRecordInfo
		);

	if ( FAILED(m_result) )
	{
		ErrorHandle(m_result);
	}

	iTag = 1;
	m_dwRecordType = m_SimRecordInfo.dwRecordType;
	m_dwItemCount = m_SimRecordInfo.dwItemCount;	//Number of items in the record
	m_dwSize = m_SimRecordInfo.dwSize;	//Size in bytes of each item

	if (SIM_RECORDTYPE_TRANSPARENT == m_dwRecordType)
	{
		SetDlgItemText(
			IDC_EDIT_ATTRIBUTE,
			L"Transparent"
			);

		wsprintf(tmp, TEXT("%d"),m_dwSize);
		SetDlgItemText(
			IDC_EDIT_FSIZE,
			tmp);

		wsprintf(tmp, TEXT(""));
		SetDlgItemText(
			IDC_EDIT_COUNT,
			tmp);

		SetDlgItemText(
			IDC_EDIT_ISIZE,
			tmp);

		SetDlgItemText(
			IDC_EDIT_UP,
			tmp);

		SetDlgItemText(
			IDC_EDIT_DOWN,
			tmp);
	} 
	else
	{
		if (SIM_RECORDTYPE_CYCLIC == m_dwRecordType)
		{
			SetDlgItemText(
				IDC_EDIT_ATTRIBUTE,
				L"Cyclic"
				);
		} 
		else
		{
			SetDlgItemText(
				IDC_EDIT_ATTRIBUTE,
				L"Linear fixed"
				);
		}

		wsprintf(tmp, TEXT("%d"), m_dwItemCount);
		SetDlgItemText(
			IDC_EDIT_COUNT,
			tmp);

		wsprintf(tmp, TEXT("%d"), m_dwSize);
		SetDlgItemText(
			IDC_EDIT_ISIZE,
			tmp);

		wsprintf(tmp, TEXT("%d"),(m_dwItemCount * m_dwSize));
		SetDlgItemText(
			IDC_EDIT_FSIZE,
			tmp);
	}
	return 0;
}

/*********************************************************************************************
* 名称:	OnMenuExit()
* 功能:	当点击菜单中的“Exit”时,执行该函数
* 参数:	-- 无
*
* 返回:	-- 无
* 
*********************************************************************************************/

LRESULT CSIMDlg::OnMenuExit(UINT /*uMsg*/, WPARAM /*wParam*/, HWND /*lParam*/, BOOL& /*bHandled*/)
{
	if (m_lpData)
	{
		free(m_lpData);
	}
	m_lpData = NULL;

	if(m_hSim)
		SimDeinitialize(m_hSim);

	EndDialog(0);

	return 0;
}

/*********************************************************************************************
* 名称:	ErrorHandle()
* 功能:	出错处理函数
* 参数:	-- result:各API函数执行返回值
*
* 返回:	-- 无
* 
*********************************************************************************************/

void CSIMDlg::ErrorHandle(HRESULT result)
{
	switch(result)
	{
	case SIM_E_SIMFAILURE:
		MessageBoxW(_T("SIM_E_SIMFAILURE: SIM failure was detected"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMBUSY:
		MessageBoxW(_T("SIM_E_SIMBUSY: SIM is busy"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMWRONG:
		MessageBoxW(_T("SIM_E_SIMWRONG: Inorrect SIM was inserted"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NOSIMMSGSTORAGE:
		MessageBoxW(_T("SIM_E_NOSIMMSGSTORAGE: SIM isn't capable of storing messages"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMTOOLKITBUSY:
		MessageBoxW(_T("SIM_E_SIMTOOLKITBUSY: SIM Application Toolkit is busy"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMDOWNLOADERROR:
		MessageBoxW(_T("SIM_E_SIMDOWNLOADERROR: SIM data download error"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMNOTINSERTED:
		MessageBoxW(_T("SIM_E_SIMNOTINSERTED: SIM isn't inserted into the phone"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_PHSIMPINREQUIRED :
		MessageBoxW(_T("SIM_E_PHSIMPINREQUIRED: PH-SIM PIN is required to perform this operation"), _T("Error"),    MB_OK|MB_ICONERROR);
		break;
	case SIM_E_PHFSIMPINREQUIRED:
		MessageBoxW(_T("SIM_E_PHFSIMPINREQUIRED: PH-FSIM PIN is required to perform this operation"), _T("Error"),    MB_OK|MB_ICONERROR);
		break;
	case SIM_E_PHFSIMPUKREQUIRED:
		MessageBoxW(_T("SIM_E_PHFSIMPUKREQUIRED: PH-FSIM PUK is required to perform this operation"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	//case SIM_E_SIMPINREQUIRED:
	//	MessageBoxW(_T("SIM_E_SIMPINREQUIRED: SIM PIN is required to perform this operation"), _T("Error"),   MB_OK|MB_ICONERROR);
	//	break;
	case SIM_E_SIMPUKREQUIRED:
		MessageBoxW(_T("SIM_E_SIMPUKREQUIRED: SIM PUK is required to perform this operation"), _T("Error"),    MB_OK|MB_ICONERROR);
		break;
	case SIM_E_INCORRECTPASSWORD:
		MessageBoxW(_T("SIM_E_INCORRECTPASSWORD: Incorrect password was supplied"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMPIN2REQUIRED :
		MessageBoxW(_T("SIM_E_SIMPIN2REQUIRED : SIM PIN2 is required to perform this operation"), _T("Error"),    MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMPUK2REQUIRED:
		MessageBoxW(_T("SIM_E_SIMPUK2REQUIRED: SIM PUK2 is required to perform this operation"), _T("Error"),    MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NETWKPINREQUIRED:
		MessageBoxW(_T("SIM_E_NETWKPINREQUIRED: Network Personalization PIN is required to perform this operation"), _T  ("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NETWKPUKREQUIRED:
		MessageBoxW(_T("SIM_E_NETWKPUKREQUIRED: Network Personalization PUK is required to perform this operation"), _T  ("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SUBSETPINREQUIRED:
		MessageBoxW(_T("SIM_E_SUBSETPINREQUIRED: Network Subset Personalization PIN is required to perform this operation"),   _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SUBSETPUKREQUIRED:
		MessageBoxW(_T("SIM_E_SUBSETPUKREQUIRED: Network Subset Personalization PUK is required to perform this operation"),   _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SVCPINREQUIRED:
		MessageBoxW(_T("SIM_E_SVCPINREQUIRED: Service Provider Personalization PIN is required to perform this operation"), _T  ("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SVCPUKREQUIRED :
		MessageBoxW(_T("SIM_E_SVCPUKREQUIRED : Service Provider Personalization PUK is required to perform this operation"),   _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_CORPPINREQUIRED:
		MessageBoxW(_T("SIM_E_CORPPINREQUIRED: Corporate Personalization PIN is required to perform this operation"), _T  ("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_CORPPUKREQUIRED :
		MessageBoxW(_T("SIM_E_CORPPUKREQUIRED : Corporate Personalization PUK is required to perform this operation"), _T  ("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_MEMORYFULL:
		MessageBoxW(_T("SIM_E_MEMORYFULL: Storage memory is full"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_INVALIDINDEX:
		MessageBoxW(_T("SIM_E_INVALIDINDEX: Invalid storage index was supplied"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NOTFOUND:
		MessageBoxW(_T("SIM_E_NOTFOUND: A requested storage entry was not found"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_MEMORYFAILURE:
		MessageBoxW(_T("SIM_E_MEMORYFAILURE: Storage memory failure"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SIMMSGSTORAGEFULL:
		MessageBoxW(_T("SIM_E_SIMMSGSTORAGEFULL: Message sto age on the SIM is full"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_EMPTYINDEX:
		MessageBoxW(_T("SIM_E_EMPTYINDEX: Storage location is empty"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_TEXTSTRINGTOOLONG:
		MessageBoxW(_T("SIM_E_TEXTSTRINGTOOLONG: Supplied text string is too long"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_DIALSTRINGTOOLONG:
		MessageBoxW(_T("SIM_E_DIALSTRINGTOOLONG: Supplied dial string contains invalid characters"), _T("Error"), MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NOTREADY:
		MessageBoxW(_T("SIM_E_NOTREADY: SIM isn't yet ready to perform the requested operation"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_SECURITYFAILURE:
		MessageBoxW(_T("SIM_E_SECURITYFAILURE: SIM isn't yet ready to perform the requested operation"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_BUFFERTOOSMALL:
		MessageBoxW(_T("SIM_E_BUFFERTOOSMALL: bufer too small"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NOTTEXTMESSAGE:
		MessageBoxW(_T("SIM_E_NOTTEXTMESSAGE: Requested SMS message is not a text message"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NOSIM:
		MessageBoxW(_T("SIM_E_NOSIM: Device doesn't have a SIM"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_NETWORKERROR:
		MessageBoxW(_T("SIM_E_NETWORKERROR: There was a network error"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_MOBILEERROR:
		MessageBoxW(_T("SIM_E_MOBILEERROR: Mobile error"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_UNSUPPORTED:
		MessageBoxW(_T("SIM_E_UNSUPPORTED: The command is unsupported"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_BADPARAM :
		MessageBoxW(_T("SIM_E_BADPARAM: Bad parameter"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_UNDETERMINED :
		MessageBoxW(_T("SIM_E_UNDETERMINED:  Undetermined error"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_RADIONOTPRESENT :
		MessageBoxW(_T("SIM_E_RADIONOTPRESENT: The Radio is not present"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	case SIM_E_RADIOOFF  :
		MessageBoxW(_T("SIM_E_RADIOOFF: The Radio is off"), _T("Error"),  MB_OK|MB_ICONERROR);
		break;
	default:
		MessageBoxW(_T("Unknown Error"), _T("Error"), MB_OK|MB_ICONERROR);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -