|
That's a tall order as the information is scattered amongst several APIs. Try:
GetSystemInfo()<br />
GetVersionEx()<br />
DirectSoundEnumerate()<br />
GlobalMemoryStatusEx()
Other posters can surely complement this list to get a full manifest of the hardware information.
|
|
|
|
|
I have a file deleter program that deletes cookies and temporary internet files, however, it sends the files to the recycle bin.
Is it possible to secure delete the files, this means that the files are not moved to the "recycle bin", but rather are actually fully deleted from the hard drive.
|
|
|
|
|
What method are you currently using to delete with?
|
|
|
|
|
I use the following class to delete files.
void CFileDeleter::Initialize()
{
m_nDeleteType = 0;
LoadDeleteType();
}
void CFileDeleter::LoadDeleteType()
{
HKEY hKey;
long lRet;
int nType = 0;
DWORD dw = sizeof(int);
DWORD dwType = REG_DWORD;
lRet = RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\iISoftware\\iIWiper",0,KEY_ALL_ACCESS,&hKey);
if(lRet == ERROR_SUCCESS)
{
lRet = RegQueryValueEx(hKey,"DeleteType",0,&dwType,(LPBYTE)&nType,&dw);
if(lRet == ERROR_SUCCESS)
{
m_nDeleteType = nType;
}
}
}
BOOL CFileDeleter::FileDeleteOperation(LPTSTR lpszFile)
{
DWORD dwAttrib;
dwAttrib = GetFileAttributes(lpszFile);
if (dwAttrib == 0xFFFFFFFF)
{
return FALSE;
}
switch(m_nDeleteType)
{
case DELETE_RECYCLE:
{
SHFILEOPSTRUCT f;
ZeroMemory(&f, sizeof(SHFILEOPSTRUCT));
f.wFunc = FO_DELETE;
f.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
lpszFile[lstrlen(lpszFile)+1] = 0;
f.pFrom = lpszFile;
SHFileOperation(&f);
break;
}
case DELETE_DELETE:
DeleteFile(lpszFile);
break;
default:
{
SHFILEOPSTRUCT f;
ZeroMemory(&f, sizeof(SHFILEOPSTRUCT));
f.wFunc = FO_DELETE;
f.fFlags = FOF_ALLOWUNDO;
f.pFrom = lpszFile;
SHFileOperation(&f);
break;
}
}
return TRUE;
}
|
|
|
|
|
Just omit the FOF_ALLOWUNDO flag.
|
|
|
|
|
Thank's,
I deleted the FOF_ALLOWUNDO flag.
Now I don't see the deleted files in the recycle bin anymore, but when I click on the Clean File in my program, before it deletes, for every cookie a dialog box pops: Are you sure you want to delete "cookie name"?
Is there any way I could stop that message from popping up?
|
|
|
|
|
azi_games wrote:
Is there any way I could stop that message from popping up?
Adding FOF_NOCONFIRMATION, I presume.
|
|
|
|
|
|
I'm wondering if there is an API fn to effectively perform the same as adding an entry to the HOSTS file. I seem to be drawing a blank so far with my google and msdn search attempts.
Cheers,
Phil
|
|
|
|
|
It's just a text file. Would the normal file I/O functions not suffice?
CStdioFile file;<br />
file.Open("hosts", CFile::modeWrite);<br />
file.SeekToEnd();<br />
file.WriteString("127.0.0.1 www.aadserver.net\n");<br />
file.Close();
|
|
|
|
|
Hello
I'm currently trying to port a library to CE.NET using eVC 4.0. The problem is we are using a 3rd party library that uses iostreams. There is a verion of STL that comes with eVC 4.0 but it doesn't support iostreams. I tried using STLport without much luck.
Has anybody had similar problems? What had to be done to get it to work?
Thanks!
|
|
|
|
|
Does anyone know of any way to find out info about drivers through some sort of API? In particular I am interested in the sound card driver. To be clear, I am not looking for the capabilities of the sound card, like supported bitrates, and things like that, more like, is it a VxD, WDM, etc..
Any ideas?
Marcus Spitzmiller
"Why must life be so hard? Why must I fail at every attempt at masonry?" - Homer
|
|
|
|
|
Ok... Can anybody tell me what I am doing wrong here please ?
struct MyStruct1 {
int m_integer;
}
struct MyStruct2 {
MyStruct1 *pStruct1;
}
MyStruct2 m_struct2;
MyObject::MyObject() {
m_struct2.pStruct1 = NULL;
m_struct2.pStruct1 = (MyStruct1 *) malloc(10);
}
MyObject::~MyObject() {
if(m_struct2.pStruct1)
{
free(struct2.pStruct1); <- I get error here....
}
}
|
|
|
|
|
You failed to mention if the error was compilation or run-time. Given just the code you provided, it has several compile errors. Those can be fixed with:
struct MyStruct1
{
int m_integer;
};
struct MyStruct2
{
MyStruct1 *pStruct1;
};
MyStruct2 m_struct2;
class MyObject
{
public:
MyObject();
~MyObject();
};
MyObject::MyObject()
{
m_struct2.pStruct1 = NULL; <code>
m_struct2.pStruct1 = (MyStruct1 *) malloc(10);
}
MyObject::~MyObject()
{
if(m_struct2.pStruct1)
{
free(m_struct2.pStruct1);
}
}
|
|
|
|
|
Ok.. You are right.. I didn't have enough info.. Here is what I use and I am getting run time error..'Debug error.. DAMAGE: after normal block(#42) at 0x002FO7EO.' and it is failing at free(m_Struct2.pStruct1); What do you think that I am doing wrong??
#include <stdio.h>
#include <memory.h>
#include <malloc.h>
struct MyStruct1 {
int m_interger;
int m_interger2;
int m_interger3;
int m_interger4;
};
struct MyStruct2 {
MyStruct1 *pStruct1;
};
class MyObject {
public:
MyObject();
~MyObject();
MyStruct2 m_Struct2;
};
MyObject::MyObject()
{
m_Struct2.pStruct1 = (MyStruct1 *) malloc(10);
m_Struct2.pStruct1->m_interger = 5;
m_Struct2.pStruct1->m_interger2 = 5;
m_Struct2.pStruct1->m_interger3 = 5;
m_Struct2.pStruct1->m_interger4 = 5;
}
MyObject::~MyObject()
{
if(m_Struct2.pStruct1)
free(m_Struct2.pStruct1);
m_Struct2.pStruct1 = NULL;
}
main() {
MyObject *myObject;
myObject = new MyObject;
delete(myObject);
return 0;
}
|
|
|
|
|
Actually, I think as I type it out onto the editor, I saw one problem with my logic.
m_Struct2.pStruct1 = (MyStruct1 *) malloc(10); <- Bad isn't it?
That ought to be
m_Struct2.pStruct1 = (MyStruct1 *) malloc(sizeof(MyStruct1) * 10);
Otherwise m_Struct2 is not getting created enough space for it's field..
Silly me.. Thanks for the heads up.
|
|
|
|
|
It looks like you need to use new instead of malloc() so that MyStruct1's constructor gets called. Maybe that's just for MFC, however, and not C++ in general.
|
|
|
|
|
new is c++ all the way, no MFC required
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
In the "Hello World!" sample, I put some code of mine, it looks like following:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
...<br />
<br />
static HWND hwndButton,hwndEdit,hwndList;<br />
switch (message) <br />
{<br />
case WM_CREATE:<br />
hwndButton=CreateWindow("BUTTON","Hell&o",WS_CHILD|WS_VISIBLE|WS_BORDER,10,10,100,20,hWnd,(HMENU)1001,hInst,NULL);<br />
hwndEdit=CreateWindow("EDIT",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,10,50,100,20,hWnd,(HMENU)1002,hInst,NULL);<br />
hwndList=CreateWindow("LISTBOX",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,10,90,100,100,hWnd,(HMENU)1003,hInst,NULL);<br />
break;<br />
case WM_COMMAND:<br />
if(LOWORD(wParam)==1001)<br />
MessageBox(hWnd,"hello",NULL,MB_OK);<br />
break;<br />
...<br />
}
When I clicked (use mouse) in Button, it shows a "hello" messagebox and in its caption, 'o' char is underlined
but when I am typing in Edit(Edit got focus) or ListBox got focus, I use keyboard to press Alt-O, I dont see "hello" messagebox
And when I pressed TAB key, focus dont move between my controls
Could you show me the way to help me
thank you so much!
|
|
|
|
|
Try adding WS_TABSTOP to the style for each control when you create it:
hwndButton=CreateWindow("BUTTON","Hell&o",WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,10,10,100,20,hWnd,(HMENU)1001,hInst,NULL);
hwndEdit=CreateWindow("EDIT",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,10,50,100,20,hWnd,(HMENU)1002,hInst,NULL);
hwndList=CreateWindow("LISTBOX",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,10,90,100,100,hWnd,(HMENU)1003,hInst,NULL);
Good luck with it,
Chris Richardson Terrain Software
|
|
|
|
|
thank but sorry
I still dont see that msgbox when press Alt-O
|
|
|
|
|
Strange. Maybe it's something messed up in the message loop. Can you post all the code?
Chris Richardson Terrain Software
|
|
|
|
|
this is my code
it looks very simple
mycode
|
|
|
|
|
Here is my problem.
I am writting a console application and I need to connect to a database in order to either execute a stored procedure or to execute a query. Then I want to store the results into an array and use it for other purposes throughout the program. My problem is how do I connect, and how to I execute and retreive the results. I've tried using a wizard, but it creates so much code that I don't understand. Is there a library that I can pull in and use it?
My database is accessable via ODBC, I've tested the connection and it works.
Assuming the ODBC name for the database is "Database1" and the stored procedure name is "sp1", how would I code it?
ICXC
NIKA
|
|
|
|
|
The following classes should help you. The first is the .H file, followed by the .cpp.
#if !defined(_DBCONN_H_)
#define _DBCONN_H_
#ifdef RETCODE
#undef RETCODE
#endif
#ifndef FAR
#define FAR __far
#endif
#ifdef ODBCVER
#undef ODBCVER
#endif
#define ODBCVER 0x0100
#include <sqlext.h>
#include <afx.h>
#include <malloc.h>
#define STATIC_BUFFER_SIZE 1024
class DBStatement;
class AFX_EXT_CLASS DBConnection
{
protected:
HDBC m_hConnHdl;
long m_lErrorCode;
char m_caErrorMsg[SQL_MAX_MESSAGE_LENGTH];
BOOL m_bDisableStatementScanning;
public:
static HENV m_hEnvironment;
DBConnection();
virtual ~DBConnection();
HDBC GetConnHdl() { return m_hConnHdl; }
BOOL IsDisableStatementScanning() { return m_bDisableStatementScanning; }
void DisableStatementScanning(BOOL bDisable = TRUE) { m_bDisableStatementScanning = bDisable; }
// startup/shutdown functions
static void InitODBC();
static void DeInitODBC();
// connection functions
BOOL Connect(LPCSTR cpDSN, LPCSTR cpUID, LPCSTR cpPWD);
BOOL Disconnect();
// statement functions
DBStatement *ExecSQL(LPCTSTR cpSQLText, DBStatement *pStmt = NULL);
// Error Functions
long GetErrorCode() { return m_lErrorCode; }
LPSTR GetErrorMessage() { return m_caErrorMsg; }
long GetErrorInfo(LPSTR cpErrorText) { if (cpErrorText) strcpy(cpErrorText,m_caErrorMsg); return m_lErrorCode; }
void SetErrorCode(long lErrorCode = 0) { m_lErrorCode = lErrorCode; }
void SetErrorText(LPCSTR cpErrorText) { strcpy(m_caErrorMsg,cpErrorText); }
void SetErrorInfo(long lErrorCode, LPCSTR cpErrorText)
{ m_lErrorCode = lErrorCode; strcpy(m_caErrorMsg,cpErrorText); }
void ResetError() { if (m_lErrorCode) { m_lErrorCode = 0; m_caErrorMsg[0] = 0; } }
void UpdateDBError(HSTMT pStmt = NULL);
};
class AFX_EXT_CLASS DBStatement
{
protected:
HSTMT m_hStmtHdl;
DBConnection *m_pDBConn;
char m_caDataBuf[STATIC_BUFFER_SIZE+1];
BOOL m_bIsDataNull;
BOOL m_bStatementActive;
SYSTEMTIME m_sDateTime;
void InitDataBuffer() { m_caDataBuf[0] = 0; }
public:
DBStatement(DBConnection *pDBConn);
virtual ~DBStatement();
HSTMT GetStmtHdl() { return m_hStmtHdl; }
DBConnection *GetDBConn() { return m_pDBConn; }
BOOL AllocStatement();
BOOL EndSQL();
BOOL IsStatementActive() { return m_bStatementActive; }
void SetStatementActive() { m_bStatementActive = TRUE; }
// data retrieval functions
char *GetString(int iColumnNum, long *lpDataSize = NULL);
void GetString(int iColumnNum, CString *pStr, long *lpDataSize = NULL);
void GetCharBuf(int iColumnNum, char *cpValueBuf, long *lpDataSize = NULL);
double GetDouble(int iColumnNum);
void GetDoubleBuf(int iColumnNum, double *dpValueBuf);
short GetShort(int iColumnNum);
void GetShortBuf(int iColumnNum, short *ipValueBuf);
WORD GetWORD(int iColumnNum) { return (WORD)GetShort(iColumnNum); }
DWORD GetDWORD(int iColumnNum) { return (DWORD)GetLong(iColumnNum); }
long GetLong(int iColumnNum);
void GetLongBuf(int iColumnNum, long *lpValueBuf);
char *GetBinary(int iColumnNum, long *lpDataSize = NULL);
void GetBinary(int iColumnNum, char *cpDataBuf, int iBufferSize, long *lpDataSize = NULL);
SYSTEMTIME *GetDate(int iColumnNum, SYSTEMTIME *spDateTime = NULL);
BYTE *GetBlob(int iColumnNum, long *lpDataSize = NULL);
void SetNullValue(BOOL bIsDataNull = TRUE) { m_bIsDataNull = bIsDataNull; }
BOOL IsNullValue() { return m_bIsDataNull; }
// cursor functions
BOOL FetchNext();
BOOL NextResultSet();
// statement info functions
short GetColType(int iColumnNum); // simplified column type
short GetColDBType(int iColumnNum); // native odbc column type
LPSTR GetColName(int iColumnNum);
void GetColNameBuf(int iColumnNum, LPTSTR cpColNameBuf);
long GetColWidth(int iColumnNum);
int GetNumCols();
// Error Functions
long GetErrorCode() { return m_pDBConn->GetErrorCode(); }
LPSTR GetErrorMessage() { return m_pDBConn->GetErrorMessage(); }
long GetErrorInfo(LPSTR cpErrorText) { return m_pDBConn->GetErrorInfo(cpErrorText); }
void SetErrorCode(long lErrorCode = 0) { m_pDBConn->SetErrorCode(lErrorCode); }
void SetErrorText(LPCSTR cpErrorText) { m_pDBConn->SetErrorText(cpErrorText); }
void SetErrorInfo(long lErrorCode, LPCSTR cpErrorText)
{ m_pDBConn->SetErrorInfo(lErrorCode,cpErrorText); }
void ResetError() { m_pDBConn->ResetError(); }
void UpdateDBError() { m_pDBConn->UpdateDBError(m_hStmtHdl); }
void NoActiveStatement() { SetErrorInfo(-10, "No Active Statement Exists!"); }
};
#endif
///////////////////////////////////
#include "stdafx.h"
#include "dbconn.h"
// init statics
HENV DBConnection::m_hEnvironment = NULL;
DBStatement::DBStatement(DBConnection *pDBConn)
{
m_pDBConn = pDBConn;
m_hStmtHdl = NULL;
m_caDataBuf[0] = 0;
m_bIsDataNull = FALSE;
m_bStatementActive = FALSE;
memset(&m_sDateTime,0,sizeof(SYSTEMTIME));
}
DBStatement::~DBStatement()
{
if (m_hStmtHdl)
{
::SQLFreeStmt(m_hStmtHdl,SQL_DROP);
m_hStmtHdl = NULL;
}
}
BOOL DBStatement::AllocStatement()
{
BOOL bRetval = TRUE;
if (!m_hStmtHdl)
{
short iStatus = ::SQLAllocStmt(m_pDBConn->GetConnHdl(),&m_hStmtHdl);
if (iStatus == SQL_SUCCESS)
{
::SQLSetStmtOption(m_hStmtHdl,SQL_ASYNC_ENABLE,SQL_ASYNC_ENABLE_OFF);
if (m_pDBConn->IsDisableStatementScanning())
::SQLSetStmtOption(m_hStmtHdl,SQL_NOSCAN,SQL_NOSCAN_ON);
}
else
{
SetErrorInfo(-8, "Unable to allocate statement handle!");
bRetval = FALSE;
m_hStmtHdl = NULL;
}
}
return bRetval;
}
BOOL DBStatement::EndSQL()
{
BOOL bRetval = FALSE;
if (m_hStmtHdl)
{
m_bStatementActive = FALSE;
bRetval = TRUE;
::SQLFreeStmt(m_hStmtHdl,SQL_CLOSE);
m_hStmtHdl = NULL;
}
else
NoActiveStatement();
return bRetval;
}
char *DBStatement::GetString(int iColumnNum, long *lpDataSize /*=NULL*/)
{
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
InitDataBuffer();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_CHAR,(LPSTR)m_caDataBuf,STATIC_BUFFER_SIZE,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
m_caDataBuf[0] = 0;
else
{
if (iStatus == SQL_SUCCESS_WITH_INFO)
{
iStatus = SQL_SUCCESS;
m_caDataBuf[STATIC_BUFFER_SIZE] = 0;
}
else
{
if (lColSize != SQL_NO_TOTAL && lColSize <= STATIC_BUFFER_SIZE)
m_caDataBuf[lColSize] = 0;
}
}
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
if (lpDataSize)
*lpDataSize = lColSize;
}
else
NoActiveStatement();
if (iStatus != SQL_SUCCESS)
m_caDataBuf[0] = 0;
return m_caDataBuf;
}
void DBStatement::GetString(int iColumnNum, CString *pStr, long *lpDataSize /*=NULL*/)
{
short iStatus = -1;
pStr->Empty();
if (m_hStmtHdl)
{
ResetError();
InitDataBuffer();
long lColSize = 0;
long lDataReturned = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_CHAR,(LPSTR)m_caDataBuf,STATIC_BUFFER_SIZE,&lDataReturned);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lDataReturned == SQL_NULL_DATA)
{
lColSize = 0;
}
else
{
if (iStatus == SQL_SUCCESS_WITH_INFO)
{
while (iStatus == SQL_SUCCESS_WITH_INFO && lDataReturned > 0)
{
lColSize += STATIC_BUFFER_SIZE;
m_caDataBuf[STATIC_BUFFER_SIZE] = 0;
*pStr += m_caDataBuf;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_CHAR,(LPSTR)m_caDataBuf,STATIC_BUFFER_SIZE,&lDataReturned);
}
if (iStatus == SQL_SUCCESS)
{
lColSize += (lDataReturned - 1);
m_caDataBuf[min(STATIC_BUFFER_SIZE,lDataReturned)] = 0;
*pStr += m_caDataBuf;
}
m_caDataBuf[0] = 0;
iStatus = SQL_SUCCESS;
}
else
{
if (lDataReturned != SQL_NO_TOTAL && lDataReturned <= STATIC_BUFFER_SIZE)
m_caDataBuf[lDataReturned] = 0;
lColSize = lDataReturned;
}
}
if (lColSize > 0 && m_caDataBuf[0])
*pStr += m_caDataBuf;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
if (lpDataSize)
*lpDataSize = lColSize;
}
else
NoActiveStatement();
if (iStatus != SQL_SUCCESS)
pStr->Empty();
}
void DBStatement::GetCharBuf(int iColumnNum, char *cpValueBuf, long *lpDataSize /*=NULL*/)
{
short iStatus = -1;
*cpValueBuf = 0;
if (m_hStmtHdl)
{
ResetError();
InitDataBuffer();
long lColSize = 0;
long lDataReturned = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_CHAR,(LPSTR)m_caDataBuf,STATIC_BUFFER_SIZE,&lDataReturned);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lDataReturned == SQL_NULL_DATA)
{
lColSize = 0;
m_caDataBuf[0] = 0;
}
else
{
if (iStatus == SQL_SUCCESS_WITH_INFO)
{
while (iStatus == SQL_SUCCESS_WITH_INFO && lDataReturned > 0)
{
lColSize += STATIC_BUFFER_SIZE;
m_caDataBuf[STATIC_BUFFER_SIZE] = 0;
strcat(cpValueBuf,m_caDataBuf);
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_CHAR,(LPSTR)m_caDataBuf,STATIC_BUFFER_SIZE,&lDataReturned);
}
if (iStatus == SQL_SUCCESS)
{
lColSize += (lDataReturned - 1);
m_caDataBuf[min(STATIC_BUFFER_SIZE,lDataReturned)] = 0;
strcat(cpValueBuf,m_caDataBuf);
}
m_caDataBuf[0] = 0;
iStatus = SQL_SUCCESS;
}
else
{
if (lDataReturned != SQL_NO_TOTAL && lDataReturned <= STATIC_BUFFER_SIZE)
m_caDataBuf[lDataReturned] = 0;
lColSize = lDataReturned;
}
}
if (lColSize > 0 && m_caDataBuf[0])
strcat(cpValueBuf,m_caDataBuf);
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
if (lpDataSize)
*lpDataSize = lColSize;
}
else
NoActiveStatement();
if (iStatus != SQL_SUCCESS)
*cpValueBuf = 0;
}
double DBStatement::GetDouble(int iColumnNum)
{
double dRetval = 0.0;
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_DOUBLE,(LPSTR)&dRetval,8,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
dRetval = 0.0;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
}
else
NoActiveStatement();
return dRetval;
}
void DBStatement::GetDoubleBuf(int iColumnNum, double *dpValueBuf)
{
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_DOUBLE,(LPSTR)dpValueBuf,8,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
*dpValueBuf = 0.0;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
}
else
NoActiveStatement();
}
short DBStatement::GetShort(int iColumnNum)
{
short iRetval = 0;
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_SHORT,(LPSTR)&iRetval,2,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
iRetval = 0;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
}
else
NoActiveStatement();
return iRetval;
}
void DBStatement::GetShortBuf(int iColumnNum, short *ipValueBuf)
{
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_SHORT,(LPSTR)ipValueBuf,2,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
*ipValueBuf = 0;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
}
else
NoActiveStatement();
}
long DBStatement::GetLong(int iColumnNum)
{
long lRetval = 0;
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_LONG,(LPSTR)&lRetval,4,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
lRetval = 0;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
}
else
NoActiveStatement();
return lRetval;
}
void DBStatement::GetLongBuf(int iColumnNum, long *lpValueBuf)
{
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_LONG,(LPSTR)lpValueBuf,4,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
*lpValueBuf = 0;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
}
else
NoActiveStatement();
}
char *DBStatement::GetBinary(int iColumnNum, long *lpDataSize /*=NULL*/)
{
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
InitDataBuffer();
memset(m_caDataBuf,0,STATIC_BUFFER_SIZE);
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_BINARY,(LPSTR)m_caDataBuf,STATIC_BUFFER_SIZE,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (iStatus == SQL_SUCCESS_WITH_INFO)
iStatus = SQL_SUCCESS;
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
if (lpDataSize)
*lpDataSize = lColSize;
}
else
NoActiveStatement();
return m_caDataBuf;
}
void DBStatement::GetBinary(int iColumnNum, char *cpDataBuf, int iBufferSize, long *lpDataSize /*=NULL*/)
{
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_BINARY,(LPSTR)cpDataBuf,iBufferSize,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize == SQL_NULL_DATA)
memset(cpDataBuf,0,iBufferSize);
else
{
if (iStatus == SQL_SUCCESS_WITH_INFO)
iStatus = SQL_SUCCESS;
}
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
if (lpDataSize)
*lpDataSize = lColSize;
}
else
NoActiveStatement();
}
SYSTEMTIME *DBStatement::GetDate(int iColumnNum, SYSTEMTIME *spDateTime /*=NULL*/)
{
short iStatus = -1;
if (!spDateTime)
spDateTime = &m_sDateTime;
memset(spDateTime,0,sizeof(SYSTEMTIME));
if (m_hStmtHdl)
{
ResetError();
SQL_TIMESTAMP_STRUCT sTS;
long lColSize = 0;
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_TIMESTAMP,&sTS,sizeof(SQL_TIMESTAMP_STRUCT),&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (iStatus == SQL_SUCCESS_WITH_INFO)
iStatus = SQL_SUCCESS;
SetNullValue(lColSize == SQL_NULL_DATA);
if (lColSize != SQL_NULL_DATA)
{
spDateTime->wYear = sTS.year;
spDateTime->wMonth = sTS.month;
spDateTime->wDay = sTS.day;
spDateTime->wHour = sTS.hour;
spDateTime->wMinute = sTS.minute;
spDateTime->wSecond = sTS.second;
// spDateTimeStruct->wMilliseconds = (USHORT)sTS.fraction;
}
}
else
UpdateDBError();
}
else
NoActiveStatement();
return spDateTime;
}
BYTE *DBStatement::GetBlob(int iColumnNum, long *lpDataSize /*=NULL*/)
{
BYTE *pRetval = NULL;
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
long lColSize = 0;
char caDummy[20];
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_BINARY,caDummy,0,&lColSize);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
{
if (lColSize != SQL_NULL_DATA)
{
if (iStatus == SQL_SUCCESS_WITH_INFO && lColSize > 0)
{
pRetval = new BYTE[lColSize+1]; // add 1 just in case
iStatus = ::SQLGetData(m_hStmtHdl,iColumnNum,SQL_C_BINARY,pRetval,lColSize,&lColSize);
if (iStatus != SQL_SUCCESS)
{
UpdateDBError();
delete [] pRetval;
pRetval = NULL;
}
else
*(pRetval+lColSize) = 0; // null terminate
}
}
SetNullValue(lColSize == SQL_NULL_DATA);
}
else
UpdateDBError();
if (lpDataSize)
*lpDataSize = lColSize;
}
else
NoActiveStatement();
return pRetval;
}
BOOL DBStatement::FetchNext()
{
short iStatus = -1;
if (m_hStmtHdl)
{
ResetError();
iStatus = ::SQLFetch(m_hStmtHdl);
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO)
iStatus = SQL_SUCCESS;
else
UpdateDBError();
}
else
NoActiveStatement();
return (iStatus == SQL_SUCCESS);
}
BOOL DBStatement::NextResultSet()
{
BOOL bRetval = FALSE;
if (m_hStmtHdl)
{
ResetError();
if (::SQLMoreResults(m_hStmtHdl) == SQL_SUCCESS)
bRetval = TRUE;
}
else
NoActiveStatement();
return bRetval;
}
short DBStatement::GetColType(int iColumnNum)
{
short iRetval = -1;
ResetError();
short iColType = GetColDBType(iColumnNum);
switch(iColType){
case SQL_CHAR:
iRetval = 1;
break;
case SQL_LONGVARCHAR:
case SQL_VARCHAR:
iRetval = 2;
break;
case SQL_NUMERIC:
case SQL_DECIMAL:
iRetval = 3;
break;
case SQL_INTEGER:
iRetval = 4;
break;
case SQL_BIT:
case SQL_TINYINT:
case SQL_SMALLINT:
iRetval = 5;
break;
case SQL_FLOAT:
case SQL_REAL:
iRetval = 6;
break;
case SQL_DOUBLE:
iRetval = 7;
break;
case SQL_DATE:
case SQL_TIME:
case SQL_TIMESTAMP:
iRetval = 8;
break;
case SQL_BINARY:
case SQL_VARBINARY:
iRetval = 9;
break;
case SQL_LONGVARBINARY: // blob
iRetval = 10;
break;
default:
iRetval = 2; // default to varchar...
break;
}
return iRetval;
}
short DBStatement::GetColDBType(int iColumnNum)
{
short iStatus;
char caDummy[2];
short iDummy;
long lColType = -1;
if (m_hStmtHdl)
{
ResetError();
iStatus = ::SQLColAttributes(m_hStmtHdl,(UWORD)iColumnNum,(UWORD)SQL_COLUMN_TYPE,(PTR)caDummy,(SWORD)1,(SWORD *)&iDummy,(SDWORD *)&lColType);
if (iStatus != SQL_SUCCESS && iStatus != SQL_SUCCESS_WITH_INFO)
{
UpdateDBError();
lColType = -1;
}
}
else
NoActiveStatement();
return (short)lColType;
}
LPSTR DBStatement::GetColName(int iColumnNum)
{
short iStatus;
long lDummy;
short iDummy;
InitDataBuffer();
if (m_hStmtHdl)
{
ResetError();
iStatus = ::SQLColAttributes(m_hStmtHdl,(UWORD)iColumnNum,(UWORD)SQL_COLUMN_NAME,(PTR)m_caDataBuf,(SWORD)1000,(SWORD *)&iDummy,(SDWORD *)&lDummy);
if (iStatus != SQL_SUCCESS && iStatus != SQL_SUCCESS_WITH_INFO)
{
UpdateDBError();
m_caDataBuf[0] = 0;
}
}
else
NoActiveStatement();
return m_caDataBuf;
}
void DBStatement::GetColNameBuf(int iColumnNum, LPTSTR cpColNameBuf)
{
short iStatus;
long lDummy;
short iDummy;
if (m_hStmtHdl)
{
ResetError();
iStatus = ::SQLColAttributes(m_hStmtHdl,(UWORD)iColumnNum,(UWORD)SQL_COLUMN_NAME,(PTR)cpColNameBuf,(SWORD)1000,(SWORD *)&iDummy,(SDWORD *)&lDummy);
if (iStatus != SQL_SUCCESS && iStatus != SQL_SUCCESS_WITH_INFO)
{
UpdateDBError();
*cpColNameBuf = 0;
}
}
else
NoActiveStatement();
}
long DBStatement::GetColWidth(int iColumnNum)
{
short iStatus;
char caDummy[2];
long lColWidth = -1;
short iDummy;
if (m_hStmtHdl)
{
ResetError();
iStatus = ::SQLColAttributes(m_hStmtHdl,(UWORD)iColumnNum,(UWORD)SQL_COLUMN_LENGTH,(PTR)caDummy,(SWORD)1,(SWORD *)&iDummy,(SDWORD *)&lColWidth);
if (iStatus != SQL_SUCCESS && iStatus != SQL_SUCCESS_WITH_INFO)
UpdateDBError();
}
else
NoActiveStatement();
return lColWidth;
}
int DBStatement::GetNumCols()
{
short iStatus;
short iRetval = -1;
if (m_hStmtHdl)
{
ResetError();
iStatus = ::SQLNumResultCols(m_hStmtHdl,(short *)&iRetval);
if (iStatus != SQL_SUCCESS && iStatus != SQL_SUCCESS_WITH_INFO)
UpdateDBError();
}
else
NoActiveStatement();
return (int)iRetval;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
DBConnection::DBConnection()
{
m_hConnHdl = NULL;
m_lErrorCode = 0;
m_caErrorMsg[0] = 0;
m_bDisableStatementScanning = FALSE;
}
DBConnection::~DBConnection()
{
if (m_hConnHdl)
Disconnect();
}
void DBConnection::InitODBC()
{
if (!m_hEnvironment)
::SQLAllocEnv(&m_hEnvironment);
}
void DBConnection::DeInitODBC()
{
if (m_hEnvironment)
{
::SQLFreeEnv(m_hEnvironment);
m_hEnvironment = NULL;
}
}
void DBConnection::UpdateDBError(HSTMT pStmt /*=NULL*/)
{
char caSQLState[21];
short iError = -1;
caSQLState[0] = 0;
m_caErrorMsg[0] = 0;
::SQLError(m_hEnvironment,m_hConnHdl,pStmt,(unsigned char *)caSQLState,&m_lErrorCode,(unsigned char *)m_caErrorMsg,sizeof(m_caErrorMsg)-1,(short *)&iError);
}
BOOL DBConnection::Connect(LPCSTR cpDSN, LPCSTR cpUID, LPCSTR cpPWD)
{
if (m_hConnHdl)
{
SetErrorInfo(-5,"Already Connected!");
return FALSE;
}
ResetError();
short iStatus = ::SQLAllocConnect(m_hEnvironment,&m_hConnHdl);
if (iStatus == SQL_SUCCESS)
{
iStatus = ::SQLConnect(m_hConnHdl,
(UCHAR *)cpDSN,(SWORD)strlen(cpDSN),
(UCHAR *)cpUID,(SWORD)strlen(cpUID),
(UCHAR *)cpPWD,(SWORD)strlen(cpPWD));
if (iStatus != SQL_SUCCESS && iStatus != SQL_SUCCESS_WITH_INFO)
{
UpdateDBError();
::SQLFreeConnect(m_hConnHdl);
m_hConnHdl = NULL;
}
iStatus = 0;
}
else
{
m_hConnHdl = NULL;
}
if (iStatus)
UpdateDBError();
return (m_hConnHdl != NULL);
}
BOOL DBConnection::Disconnect()
{
short iStatus = -1;
if (m_hConnHdl)
{
iStatus = ::SQLDisconnect(m_hConnHdl);
if (iStatus == SQL_SUCCESS)
{
iStatus = ::SQLFreeConnect(m_hConnHdl);
m_hConnHdl = NULL;
}
else
UpdateDBError();
}
else
{
SetErrorInfo(-6,"Not Connected!");
}
return (iStatus == SQL_SUCCESS);
}
DBStatement *DBConnection::ExecSQL(LPCTSTR cpSQLText, DBStatement *pStmt /*=NULL*/)
{
DBStatement *pRetval = NULL;
if (m_hConnHdl)
{
if (pStmt)
pRetval = pStmt;
else
pRetval = new DBStatement(this);
if (pRetval)
{
if (pRetval->AllocStatement())
{
short iStatus = ::SQLExecDirect(pRetval->GetStmtHdl(),
(unsigned char *)cpSQLText,
strlen(cpSQLText));
if (iStatus == SQL_SUCCESS || iStatus == SQL_SUCCESS_WITH_INFO || iStatus == SQL_NO_DATA_FOUND)
{
iStatus = SQL_SUCCESS;
pRetval->SetStatementActive();
}
else
{
UpdateDBError(pRetval->GetStmtHdl());
if (!pStmt)
delete pRetval;
else
pStmt->EndSQL();
pRetval = NULL;
}
}
else
{
if (!pStmt)
delete pRetval;
pRetval = NULL;
}
}
}
else
{
SetErrorInfo(-6,"Not Connected!");
}
return pRetval;
}
////////////////////
In your main startup function, add the line:
DBConnection::InitODBC();
In your exit function add the line
DBConnection::DeInitODBC();
In your processing function, add the following code:
DBConnection oDBConn;
if (oDBConn.Connect("Database1", "sa", "the sa password"))
{
DBStatement *pStmt = oDBConn.ExecSQL("exec databasename..sp1");
if (pStmt)
{
int iIndex = 0;
while (pStmt->FetchNext())
{
YourArray[iIndex] = pStmt->GetString | pStmt->GetLong | pStmt->GetDouble.... etc.
iIndex++;
}
pStmt->EndSQL();
delete pStmt;
}
else
{
// handle error using oDBConn.GetErrorCode,
// oDBConn.GetErrorMessage, etc..
}
oDBConn.Disconnect();
}
else
{
// handle error using oDBConn.GetErrorCode,
// oDBConn.GetErrorMessage, etc..
}
Hope this helps...
|
|
|
|
|