在一个工程里,我有多个工站,几个相同的处理,如果直接复制,会提示多重定义。可用static
1.在源文件中,不对外提供的函数,定义时都用static修饰,将该函数的链接属性限定在本文件;
2.函数命名时,在每个函数的名字前都加上库的名称,一般库的名字是唯一且不重合的;
3.全局变量如果只在本文件中使用,最好也使用static修饰;
BOOL getDataFromDevice()
{
CString sn;
CString csImei1, csImei2;
int ret = halGetSn(sn.GetBuffer(32));
sn.ReleaseBuffer();
if (ret != 0) { pWnd->ShowInfo(("获取设备sn 错误码 " + to_string(ret)).c_str()); return FALSE; }
ret = getAllImei(csImei1, csImei2);
if (ret != 0) { pWnd->ShowInfo(("读IMEI失败 错误码 " + to_string(ret)).c_str()); return FALSE; }
pWnd->GetDlgItem(IDC_EDIT_SNsn)->SetWindowText(sn);
pWnd->GetDlgItem(IDC_EDIT_IMEI1)->SetWindowText(csImei1);
pWnd->GetDlgItem(IDC_EDIT_IMEI2)->SetWindowText(csImei2);
SetClipboard(sn + "\r\n" + csImei1 + "\r\n" + csImei2);
return TRUE;
}
static BOOL ConnectDevice()
{
for (int i = 0; i < 60; i++)
{
pWnd->displayDeviceState(i + 1);
int ret = halConnectDevice();
if (ret == 0)
{
pWnd->displayDeviceState(0);
return TRUE;
}
else
{
logger.INFO_F("连接次数" + to_string(i + 1) + " halConnectDevice ret=" + to_string(ret) + " " + errArray[(abs(ret) >= Size(errArray) ? Size(errArray) - 1 : abs(ret))]);
if (i % 3 == 0)
{
pipe_system("adb.exe devices\n"); //看看设备是否存在
pipe_system("adb.exe kill-server\n");
pipe_system("adb.exe shell am force-stop com.ftsafe.cit\n");
pipe_system("adb.exe shell am force-stop com.ftpos.productiontoolandroid");
pipe_system("adb.exe shell am startservice -n com.ftpos.productiontoolandroid/com.ftpos.productiontoolandroid.ServiceManager -a com.ftpos.productiontoolandroid");
}
else
{
Sleep(DELAY);
}
}
}
pWnd->displayDeviceState(-1);
return FALSE;
}
BOOL myprocGetDeviceInfo()
{
if (ConnectDevice())
{
return getDataFromDevice();
}
return FALSE;
}
static UINT procCommon(LPVOID lParam)
{
if (g_bThreadFlag) { pWnd->ShowInfo("There are unfinished tasks"); return -1; }
g_bThreadFlag = TRUE;
logger.INFO_F(FUN_LINE" start ************************************************************");
DWORD start = GetTickCount();
pWnd->m_staResult.SetWindowText("");
pWnd->m_editDisplay.SetWindowText("");
pWnd->GetDlgItem(IDC_EDIT_SNsn)->SetWindowText("");
pWnd->GetDlgItem(IDC_EDIT_IMEI1)->SetWindowText("");
pWnd->GetDlgItem(IDC_EDIT_IMEI2)->SetWindowText("");
typedef BOOL(*pMethod)(); //定义一种函数指针类型
pMethod method = (pMethod)lParam; //强制转换为该类型
pWnd->ShowSetResult(method()); //调用
pWnd->ShowInfo(("耗时:" + to_string(GetTickCount() - start) + "ms").c_str());
logger.INFO_F(FUN_LINE" end ************************************************************");
g_bThreadFlag = FALSE;
return 0;
}
void PrintAllDlg::OnBnClickedButtonGetdata()
{
AfxBeginThread(procCommon, myprocGetDeviceInfo);
}