Click here to Skip to main content
15,914,594 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
valikac31-Jul-06 9:47
valikac31-Jul-06 9:47 
AnswerRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
krmed31-Jul-06 10:22
krmed31-Jul-06 10:22 
GeneralRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
Jesse Evans31-Jul-06 11:16
Jesse Evans31-Jul-06 11:16 
GeneralRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
Jesse Evans31-Jul-06 11:18
Jesse Evans31-Jul-06 11:18 
AnswerRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
Zac Howland31-Jul-06 10:37
Zac Howland31-Jul-06 10:37 
GeneralRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
Jesse Evans31-Jul-06 11:26
Jesse Evans31-Jul-06 11:26 
AnswerRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
Michael Dunn31-Jul-06 18:07
sitebuilderMichael Dunn31-Jul-06 18:07 
QuestionReading And Writting Files to Access Database using ADO [modified] Pin
Christopher Stratmann31-Jul-06 8:36
Christopher Stratmann31-Jul-06 8:36 
I think I got writing the files to the database to work it taking the file out which seems to be the problem. It doesn't give me any errors after the write and when I look into the database I see that a row was correctly inserted and I see under the file field (OLE Object Type) it says "Long binary data". Here is the code I use for that...

struct RS_FILE
{
	long	m_l_FileID;
	CString m_cs_FileName;		// This will get set by the DB functions for the following class.
	CLongBinary	m_lb_FileData;	// This will get set by the DB functions for the following class.


Any Ideas?

bool DB_File_Insert(CDatabase* db, RS_FILE& rs_file, const std::string& s_file_fullname, CErrorHandler& eh)
{
	bool b_ret = true;
	HGLOBAL hGlobal = NULL;
	try
	{
		CRecordSetFile rs(db);
		if (rs.Open() == FALSE)
		{
			eh.Initialize();
			eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to open the file record set to insert a new record.");
			return false;
		}

		rs.AddNew();
		rs.m_rs_file = rs_file;

		// Open the file.
		CFile cf;
	    CFileStatus	fileStatus;
		if (cf.Open(s_file_fullname.c_str(), CFile::modeRead) == FALSE)
		{
			eh.Initialize();
			eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to open the file '%s' for inserting into the database.",s_file_fullname.c_str());
			b_ret = false;
			goto LabelRecordSetFileClose;
		}
		if (cf.GetStatus(fileStatus) == FALSE)
		{
			eh.Initialize();
			eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to get the status of the file '%s' after opening it to insert into the database.",s_file_fullname.c_str());
			b_ret = false;
			goto LabelFileClose;
		}

		rs.m_rs_file.m_cs_FileName = cf.GetFileName();
		rs.m_rs_file.m_lb_FileData.m_dwDataLength = fileStatus.m_size;

		hGlobal	= GlobalAlloc(GPTR,fileStatus.m_size);
		if (hGlobal == NULL)
		{
			eh.Initialize();
			eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to allocate the specified number of bytes '%ld' for copying the file '%s' into for storing into the database.",fileStatus.m_size,s_file_fullname.c_str());
			b_ret = false;
			goto LabelFileClose;
		}

		rs.m_rs_file.m_lb_FileData.m_hData = GlobalLock(hGlobal);

		cf.ReadHuge(rs.m_rs_file.m_lb_FileData.m_hData,fileStatus.m_size);

		rs.SetFieldDirty(&rs.m_rs_file.m_lb_FileData, TRUE );
		rs.SetFieldNull(&rs.m_rs_file.m_lb_FileData, FALSE );

		if (rs.Update() == FALSE)
		{
			eh.Initialize();
			eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to insert the new record into the database.");
			b_ret = false;
		}

	    GlobalUnlock(hGlobal);

LabelFileClose:
		cf.Close();

LabelRecordSetFileClose:
	    rs.Close();
	}
	catch(CDBException* e1)
	{
		eh.Initialize();
		eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Database Error: %s",e1->m_strError);
		return false;
	}
	catch(CException* e2)
	{
		eh.Initialize();
		char sz_error [MAX_PATH] = "";
		e2->GetErrorMessage(sz_error,sizeof(sz_error));
		eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Database Error: %s",sz_error);
		return false;
	}
	catch(...)
	{
		eh.Initialize();
		eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Database Error: Unknown.");
		return false;
	}
	return b_ret;
}


Now here it the code where I try to get the file and write it to a folder....

// This function seems to work fine.
bool DB_File_GetID(CDatabase* db, const long& l_id, RS_FILE& rs_file, CErrorHandler& eh)
{
	try
	{
		CRecordSetFile rs(db);
		rs.m_strFilter.Format("[FileID] = %ld",l_id);
		if (rs.Open() == FALSE)
		{
			eh.Initialize();
			eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to open the record set to get the file id '%ld'.",l_id);
			return false;
		}
		rs_file = rs.m_rs_file;
		rs.Close();
	}
	catch(CDBException* e1)
	{
		eh.Initialize();
		eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Database Error: %s",e1->m_strError);
		return false;
	}
	catch(CException* e2)
	{
		eh.Initialize();
		char sz_error [MAX_PATH] = "";
		e2->GetErrorMessage(sz_error,sizeof(sz_error));
		eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Database Error: %s",sz_error);
		return false;
	}
	catch(...)
	{
		eh.Initialize();
		eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Database Error: Unknown.");
		return false;
	}
	return true;
}

bool RS_FILE::WriteFile(const std::string& s_folder, CErrorHandler& eh)
{
    std::string s_temp_folder = s_folder;
    if(s_temp_folder.length() == 0)
    {
        eh.Initialize();
        eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to write the file to a folder because the folder is not specified.");
        return false;
    }

    if (s_temp_folder[s_temp_folder.length()-1] != '\\')
        s_temp_folder += "\\";

    std::string s_file = ssprintf("%s%s",s_temp_folder.c_str(),m_cs_FileName);

    CFile cf;
    if (cf.Open(s_file.c_str(),CFile::modeCreate|CFile::modeWrite) == FALSE)
    {
        eh.Initialize();
        eh.Set(__FUNCTION__,__FILE__,__LINE__,EHTYPE_ERROR,"Failure to open and create the file '%s' to write data to.",s_file.c_str());
        return false;
    }
    LPSTR buffer = (LPSTR)GlobalLock(m_lb_FileData.m_hData);
    cf.WriteHuge(buffer,m_lb_FileData.m_dwDataLength); // Failure here.
    GlobalUnlock(m_lb_FileData.m_hData);
    cf.Close();
    return true;
}


The failure is occurring in the CFile look below....
void CFile::Write(const void* lpBuf, UINT nCount)
{
	ASSERT_VALID(this);
	ASSERT(m_hFile != (UINT)hFileNull);

	if (nCount == 0)
		return;     // avoid Win32 "null-write" option

	ASSERT(lpBuf != NULL); // For some reason lpBuf is NULL


Any help is greatly appreciated. I know this post is a little long be I am not sure where the error is.

Chris
AnswerRe: Reading And Writting Files to Access Database using ADO Pin
Zac Howland31-Jul-06 8:45
Zac Howland31-Jul-06 8:45 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Christopher Stratmann31-Jul-06 10:56
Christopher Stratmann31-Jul-06 10:56 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Zac Howland31-Jul-06 11:16
Zac Howland31-Jul-06 11:16 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Christopher Stratmann31-Jul-06 23:50
Christopher Stratmann31-Jul-06 23:50 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Zac Howland1-Aug-06 3:46
Zac Howland1-Aug-06 3:46 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Christopher Stratmann1-Aug-06 4:22
Christopher Stratmann1-Aug-06 4:22 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Zac Howland1-Aug-06 4:40
Zac Howland1-Aug-06 4:40 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Christopher Stratmann1-Aug-06 7:58
Christopher Stratmann1-Aug-06 7:58 
GeneralRe: Reading And Writting Files to Access Database using ADO Pin
Christopher Stratmann4-Aug-06 2:09
Christopher Stratmann4-Aug-06 2:09 
Questionwininet issue Pin
EpicYeti31-Jul-06 8:20
EpicYeti31-Jul-06 8:20 
AnswerRe: wininet issue Pin
Zac Howland31-Jul-06 9:11
Zac Howland31-Jul-06 9:11 
GeneralRe: wininet issue Pin
EpicYeti31-Jul-06 9:52
EpicYeti31-Jul-06 9:52 
GeneralRe: wininet issue Pin
Zac Howland31-Jul-06 9:57
Zac Howland31-Jul-06 9:57 
GeneralRe: wininet issue Pin
EpicYeti31-Jul-06 10:25
EpicYeti31-Jul-06 10:25 
GeneralRe: wininet issue Pin
Lucky the code machine23-Feb-08 5:55
Lucky the code machine23-Feb-08 5:55 
QuestionProblems with Dialog Buttons Pin
kitty531-Jul-06 6:34
kitty531-Jul-06 6:34 
AnswerRe: Problems with Dialog Buttons Pin
toxcct31-Jul-06 6:40
toxcct31-Jul-06 6:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.