📄 simdlg.cpp
字号:
// SIMDlg.cpp : implementation of the CSIMDlg class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "SIMDlg.h"
#include "Transform.h"
#include <atlstr.h>
#include <malloc.h>
#define FILEPATH TEXT("\\Program Files\\SIM_File\\SIM_File.txt") //The file address
#define N 9 //The number of character display in each line
/*********************************************************************************************
* 名称: SimCallbackProc()
* 功能: SIM通告回调函数,响应因SIM的改变而发出的通告
* 参数: dwNotifyCode -- 输入,通告类型代码
* lpData -- 输入,通告类型的数据结构指针
* dwDataSize -- 输入,数据结构长度
* dwParam -- 输入,传递进SimInitialize()的参数
*
* 返回: --- 无
*
*********************************************************************************************/
void SimCallbackProc(DWORD dwNotifyCode, const void *lpData,
DWORD dwDataSize, DWORD dwParam)
{
// Handle a file change
if(dwNotifyCode == SIM_NOTIFY_FILE_REFRESH)
{
SIMFILEREFRESH *pfileRefresh = (SIMFILEREFRESH *)lpData;
TCHAR tchNotifyMessage[1024] = TEXT("\0");
if(!pfileRefresh)
return;
// Handle the file change notification
if(pfileRefresh->dwParams & SIM_PARAM_FILEREFRESH_FILECOUNT)
{
wsprintf(tchNotifyMessage, TEXT("%d files have changed"), pfileRefresh->dwFileCount);
MessageBoxW(NULL, tchNotifyMessage, TEXT("SIM File Change"), MB_OK);
}
// Get the DWORD address of the SIM files that have changed
if(pfileRefresh->dwParams & SIM_PARAM_FILEREFRESH_FILEARRAY)
{
DWORD dwFiles = 0;
for(dwFiles; dwFiles < pfileRefresh->dwFileCount; dwFiles++)
{
wsprintf(tchNotifyMessage, TEXT("File change #%d. File address: %d"), dwFiles,
pfileRefresh->rgdwAddress[dwFiles]);
MessageBoxW(NULL,tchNotifyMessage, TEXT("SIM Files Change"), MB_OK);
}
}
}
return;
}
/*********************************************************************************************
* 名称: ReadSIM()
* 功能: Read the SIM record
* 参数: hWnd --- Handle to the owner window of the message box to be created
* hSim --- Points to a valid HSIM handle
* dwAddress --- A unique ID specifying the name of the SIM record
* dwRecordType --- A SIM_RECORDTYPE constant
* dwIndex --- Item index
* lpData --- Data buffer
* dwBufferSize --- Size of the data buffer
* lpdwBytesRead --- Size of the data buffer
*
* 返回: TRUE --- Success
* SIM_E error --- Fail
*
*********************************************************************************************/
void CSIMDlg::ReadSIM(HSIM hSim, DWORD dwAddress, DWORD dwRecordType, DWORD dwIndex, LPBYTE lpData, DWORD dwBufferSize)
{
//Read a file from the SIM(API)
HRESULT result = S_OK;
DWORD dwBytesRead = 0;
result = SimReadRecord(
hSim,
dwAddress,
dwRecordType,
dwIndex,
lpData,
dwBufferSize,
&dwBytesRead
);
if ( FAILED(result) )
{
ErrorHandle(result);
}
}
/*********************************************************************************************
* 名称: OutPutString_T()
* 功能: Read the record when the type of the record is Transparent
* 参数: listBox --- An object of CListBox
* lpData --- Data buffer
* dwBufferSize --- Size of the data buffer
*
* 返回: file_str --- The content of the file
*
*********************************************************************************************/
CString CSIMDlg::OutPutString_T(CListBox listBox, LPBYTE lpData, DWORD dwBufferSize)
{
CString s = "", file_str = "";;
int nindex = 0;
DWORD i = 0; //Count number
DWORD j = 0;
DWORD k = 0;
//The number of data to display is N
for (i = 0, j = N - 1; i < dwBufferSize - 1; i++, j--)
{
s = s + "0x" + TranToHexStr(lpData[i], 2) + ",";
if (0 == j)
{
nindex = listBox.AddString(s); //Add a string to a list box
listBox.SetCurSel(nindex); //Select a list-box string
file_str += s;
j = N;
s = "";
}
}
s = s + "0x" + TranToHexStr(lpData[dwBufferSize-1], 2);
nindex = listBox.AddString(s); //Add a string to a list box
listBox.SetCurSel(nindex); //Select a list-box string
file_str += s;
s = "";
return file_str;
}
/*********************************************************************************************
* 名称: OutPutString_LC()
* 功能: Read the record when the type of the record is Linear fixed or Cyclic
* 参数: listBox --- An object of CListBox
* lpData --- Data buffer
* dwItemCount --- Number of items in the record
* dwSize --- Size in bytes of each item
*
* 返回: file_str --- The content of the file
*
*********************************************************************************************/
CString CSIMDlg::OutPutString_LC(CListBox listBox, LPBYTE lpData, DWORD dwDownBound, DWORD dwUpBound, DWORD dwSize)
{
CString s = "", file_str = "";
CString CR = "\r\n";
DWORD i = 0, j = 0, k = 0; //Count number
int nindex = 0;
TCHAR index[2];
memset(index, 0 ,sizeof(index));
for (i = dwDownBound; i <= dwUpBound; i++)
{
wsprintf(index, TEXT("%d-"),i);
s = index + s;
for (j = 0, k = N - 1; j < dwSize - 1; j++, k--)
{
s = s + "0x" + TranToHexStr(lpData[dwSize*i+j], 2) + ",";
if (0 == k)
{
nindex = listBox.AddString(s);
listBox.SetCurSel(nindex);
file_str += s;
k = N;
s = "";
}
}
s = s + "0x" + TranToHexStr(lpData[dwSize*i+dwSize-1], 2);
nindex = listBox.AddString(s);
listBox.SetCurSel(nindex);
file_str += s;
file_str += CR;
s = "";
}
return file_str;
}
/*********************************************************************************************
* 名称: OnInitDialog()
* 功能: 初始化对话框、SIM卡
* 参数: -- 无
*
* 返回: TRUE --- Success
* SIM_E error --- Fail
*
*********************************************************************************************/
LRESULT CSIMDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();
// set icons
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
SetIcon(hIconSmall, FALSE);
SHINITDLGINFO shidi;
SecureZeroMemory(&shidi, sizeof(shidi));
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_SIZEDLGFULLSCREEN;
shidi.dwFlags = SHIDIF_SIZEDLG;
shidi.hDlg = m_hWnd;
if( !SHInitDialog(&shidi) )
{
EndDialog(0);
}
//Initialize the variable
m_lpData = NULL;
m_dwFileSize = 0;
iTag = 0;
myTag = 0;
m_dwAddress = 0;
m_predwAddress = 0;
//Create a MenuBar.
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = m_hWnd;
mbi.hInstRes = _Module.GetModuleInstance();
mbi.nToolBarId = IDR_MENU;
mbi.dwFlags = SHCMBF_HMENU ;
// Create bar and check for errors.
if (!SHCreateMenuBar(&mbi))
{
return FALSE;
}
//Initialize the SIM card and register the call back function(API)
m_result = SimInitialize(
SIM_INIT_SIMCARD_NOTIFICATIONS,
SimCallbackProc,
0,
&m_hSim);
if ( FAILED(m_result) )
{
ErrorHandle(m_result);
}
memset(&m_simcaps, 0, sizeof(SIMCAPS));
m_simcaps.cbSize = sizeof(SIMCAPS);
//Get the device capabilities of the SIM(API)
m_result = SimGetDevCaps(
m_hSim,
SIM_CAPSTYPE_ALL,
&m_simcaps);
if ( FAILED(m_result) )
{
ErrorHandle(m_result);
}
m_hFile = NULL; //An open handle to the specified file
m_lpFileID = FILEPATH; //Pointer to a null-terminated string that specifies the name of the object
DWORD cbTmp = 0;
CString tmp_str = "";
char *tmp = NULL;
m_hFile = CreateFile(
m_lpFileID,
GENERIC_WRITE,
0,
NULL,
CREATE_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, tmp_str, -1, tmp, 0, NULL, NULL);
//Writes data to a file
WriteFile(m_hFile, tmp, 0, &cbTmp, NULL);
CloseHandle(m_hFile);
return TRUE;
}
/*********************************************************************************************
* 名称: onMenuReadSim()
* 功能: 当点击菜单中的“Read SIM”时,执行该函数
* 参数: -- 无
*
* 返回: -- 无
*
*********************************************************************************************/
LRESULT CSIMDlg::onMenuReadSim(UINT /*uMsg*/, WPARAM /*wParam*/, HWND /*lParam*/, BOOL& /*bHandled*/)
{
// TODO: Add your control notification handler code here
TCHAR up[5], down[5] ,sum[5];
DWORD i = 0, j = 0, k = 0, t = 0; //Count number
DWORD numUp = 0, numDown = 0, numSum = 0; //The bound number which has been transformed to DEC
memset(up, 0, sizeof(up));
memset(down, 0, sizeof(down));
memset(sum, 0, sizeof(sum));
CString CR = "\r\n";
CString s = "";
LPBYTE sc = NULL;
m_csFileStr = "";
m_csFileName = "";
if (m_lpData)
{
free(m_lpData);
}
m_lpData = NULL;
CListBox listBox; //declare an object of CListBox Class
memset(&listBox, 0, sizeof(listBox));
listBox.Attach(GetDlgItem(IDC_LIST));
listBox.ResetContent(); //Clears all the entries from a list box
int nindex = 0; //The zero-based index to the string in the list box
//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);
if (m_predwAddress == m_dwAddress)
{
iTag = 1;
}
if (!iTag)
{
MessageBoxW(_T("Please click 'Get Info' first!"), _T("Error"), MB_OK|MB_ICONERROR);
return 0;
}
for (i = 0; i < 4; i++)
{
m_csFileName += m_tcFileID[i];
}
m_dwFileIDLen = m_csFileName.GetLength();
//Get the up bound and down bound
GetDlgItemText(
IDC_EDIT_UP,
up,
5);
GetDlgItemText(
IDC_EDIT_DOWN,
down,
5);
//Choose appropriate processing according to the information gotten from the SimGetRecordInfo()
if (SIM_RECORDTYPE_TRANSPARENT == m_dwRecordType)
{
//If the file type is transparent(binary),the bound should not be supplied
if ( (up[0] != NULL) || (down[0] != NULL) )
{
MessageBoxW(_T("Be sure to read a transparent(binary) file!"), _T("Error"), MB_OK|MB_ICONERROR);
return 0;
}
else
{
//In the transparent file,the item size is the file size
m_dwBufferSize = m_dwSize;
m_lpData = (BYTE *)malloc(m_dwBufferSize);
memset(m_lpData, 0, m_dwBufferSize);
SetCursor(LoadCursor(NULL, IDC_WAIT));
ReadSIM(
m_hSim,
m_dwAddress,
m_dwRecordType,
NULL,
m_lpData,
m_dwBufferSize
);
m_csFileStr = OutPutString_T(listBox, m_lpData, m_dwBufferSize);
SetCursor(LoadCursor(NULL, IDC_NO));
}
}
else
{
//When the bounds are not given,then read the whole file
if ( (up[0] == NULL) && (down[0] == NULL) )
{
m_dwBufferSize = m_dwItemCount * m_dwSize;
m_lpData = (BYTE *)malloc(m_dwBufferSize);
memset(m_lpData, 0, m_dwBufferSize);
SetCursor(LoadCursor(NULL, IDC_WAIT));
for (i =0, j = 0; i < m_dwItemCount; i++, j +=m_dwSize )
{
ReadSIM(
m_hSim,
m_dwAddress,
m_dwRecordType,
i+1,
&m_lpData[j],
m_dwSize
);
}
m_csFileStr = OutPutString_LC(listBox,m_lpData, 1, m_dwItemCount, m_dwSize);
SetCursor(LoadCursor(NULL, IDC_NO));
}
else
{
//When only one bound is given,then treat the bound as an index to read the record
if ( (up[0] == NULL) || (down[0] == NULL) )
{
numSum = StrToDec(up) + StrToDec(down);
if ( (numSum <= 0) || (numSum > m_dwItemCount) )
{
MessageBoxW(_T("The wrong bound number!"), _T("Error"), MB_OK|MB_ICONERROR);
return 0;
}
m_dwBufferSize = m_dwSize;
m_lpData = (BYTE *)malloc(m_dwBufferSize);
memset(m_lpData, 0, m_dwBufferSize);
SetCursor(LoadCursor(NULL, IDC_WAIT));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -