Windows has stopped working; compilier throws no error

Something is terribly wrong in my code, and I cannot find where the problem is. I think it is a casting problem....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//the problem code from main.cpp
						//view accounts balance
						banking_connect("acct_sav");
						//ErrorExit("ERR18");
						std::vector<char*> aacctmsgs;
					    int rfrslt=ReadFile(hfile4, (LPVOID)acct, 8000, NULL, NULL);
					    //ErrorExit("ERR17");
					    std::string acctt=(std::string)acct;
					    srchd2=acctt.find("Balance");
				    	if(srchd2!=std::string::npos)
					    {
					    	//ErrorExit("ERR16");
					        //acctc=(char*)acctt.c_str();
					        StringCbCopyA((LPCSTR)acctc, 8001, (LPCTSTR)acctt.c_str();
					        std::vector<char> accts;
					        //ErrorExit("ERR15");
					        for(int kk=0;kk<21;kk++)
					        {
					            accts.push_back(acctc[srchd2+kk]);
					        }
					        std::stringstream aarslts;
					        ErrorExit("ERR14");
					        for(int ll=0;ll<20;ll++)
					        {
					            aarslts << accts[ll];
					        }
					        std::string strarslts=aarslts.str();
					        ErrorExit("ERR13");
					        StringCbCopyA((LPCSTR)aacctmsg, 8001, (LPCTSTR)strarslts.c_str());
					        ErrorExit("ERR12");
					        aacctmsgs.push_back(aacctmsg);
					        aacctmsgs.push_back("\n");
					        ErrorExit("ERR11");
							//SetWindowText(txtfld2, (LPCSTR)aacctmsg); 


1
2
3
4
5
6
7
8
9
10
11
12
//variables from first.h
    char* acct=new char[71];
    char* acct2=new char[71];
    char* acct3=new char[71];
    char* acctc=new char[21];
    char* acctc2=new char[21];
    char* acctc3=new char[21];
    size_t srchd2;
    size_t srchd3;
    char* aacctmsg=new char[21];
    char* aacctmsg2=new char[21];
    char* aacctmsgsssc=new char[81];


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//some functions from second.h
void banking_connect(std::string BankFileType)
{
	if(BankFileType == "bank")
	{
		hfile3=CreateFile("banking.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		hfile4=CreateFile("banking.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	}
	else if (BankFileType == "sys")
	{
		hfile3=CreateFile("banking_sys.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		hfile4=CreateFile("banking_sys.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	}
	else if (BankFileType == "acct_sav")
	{
		hfile3=CreateFile("bnk_savings.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
		hfile4=CreateFile("bnk_savings.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	}
		else if (BankFileType == "acct_chk")
	{
		hfile3=CreateFile("bnk_checking.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
		hfile4=CreateFile("bnk_checking.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	}
		else if (BankFileType == "acct_inv")
	{
		hfile3=CreateFile("bnk_investment.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
		hfile4=CreateFile("bnk_investment.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	}
}

void banking_close()
{
	CancelIo(hfile3);
	CancelIo(hfile4);
} 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//ErrorExit() (taken from MSDN)
void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}


I think that the problem is near this line in main.cpp:
StringCbCopyA((LPCSTR)acctc, 8001, (LPCTSTR)acctt.c_str());

I am completely baffled by this one. When the code including && before the first banking_close() is run alone, it works fine. Add the second bit after that, and everything goes boom.

So:
1) What is wrong?
2) Solution?
3) Bad Practices Within The Code Snippets?

Thanks,
~Hom
Last edited on
1
2
3
    char* acct=new char[71];
...
int rfrslt=ReadFile(hfile4, (LPVOID)acct, 8000, NULL, NULL);


Do we see anything odd here? What significance do the magic numbers 71 and 8000 have?

3) Bad Practices Within The Code Snippets?

Horrible variable names.
Manual memory management.
Use of magic numbers.
Definition of variables in header file.
Use of C-style casts, even where casts are not required.
Horrible variable names.

lol....

---

And thanks. It looks like the code works at ErrorExit("ERR14");..
Topic archived. No new replies allowed.