|
Hello everyone,
There is error message when executing my program,
Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002.
It is very simple, does anyone know what is wrong with the program?
I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
foo.c
<br />
int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};<br />
goo.c
<br />
extern int* p_int;<br />
<br />
int main (int argc, char** argv)<br />
{<br />
int i;<br />
int sum = 0;<br />
for (i = 0; i < 16; i++)<br />
{<br />
sum += p_int [i];
}<br />
<br />
return 0;<br />
}<br />
thanks in advance,
George
|
|
|
|
|
What is the address of p_int (in main()) for both the cases.
|
|
|
|
|
Both are 0x00427b40.
regards,
George
|
|
|
|
|
George_George wrote: I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
Not quite. The compiler can't make the connection that the extern *p_int in goo.c is the same variable as the int p_int[16] in foo.c. It can't reconcile the integer pointer and the interger array. Just use extern int p_int[] in goo.c - you don't need to specify the size but you do need to tell the compiler the external variable is an array.
Judy
|
|
|
|
|
Thanks for your advice, Judy!
regards,
George
|
|
|
|
|
Hi all. I have a TCP program that crashes when a connection isnt made. One example is when i open it and dont allow the connection through a firewall it crashes in Windows. How would i go about making it try to reconnect every so often if a connection isnt established without crashing? Thanx in advance!
|
|
|
|
|
dellthinker wrote: How would i go about making it try to reconnect every so often if a connection isnt established without crashing?
Probably the first step is to use the debugger to determine exactly why it is crashing. When it crashes, look at the call stack window, see how it got there, inspect the variables.
|
|
|
|
|
I ran the debugger and it pointed at this line:
<br />
structSock.sin_addr.s_addr = *((unsigned long*)ServerAddr->h_addr_list[0]);<br />
Do i need to loop this every so often to make sure the program doesnt crash when trying to connect?
This was the error message that VC++ gave me.
<quote>
Unhandled exception in Program.exe: 0xC0000005: Access Violation.
P.S. I set a breakpoint at that line and yes it is the problem. How can i resolve this?
Anyone care to shed some light on what that means?
|
|
|
|
|
Hello,
I am currently working on a notepad-like application and I am having trouble with the CFileDialog class. In my code there is a save function, and when the user selects save as from the menu, the function is run and the "Save File As" dialog comes up. When the user enters the filename and presses the save button, my program cannot seem to get the file extension...As an example, if the user entered foo.txt for the filename, I can use GetFileExt() to get the file extension, but if the user just enters foo and selects .txt extension from the dropdown, I cannot seem to get the extension
Currently I'm using a VC++ 6.0 compiler. Here is the code:
void CMFCTestDlg::OnFileSaveas()
{
CFileDialog filesave(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Text files (*.txt)|*.txt|All Files|*||");
if (filesave.DoModal()==IDOK){
filename=filesave.GetFileTitle();
filename+=".";
filename+=filesave.GetFileExt();
CFile theFile;
theFile.Open(filename,CFile::modeWrite | CFile::modeCreate);
int filesize=m_editbox.GetWindowTextLength();
char* buffer=new char [filesize+1];
m_editbox.GetWindowText(buffer,filesize+1);
theFile.Write(buffer,filesize+1);
delete [] buffer;
theFile.Close();
m_editbox.SetModify(FALSE);
}
}
BTW, m_editbox is my textbox. The function saves the text in m_editbox to a file.
Thanks !
|
|
|
|
|
The nFilterIndex member in the OPENFILENAME structure indicates the currently selected index in the "save as type" combobox. So can get the extesnion using this index
CString csExt = dlg.GetFileExt();
if( csExt.IsEmpty())
{
int n = filesave.m_ofn.nFilterIndex
// now if n == 1, the item is "Text files (*.txt)"
// if n == 2, the items is "All Files"
}
BTW I would like to know what will you put extesnion when user selects "All files".
FYI :- Normally there will not be "All files" options in the save as dialog box.
|
|
|
|
|
LOL, thanks for pointing that out! I just copied the save stuff over from the open function, so I didn't notice...;P
And now I have a new problem...how do you associate a file extension with a particular program?!
|
|
|
|
|
I hope ur using an sdi/mdi application. You can find the option in the appwizard itself. How ever you can set the file associate later by following the below steps.
Suppose in the case of SDI application if you take the string table, you can find one entry with ID IDR_MAINFRAME.If my application name is MyApp, the string may look like
MyApp\n\nMyApp\n\n\nMyApp.Document\nMyApp Document. If you want to associate the file type say ".xyz", modify the string as follows
MyApp\n\nMyApp\nMyApp Files (*.xyz)\n.MyApp\nMyApp.Document\nMyApp Document. Also call the function RegisterShellFileTypes(TRUE); in the Initinstance just after the creation of the template.
|
|
|
|
|
hxhl95 wrote: ...but if the user just enters foo and selects .txt extension from the dropdown, I cannot seem to get the extension
Because NULL was used as the second argument to the CFileDialog constructor.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I am not using MFC; just pure win32 at the moment. Is there something like a font dialog box? If there is then how to I call it and set my font after disposing the dialog...?
|
|
|
|
|
Use the ChooseFont() API to show the common font chooser dialog.
|
|
|
|
|
Font Dialog Box[^]
CreateFontIndirect() can be used to create a font from the LOGFONT obtained with the dialog.
For setting a control's font there's WM_SETFONT.
SelectObject() can select a font into a device context.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
hi, i wrote a mfc dialog that's running a worker thread i want to terminate it from outside.
in CMyTestDlg: public CDialog{
static bool STOP;//a shared variable to signal the thread function
static UINT __cdecl Start();
CWinThread* pThread
....
in the cpp file, i have a OnBnClickedButtonStart() and OnBnClickedButtonStop()function to start and stop the thread
bool CMyTestDlg::Stop=false;
void CMyTestDlg::OnBnClickedButtonStart(){
pThread=AfxBeginThread(Start,this,-1,0,CREATE_SUSPENDED);
pThread->m_bAutoDelete=FALSE;
pThread->ResumeThread();
}
void CMyTestDlg::OnBnClickedButtonStop()
{
STOP=true;
if(::WaitForSingleObject(pThread->m_hThread,2000)==WAIT_OBJECT_0){
delete pThread;
m_ListBox_MSG.AddString(_T("Thread terminated succesfully"));
//message showed in a list box
}
else{
::TerminateThread(pThread->m_hThread,0);
m_ListBox_MSG.AddString(_T("Thread is not normally terminated!"));
//message showed in a list box
}
}
UINT __cdecl CMyTestDlg::Start(LPVOID pParam){
CMyTestDlg* me=(CMyTestDlg*)pParam;
while(STOP==false){
//looping here...
}
return 0;
}
when i stop the thread, from the message showed in the listbox, i found that the thread is always terminated "dangeroursly" which means the ::TerminateThread()function ran.
What could be the possible cause?
|
|
|
|
|
What happens if you wait longer?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
i tried for 10s, still doesn't terminate properly.
the looping is fairly simple, should take that long
|
|
|
|
|
another thing,in the thread function, i add the msg to listbox before return
UINT __cdecl CMyTestDlg::Start(LPVOID pParam){
CMyTestDlg* me=(CMyTestDlg*)pParam;
while(STOP==false){
//looping here...
}
me->m_ListBox_MSG.AddString(_T("ERROR!"));
return 0;
}
if the thread is terminated properly, this msg should show, how ever it didn't, this possibly means that the loop didn't exit...what's wrong?
|
|
|
|
|
How about setting the wait time to INFINITE and putting a breakpoint at the end of the thread proc
(maybe on the "return 0;").
Does the breakpoint ever get hit? If not, why?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
just tried your suggestion, the "return 0" never get hit, and dialog is stuck ,something weird is going on here
|
|
|
|
|
Mark Salsbery wrote: What happens if you wait longer?
They call your number and you pick up your Filet-O-Fish
|
|
|
|
|
lol,seriously, i think my thread loop didn't exit even i made STOp=true; what's going here?
|
|
|
|
|
alberthyc wrote: what's going here?
Sadly the same thing that goes on here everyday
|
|
|
|