Debug Assertion Faild
...
File f:\dd\vctools\crt_bld\self_x86\crt\src\memcpy_s.c
Line:62
Expression:sizeInBytes >= count
请问这是怎么回事呀?
14 个解决方案
#1
你准备拷贝一个大块数据到一个小空间 造成越界访问或缓冲区溢出?
#2
我也不清楚,请问应该怎么改呀?
#3
点击重试,看堆栈回退到你代码处,
提示的错误就是说你的目标区域不够大 或是准备拷贝的数据太长了
提示的错误就是说你的目标区域不够大 或是准备拷贝的数据太长了
#4
static void __cdecl CopyChars
( _Out_cap_post_count_(nDestLen,nChars) XCHAR* pchDest, _In_ size_t nDestLen, _In_opt_count_(nChars) const XCHAR* pchSrc, _In_ int nChars ) throw()
{
memcpy_s( pchDest, nDestLen*sizeof( XCHAR ), //在这有断点
pchSrc, nChars*sizeof( XCHAR ) );
}
应该怎么改呢?
#5
memcpy_s( pchDest, nDestLen*sizeof( XCHAR ), //在这有断点
pchSrc, min(nDestLen-1, nChars)*sizeof( XCHAR ) );
这样不会报错,但是字符串将不能完整拷贝
最好是把目标缓冲区改大
pchSrc, min(nDestLen-1, nChars)*sizeof( XCHAR ) );
这样不会报错,但是字符串将不能完整拷贝
最好是把目标缓冲区改大
#6
请问下具体应该怎么改呀?是把定义的数组改大么?
#7
是
#8
我把定义的BUF数组,和其他数组都改大了,但还是有这个问题
#9
你跟踪一下,在这个断点处,你看各参数是不是正确,不是长度这个参数超过了这个memcpy_s所要求的长度,就是如果memcpy_s这个长度的类型是short,那么,你长度的参数就不能大于65535.
另外,你最好不要改这个memcpy_s.c文件里的代码,你改了,把程序拿到其它计算机上编绎同样会出问题,因为别人的计算机里的这个文件没改.除非这个文件是你编写的才可以改
另外,你最好不要改这个memcpy_s.c文件里的代码,你改了,把程序拿到其它计算机上编绎同样会出问题,因为别人的计算机里的这个文件没改.除非这个文件是你编写的才可以改
#10
我把和数组有关的都发上来,能帮忙看一下吗?谢谢啦
CGraduationDlg* pDlg;
BYTE bs[50]={0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1};
unsigned char B1[4],B2[4];
HANDLE wait_handle;
CString strOldIP="";
int i;
CGraduationDlg::CGraduationDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGraduationDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//---------------------------------------------
pDlg=this;
if(!m_UDPSocket.Create(myPORT,SOCK_DGRAM))
{
AfxMessageBox("Failed Create Socket");
}
//---------------------------------------------
}
BOOL CGraduationDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
wait_handle=CreateEvent(NULL,true,false,"receive data");
m_spin.SetRange(100,10000);
m_spin.SetPos(100);
// -----------------ListView initialize---------------------
DWORD dwStyle=GetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE);
dwStyle&=~LVS_TYPEMASK;
dwStyle|=LVS_REPORT;
SetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE,dwStyle);
m_ListView.InsertColumn(0,"MAC地址",LVCFMT_LEFT,135);
m_ListView.InsertColumn(0,"IP地址",LVCFMT_LEFT,85);
m_ListView.SetExtendedStyle(LVS_EX_GRIDLINES);
::SendMessage(m_ListView.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
//----------------------------------------------------------
GetAdapterInfo(); // 得到适配器信息
// 添加到ComboBox
for( size_t i = 0; i < AdapterInfoVector.size(); i++ )
m_ctlAdapters.AddString(AdapterInfoVector[i]->strDriverDesc.c_str());
m_ctlAdapters.SetCurSel(0);
OnSelchangeAdapter();
return TRUE; // return TRUE unless you set the focus to a control
}
UINT GraduationThread(LPVOID param)
{
do
{
pDlg->m_strIP.Format("%d.%d.%d.%d",B1[0],B1[1],B1[2],B1[3]);
pDlg->m_ListBox.AddString(pDlg->m_strIP);
if(B1[3]!=0&&B1[2]!=0)
pDlg->m_UDPSocket.SendTo((void*)bs,50,destPORT,pDlg->m_strIP,0);
int nWait=pDlg->m_spin.GetPos();
WaitForSingleObject(
wait_handle, // handle to object to wait for
nWait // time-out interval in milliseconds
);
ResetEvent(wait_handle);
//=============================================
if(B1[2]<=B2[2])
{
if(B1[3]<B2[3])B1[3]++;
else if(B1[2]<B2[2]&&B1[3]<255)B1[3]++;
else if(B1[2]<B2[2]&&B1[3]==255)
{
B1[3]=0;
B1[2]++;
}
}
else break;
if(B1[3]>=B2[3]&&B1[2]>=B2[2])break;
}while(B1[2]<=255&&B1[3]<=255);
pDlg->m_ListBox.AddString("-----OK!-----");
pDlg->GetDlgItem(IDC_BTN_SEND)->EnableWindow(true);
pDlg->GetDlgItem(IDC_EDIT1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_SPIN1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS2)->EnableWindow(true);
return 0;
}
//-----------------------------------------------------------
void CGraduationDlg::OnReceive()
{
BYTE Buf[500];
//m_UDPSocket.Receive(Buf,500,0);
CString str,strIP,strHost,strHex,strMac,Host,Group,User;
UINT dport;
m_UDPSocket.ReceiveFrom(Buf,500,strIP,dport,0);
if(strIP==(char)NULL||strIP==strOldIP)return;
//strOldIP=strIP;
//int index=m_ListView.InsertItem(0,strIP);
int index=m_ListView.InsertItem(m_ListView.GetItemCount(),strIP);
strHost="";
strHex="";
//User="";
Host="\\";
int tem=0,num=0;
bool bAdd=true;
//if(m_strIP==strIP)
for(i=57;i<500;i++) //57-72
{
if(Buf[i]==0xcc)break;
if(Buf[i]==0x20)bAdd=false;
if(bAdd)
{
str.Format("%c",Buf[i]);
if(Buf[i]>=' ')strHost+=str;
str.Format("%02x.",Buf[i]);
strHex+=str;
}
if((++tem)%18==0)
{
bAdd=true;
//m_ListBox.AddString(strHost);//
strHost.TrimRight((char)NULL);
if(strHost=="")
{
strMac.Delete(17,strMac.GetLength()-17);
m_ListView.SetItem(index,1,LVIF_TEXT,strMac, 0, 0, 0,0);
break;
}
strMac=strHex;
strHost="";
strHex="";
}
}
SetEvent(wait_handle);
}
//-----------------------------------------------------------------
// 取得所有网卡信息
//-----------------------------------------------------------------
BOOL GetAdapterInfo()
{
HKEY hKey, hSubKey, hNdiIntKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}",
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;
DWORD dwIndex = 0;
DWORD dwBufSize = 256;
DWORD dwDataType;
char szSubKey[256];
unsigned char szData[256];
while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS)
{
dwBufSize = 256;
if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
if(strcmp((char*)szData, "ethernet") == 0) // 判断是不是以太网卡
{
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
pAI->strName = (LPCTSTR)szData;
RegGetIP(pAI, (LPCTSTR)szData);
}
AdapterInfoVector.push_back(pAI); // 加入到容器中
}
}
}
RegCloseKey(hNdiIntKey);
}
RegCloseKey(hSubKey);
}
dwBufSize = 256;
} /* end of while */
RegCloseKey(hKey);
return TRUE;
}
BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */)
{
ASSERT(pAI);
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;
unsigned char szData[256];
DWORD dwDataType, dwBufSize;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strIP = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetGate = (LPCTSTR)szData;
RegCloseKey(hKey);
return TRUE;
}
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
void CGraduationDlg::OnOK()
{
char szIP[16];
char szMask[16];
char szGate[16];
unsigned char *pIP, *pMask, *pGate;
DWORD dwIP, dwMask, dwGate;
int nSel = m_ctlAdapters.GetCurSel();
m_ctlIPAddress.GetAddress(dwIP);
m_ctlNetMask.GetAddress(dwMask);
m_ctlNetGate.GetAddress(dwGate);
pIP = (unsigned char*)&dwIP;
pMask = (unsigned char*)&dwMask;
pGate = (unsigned char*)&dwGate;
sprintf(szIP, "%u.%u.%u.%u", *(pIP+3), *(pIP+2), *(pIP+1), *pIP);
sprintf(szMask, "%u.%u.%u.%u", *(pMask+3), *(pMask+2), *(pMask+1), *pMask);
sprintf(szGate, "%u.%u.%u.%u", *(pGate+3), *(pGate+2), *(pGate+1), *pGate);
if(SetIP(AdapterInfoVector[nSel]->strName.c_str(), 0, szIP, szMask, szGate) == TRUE)
::MessageBox(this->m_hWnd, "设置IP地址成功!", "操作结果", MB_OK | MB_ICONINFORMATION );
else
::MessageBox(this->m_hWnd, "设置IP地址失败!", "操作结果", MB_OK | MB_ICONERROR);
}
BOOL ReadTxt(string strFilePath, map< string, int> &IPCount_map)
{
char buf[100] = {0};
int i = 0, ch = 0;
errno_t err;
FILE *stream = NULL;
if( (err = fopen_s(&stream, strFilePath.c_str(), "r")) != 0 )
{
pDlg->m_CListBox.AddString("ERROR\n");
//printf("ERROR: Open or read %s is Error\n", strFilePath.c_str());
return FALSE;
}
ch = fgetc( stream );
while(feof( stream ) == 0)//读取每行数据
{
buf[i] = (char)ch;
if(buf[i] == '\n')
{
buf[i] = '\0';
string strLine = buf;
IPCount_map[strLine]++;//IP计数
i = 0;
}
ch = fgetc( stream );
++i;
}
fclose( stream );
return TRUE;
}
#11
BOOL ReadTxt(string strFilePath, map< string, int> &IPCount_map)
中可能存在缓冲区溢出
while(feof( stream ) == 0)//读取每行数据
{
buf[i] = (char)ch;
if(buf[i] == '\n')
{
buf[i] = '\0';
string strLine = buf;
IPCount_map[strLine]++;//IP计数
i = 0;
}
ch = fgetc( stream );
VERIFY(++i < _countof(buf));
}
中可能存在缓冲区溢出
while(feof( stream ) == 0)//读取每行数据
{
buf[i] = (char)ch;
if(buf[i] == '\n')
{
buf[i] = '\0';
string strLine = buf;
IPCount_map[strLine]++;//IP计数
i = 0;
}
ch = fgetc( stream );
VERIFY(++i < _countof(buf));
}
#12
好像不是这里的问题,我把这个函数的内容注释掉,还是提示溢出
#13
你把有可能出错的代码逐一注释掉 看看错误到底在哪里
#14
这代码是你自己写的么
重试之后看调用堆栈定位到自己的代码
#1
你准备拷贝一个大块数据到一个小空间 造成越界访问或缓冲区溢出?
#2
我也不清楚,请问应该怎么改呀?
#3
点击重试,看堆栈回退到你代码处,
提示的错误就是说你的目标区域不够大 或是准备拷贝的数据太长了
提示的错误就是说你的目标区域不够大 或是准备拷贝的数据太长了
#4
static void __cdecl CopyChars
( _Out_cap_post_count_(nDestLen,nChars) XCHAR* pchDest, _In_ size_t nDestLen, _In_opt_count_(nChars) const XCHAR* pchSrc, _In_ int nChars ) throw()
{
memcpy_s( pchDest, nDestLen*sizeof( XCHAR ), //在这有断点
pchSrc, nChars*sizeof( XCHAR ) );
}
应该怎么改呢?
#5
memcpy_s( pchDest, nDestLen*sizeof( XCHAR ), //在这有断点
pchSrc, min(nDestLen-1, nChars)*sizeof( XCHAR ) );
这样不会报错,但是字符串将不能完整拷贝
最好是把目标缓冲区改大
pchSrc, min(nDestLen-1, nChars)*sizeof( XCHAR ) );
这样不会报错,但是字符串将不能完整拷贝
最好是把目标缓冲区改大
#6
请问下具体应该怎么改呀?是把定义的数组改大么?
#7
是
#8
我把定义的BUF数组,和其他数组都改大了,但还是有这个问题
#9
你跟踪一下,在这个断点处,你看各参数是不是正确,不是长度这个参数超过了这个memcpy_s所要求的长度,就是如果memcpy_s这个长度的类型是short,那么,你长度的参数就不能大于65535.
另外,你最好不要改这个memcpy_s.c文件里的代码,你改了,把程序拿到其它计算机上编绎同样会出问题,因为别人的计算机里的这个文件没改.除非这个文件是你编写的才可以改
另外,你最好不要改这个memcpy_s.c文件里的代码,你改了,把程序拿到其它计算机上编绎同样会出问题,因为别人的计算机里的这个文件没改.除非这个文件是你编写的才可以改
#10
我把和数组有关的都发上来,能帮忙看一下吗?谢谢啦
CGraduationDlg* pDlg;
BYTE bs[50]={0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1};
unsigned char B1[4],B2[4];
HANDLE wait_handle;
CString strOldIP="";
int i;
CGraduationDlg::CGraduationDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGraduationDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//---------------------------------------------
pDlg=this;
if(!m_UDPSocket.Create(myPORT,SOCK_DGRAM))
{
AfxMessageBox("Failed Create Socket");
}
//---------------------------------------------
}
BOOL CGraduationDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
wait_handle=CreateEvent(NULL,true,false,"receive data");
m_spin.SetRange(100,10000);
m_spin.SetPos(100);
// -----------------ListView initialize---------------------
DWORD dwStyle=GetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE);
dwStyle&=~LVS_TYPEMASK;
dwStyle|=LVS_REPORT;
SetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE,dwStyle);
m_ListView.InsertColumn(0,"MAC地址",LVCFMT_LEFT,135);
m_ListView.InsertColumn(0,"IP地址",LVCFMT_LEFT,85);
m_ListView.SetExtendedStyle(LVS_EX_GRIDLINES);
::SendMessage(m_ListView.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
//----------------------------------------------------------
GetAdapterInfo(); // 得到适配器信息
// 添加到ComboBox
for( size_t i = 0; i < AdapterInfoVector.size(); i++ )
m_ctlAdapters.AddString(AdapterInfoVector[i]->strDriverDesc.c_str());
m_ctlAdapters.SetCurSel(0);
OnSelchangeAdapter();
return TRUE; // return TRUE unless you set the focus to a control
}
UINT GraduationThread(LPVOID param)
{
do
{
pDlg->m_strIP.Format("%d.%d.%d.%d",B1[0],B1[1],B1[2],B1[3]);
pDlg->m_ListBox.AddString(pDlg->m_strIP);
if(B1[3]!=0&&B1[2]!=0)
pDlg->m_UDPSocket.SendTo((void*)bs,50,destPORT,pDlg->m_strIP,0);
int nWait=pDlg->m_spin.GetPos();
WaitForSingleObject(
wait_handle, // handle to object to wait for
nWait // time-out interval in milliseconds
);
ResetEvent(wait_handle);
//=============================================
if(B1[2]<=B2[2])
{
if(B1[3]<B2[3])B1[3]++;
else if(B1[2]<B2[2]&&B1[3]<255)B1[3]++;
else if(B1[2]<B2[2]&&B1[3]==255)
{
B1[3]=0;
B1[2]++;
}
}
else break;
if(B1[3]>=B2[3]&&B1[2]>=B2[2])break;
}while(B1[2]<=255&&B1[3]<=255);
pDlg->m_ListBox.AddString("-----OK!-----");
pDlg->GetDlgItem(IDC_BTN_SEND)->EnableWindow(true);
pDlg->GetDlgItem(IDC_EDIT1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_SPIN1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS2)->EnableWindow(true);
return 0;
}
//-----------------------------------------------------------
void CGraduationDlg::OnReceive()
{
BYTE Buf[500];
//m_UDPSocket.Receive(Buf,500,0);
CString str,strIP,strHost,strHex,strMac,Host,Group,User;
UINT dport;
m_UDPSocket.ReceiveFrom(Buf,500,strIP,dport,0);
if(strIP==(char)NULL||strIP==strOldIP)return;
//strOldIP=strIP;
//int index=m_ListView.InsertItem(0,strIP);
int index=m_ListView.InsertItem(m_ListView.GetItemCount(),strIP);
strHost="";
strHex="";
//User="";
Host="\\";
int tem=0,num=0;
bool bAdd=true;
//if(m_strIP==strIP)
for(i=57;i<500;i++) //57-72
{
if(Buf[i]==0xcc)break;
if(Buf[i]==0x20)bAdd=false;
if(bAdd)
{
str.Format("%c",Buf[i]);
if(Buf[i]>=' ')strHost+=str;
str.Format("%02x.",Buf[i]);
strHex+=str;
}
if((++tem)%18==0)
{
bAdd=true;
//m_ListBox.AddString(strHost);//
strHost.TrimRight((char)NULL);
if(strHost=="")
{
strMac.Delete(17,strMac.GetLength()-17);
m_ListView.SetItem(index,1,LVIF_TEXT,strMac, 0, 0, 0,0);
break;
}
strMac=strHex;
strHost="";
strHex="";
}
}
SetEvent(wait_handle);
}
//-----------------------------------------------------------------
// 取得所有网卡信息
//-----------------------------------------------------------------
BOOL GetAdapterInfo()
{
HKEY hKey, hSubKey, hNdiIntKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}",
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;
DWORD dwIndex = 0;
DWORD dwBufSize = 256;
DWORD dwDataType;
char szSubKey[256];
unsigned char szData[256];
while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS)
{
dwBufSize = 256;
if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
if(strcmp((char*)szData, "ethernet") == 0) // 判断是不是以太网卡
{
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
pAI->strName = (LPCTSTR)szData;
RegGetIP(pAI, (LPCTSTR)szData);
}
AdapterInfoVector.push_back(pAI); // 加入到容器中
}
}
}
RegCloseKey(hNdiIntKey);
}
RegCloseKey(hSubKey);
}
dwBufSize = 256;
} /* end of while */
RegCloseKey(hKey);
return TRUE;
}
BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */)
{
ASSERT(pAI);
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;
unsigned char szData[256];
DWORD dwDataType, dwBufSize;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strIP = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetGate = (LPCTSTR)szData;
RegCloseKey(hKey);
return TRUE;
}
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
void CGraduationDlg::OnOK()
{
char szIP[16];
char szMask[16];
char szGate[16];
unsigned char *pIP, *pMask, *pGate;
DWORD dwIP, dwMask, dwGate;
int nSel = m_ctlAdapters.GetCurSel();
m_ctlIPAddress.GetAddress(dwIP);
m_ctlNetMask.GetAddress(dwMask);
m_ctlNetGate.GetAddress(dwGate);
pIP = (unsigned char*)&dwIP;
pMask = (unsigned char*)&dwMask;
pGate = (unsigned char*)&dwGate;
sprintf(szIP, "%u.%u.%u.%u", *(pIP+3), *(pIP+2), *(pIP+1), *pIP);
sprintf(szMask, "%u.%u.%u.%u", *(pMask+3), *(pMask+2), *(pMask+1), *pMask);
sprintf(szGate, "%u.%u.%u.%u", *(pGate+3), *(pGate+2), *(pGate+1), *pGate);
if(SetIP(AdapterInfoVector[nSel]->strName.c_str(), 0, szIP, szMask, szGate) == TRUE)
::MessageBox(this->m_hWnd, "设置IP地址成功!", "操作结果", MB_OK | MB_ICONINFORMATION );
else
::MessageBox(this->m_hWnd, "设置IP地址失败!", "操作结果", MB_OK | MB_ICONERROR);
}
BOOL ReadTxt(string strFilePath, map< string, int> &IPCount_map)
{
char buf[100] = {0};
int i = 0, ch = 0;
errno_t err;
FILE *stream = NULL;
if( (err = fopen_s(&stream, strFilePath.c_str(), "r")) != 0 )
{
pDlg->m_CListBox.AddString("ERROR\n");
//printf("ERROR: Open or read %s is Error\n", strFilePath.c_str());
return FALSE;
}
ch = fgetc( stream );
while(feof( stream ) == 0)//读取每行数据
{
buf[i] = (char)ch;
if(buf[i] == '\n')
{
buf[i] = '\0';
string strLine = buf;
IPCount_map[strLine]++;//IP计数
i = 0;
}
ch = fgetc( stream );
++i;
}
fclose( stream );
return TRUE;
}
#11
BOOL ReadTxt(string strFilePath, map< string, int> &IPCount_map)
中可能存在缓冲区溢出
while(feof( stream ) == 0)//读取每行数据
{
buf[i] = (char)ch;
if(buf[i] == '\n')
{
buf[i] = '\0';
string strLine = buf;
IPCount_map[strLine]++;//IP计数
i = 0;
}
ch = fgetc( stream );
VERIFY(++i < _countof(buf));
}
中可能存在缓冲区溢出
while(feof( stream ) == 0)//读取每行数据
{
buf[i] = (char)ch;
if(buf[i] == '\n')
{
buf[i] = '\0';
string strLine = buf;
IPCount_map[strLine]++;//IP计数
i = 0;
}
ch = fgetc( stream );
VERIFY(++i < _countof(buf));
}
#12
好像不是这里的问题,我把这个函数的内容注释掉,还是提示溢出
#13
你把有可能出错的代码逐一注释掉 看看错误到底在哪里
#14
这代码是你自己写的么
重试之后看调用堆栈定位到自己的代码