关于enumwindowpro 和 stl 高手帮我看看 为什么出现访问非法地址错误???
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
#pragma once
#include "list"
#include "string"
using namespace std;
typedef struct
{
HWND hWnd;
string cWinBuf;
string cname;
}WINLIST;
typedef list<WINLIST> wndlist;
static wndlist gWinList,childlist;
static BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam )
{
WINLIST temp;
temp.hWnd=hWnd;
::GetWindowText(hWnd, (LPSTR)temp.cWinBuf.data(), 256);
::GetClassName(hWnd, (LPSTR)temp.cname.data() ,256);
string::size_type pos=0;
pos=temp.cname.find("ThunderRT6FormDC",temp.cname.length());
if(pos!=-1)
{
gWinList.push_back(temp);
}
return TRUE;
}
static BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
WINLIST temp;
temp.hWnd=hwnd;
::GetWindowText(hwnd, (LPSTR)temp.cWinBuf.data(), 256);
::GetClassName(hwnd, (LPSTR)temp.cname.data() ,256);
if(temp.cWinBuf.find("打印结果")!=NULL)
{
childlist.push_back(temp);
}
return TRUE;
}
class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
public CMessageFilter, public CIdleHandler
{
public:
void getwnd()
{
::ATLTRACE("getwnd begin");
::EnumWindows((WNDENUMPROC )EnumWindowsProc,0);
::ATLTRACE("getwnd end;");
/*list<WINLIST>::iterator it=gWinList.begin();
this->MessageBox("getwnd begin");
for(;it!=gWinList.end();it++)
{
if(!it->cname.compare("ThunderRT6FormDC"))
gWinList.erase(it);
}
this->MessageBox("getwnd ok");*/
}
HWND getchildwnd()
{
list<WINLIST>::iterator it=gWinList.begin();
for(;it!=gWinList.end();it++)
{
::EnumChildWindows(it->hWnd,(WNDENUMPROC)EnumChildProc,0);
}
; for(it=childlist.begin();it!=childlist.end();it++)
{
if(it->cWinBuf.compare("打印结果"))
return it->hWnd;
}
return NULL;
}
enum { IDD = IDD_MAINDLG };
virtual BOOL PreTranslateMessage(MSG* pMsg)
{
return IsDialogMessage(pMsg);
}
virtual BOOL OnIdle()
{
return FALSE;
}
BEGIN_UPDATE_UI_MAP(CMainDlg)
END_UPDATE_UI_MAP()
BEGIN_MSG_MAP(CMainDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
COMMAND_ID_HANDLER(IDOK, OnOK)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
COMMAND_HANDLER(IDC_BUTTON1, BN_CLICKED, OnBnClickedButton1)
END_MSG_MAP()
// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
LRESULT 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);
// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
UIAddChildWindowContainer(m_hWnd);
return TRUE;
}
LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}
LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// TODO: Add validation code
CloseDialog(wID);
return 0;
}
LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CloseDialog(wID);
return 0;
}
void CloseDialog(int nVal)
{
DestroyWindow();
::PostQuitMessage(nVal);
}
LRESULT OnBnClickedButton1(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
HWND wnd;
string a;
getwnd();
wnd=this->getchildwnd();
::sprintf((LPSTR)a.data(),"%d",wnd);
this->MessageBox((LPCTSTR)a.data());
return 0;
}
private:
};
问题点数:100、回复次数:7Top
1 楼roger_ding(海天一色)回复于 2004-11-01 13:53:57 得分 0
非法错误的时候利用stack list,先查下在哪里出错Top
2 楼wwwsq(wwwsq)回复于 2004-11-01 14:00:10 得分 0
访问的时候对象还在吗?Top
3 楼whereisthedog()回复于 2004-11-01 14:03:31 得分 0
> getwindowaaa.exe!operator delete(void * pUserData=0x6f747541) 行52 + 0x3 C++
getwindowaaa.exe!std::allocator<char>::deallocate(char * _Ptr=0x6f747541, unsigned int __formal=111) 行132 + 0x9 C++
getwindowaaa.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy(bool _Built=true, unsigned int _Newsize=0) 行1519 C++
getwindowaaa.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() 行458 C++
getwindowaaa.exe!WINLIST::~WINLIST() + 0x4a C++
getwindowaaa.exe!EnumWindowsProc(HWND__ * hWnd=0x000104bc, long lParam=0) 行31 + 0x19 C++
user32.dll!77cd55b5()
user32.dll!77cd5614()
getwindowaaa.exe!CMainDlg::getwnd() 行60 + 0xf C++
getwindowaaa.exe!CMainDlg::OnBnClickedButton1(unsigned short __formal=0, unsigned short __formal=0, unsigned short __formal=0, unsigned short __formal=0) 行167 C++
getwindowaaa.exe!CMainDlg::ProcessWindowMessage(HWND__ * hWnd=0x00050344, unsigned int uMsg=273, unsigned int wParam=1000, long lParam=328530, long & lResult=-858993460, unsigned long dwMsgMapID=0) 行105 + 0x51 C++
getwindowaaa.exe!ATL::CDialogImplBaseT<ATL::CWindow>::DialogProc(HWND__ * hWnd=0x0012fd00, unsigned int uMsg=273, unsigned int wParam=1000, long lParam=328530) 行3295 + 0x25 C++
user32.dll!77cd15df()
user32.dll!77cdbf98()
user32.dll!77cdbf4e()
user32.dll!77d05142()
user32.dll!77cdbe0e()
user32.dll!77cd2cea()
ntdll.dll!77f33868()
user32.dll!77cdbd91()
user32.dll!77cd2c62()
user32.dll!77cd5fc1()
user32.dll!77cd3374()
comctl32.dll!70af5436()
comctl32.dll!70af550f()
comctl32.dll!70af7784()
CnsMin.dll!3721a7cf()
CnsMin.dll!3721ab38()
user32.dll!77cd4868()
user32.dll!77cd15df()
user32.dll!77cd1a15()
user32.dll!77cd19cb()
user32.dll!77d05142()
user32.dll!77cd16d8()
user32.dll!77cd6b2c()
user32.dll!77cd40eb()
user32.dll!77d05142()
user32.dll!77cd1744()
user32.dll!77cd4088()
user32.dll!77cd6ee4()
getwindowaaa.exe!ATL::CWindow::IsDialogMessageA(tagMSG * lpMsg=0x0012fd68) 行1611 + 0x12 C++
getwindowaaa.exe!CMainDlg::PreTranslateMessage(tagMSG * pMsg=0x0012fd68) 行90 C++
getwindowaaa.exe!WTL::CMessageLoop::PreTranslateMessage(tagMSG * pMsg=0x0012fd68) 行330 + 0x16 C++
getwindowaaa.exe!WTL::CMessageLoop::Run() 行291 + 0x13 C++
getwindowaaa.exe!Run(char * __formal=0x00151f08, int nCmdShow=1) 行32 + 0x8 C++
getwindowaaa.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * __formal=0x00000000, char * lpstrCmdLine=0x00151f08, int nCmdShow=1) 行54 + 0xd C++
getwindowaaa.exe!WinMainCRTStartup() 行251 + 0x30 C
kernel32.dll!77e1f38c()
Top
4 楼whereisthedog()回复于 2004-11-01 14:06:51 得分 0
对象还在阿
Top
5 楼DentistryDoctor(不在无聊中无奈,就在沉默中变态)回复于 2004-11-01 15:18:14 得分 100
为WINLIST写上operator=和copy constructor即可解决问题。Top
6 楼huaguli(学VC不要命)回复于 2004-11-01 16:58:48 得分 0
为WINLIST写上operator=和copy constructorTop
7 楼whereisthedog()回复于 2004-11-01 20:59:58 得分 0
主要是 string成员的问题
WINLIST &operator=(const WINLIST& a)
{
WINLIST * temp=new WINLIST(a)
// 这里应该怎么写啊
}
WINLIST(const& WINLIST a)
{
hWnd=a.hWnd;
cWinBuff=a.WinBuff;
cname=a.cname;
}
写了也是不行啊
正确的写法谁能告诉以下阿
我现在知道了问题主要是string类的指针成员的问题
可是不知怎么解决Top




