Skip to content

Instantly share code, notes, and snippets.

@lynxluna
Created October 16, 2024 04:33
Show Gist options
  • Save lynxluna/e55ae5e133d7eb60454d4c916953000f to your computer and use it in GitHub Desktop.
Save lynxluna/e55ae5e133d7eb60454d4c916953000f to your computer and use it in GitHub Desktop.
OLEDB access
#define INITGUID
#include <windows.h>
#include <ole2.h>
#include <oledb.h>
#include <stdio.h>
#include <tchar.h>
// Include OLEDB headers
#include <msdasc.h>
#pragma comment(lib, "oledb.lib")
// OLEDB GUIDs
#include <msdaguid.h>
struct chara_stat_t {
unsigned short attack, strength, defense, intelligence;
};
struct chara_t {
char name[256];
struct chara_stat_t stats;
};
int main()
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) {
printf("Failed to initialize COM: 0x%08x\n", hr);
return 1;
}
IDBInitialize* pIDBInitialize = NULL;
IDBProperties* pIDBProperties = NULL;
IDBCreateSession* pIDBCreateSession = NULL;
IDBCreateCommand* pIDBCreateCommand = NULL;
ICommand* pICommand = NULL;
IRowset* pIRowset = NULL;
do {
// Create OLEDB Data Source Object
hr = CoCreateInstance(&CLSID_MSDASQL, NULL, CLSCTX_INPROC_SERVER,
&IID_IDBInitialize, (void**)&pIDBInitialize);
FAILED_BREAK(hr);
// Set connection properties
hr = IDBInitialize_QueryInterface(pIDBInitialize, &IID_IDBProperties, (void**)&pIDBProperties);
FAILED_BREAK(hr);
DBPROP dbProps[1];
DBPROPSET dbPropSet;
// Set the connection string
dbProps[0].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
dbProps[0].vValue.vt = VT_BSTR;
dbProps[0].vValue.bstrVal = SysAllocString(L"Driver={SQL Server};Server=ServerName;Database=DatabaseName;Trusted_Connection=yes;");
dbProps[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbProps[0].colid = DB_NULLID;
dbPropSet.guidPropertySet = DBPROPSET_DBINIT;
dbPropSet.cProperties = 1;
dbPropSet.rgProperties = dbProps;
hr = IDBProperties_SetProperties(pIDBProperties, 1, &dbPropSet);
FAILED_BREAK(hr);
// Initialize and establish connection
hr = IDBInitialize_Initialize(pIDBInitialize);
FAILED_BREAK(hr);
// Create Session
hr = IDBInitialize_QueryInterface(pIDBInitialize, &IID_IDBCreateSession, (void**)&pIDBCreateSession);
FAILED_BREAK(hr);
hr = IDBCreateSession_CreateSession(pIDBCreateSession, NULL, &IID_IDBCreateCommand, (IUnknown**)&pIDBCreateCommand);
FAILED_BREAK(hr);
// Create Command
hr = IDBCreateCommand_CreateCommand(pIDBCreateCommand, NULL, &IID_ICommand, (IUnknown**)&pICommand);
FAILED_BREAK(hr);
// Set SQL command
WCHAR wszSQL[] = L"SELECT name, atk, str, def, int FROM AccountTable";
DBPARAMS params;
params.pData = NULL;
params.cParamSets = 0;
params.hAccessor = NULL;
hr = ICommand_SetCommandText(pICommand, &DBGUID_DBSQL, wszSQL);
FAILED_BREAK(hr);
// Execute command
hr = ICommand_Execute(pICommand, NULL, &IID_IRowset, &params, NULL, (IUnknown**)&pIRowset);
FAILED_BREAK(hr);
// Fetch and print results
IAccessor* pIAccessor = NULL;
hr = IRowset_QueryInterface(pIRowset, &IID_IAccessor, (void**)&pIAccessor);
FAILED_BREAK(hr);
DBBINDING bindings[5];
DBBINDSTATUS bindStatus[5];
bindings[0].iOrdinal = 1; // name
bindings[0].obValue = offsetof(struct chara_t, name);
bindings[0].obLength = 0;
bindings[0].obStatus = 0;
bindings[0].pTypeInfo = NULL;
bindings[0].pObject = NULL;
bindings[0].pBindExt = NULL;
bindings[0].dwPart = DBPART_VALUE;
bindings[0].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
bindings[0].dwFlags = 0;
bindings[0].eParamIO = DBPARAMIO_NOTPARAM;
bindings[0].cbMaxLen = 256;
bindings[0].wType = DBTYPE_STR;
bindings[0].bPrecision = 0;
bindings[0].bScale = 0;
for (int i = 1; i < 5; i++) {
bindings[i].iOrdinal = i + 1; // atk, str, def, int
bindings[i].obValue = offsetof(struct chara_t, stats) + (i - 1) * sizeof(unsigned short);
bindings[i].obLength = 0;
bindings[i].obStatus = 0;
bindings[i].pTypeInfo = NULL;
bindings[i].pObject = NULL;
bindings[i].pBindExt = NULL;
bindings[i].dwPart = DBPART_VALUE;
bindings[i].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
bindings[i].dwFlags = 0;
bindings[i].eParamIO = DBPARAMIO_NOTPARAM;
bindings[i].cbMaxLen = sizeof(unsigned short);
bindings[i].wType = DBTYPE_I2;
bindings[i].bPrecision = 0;
bindings[i].bScale = 0;
}
HACCESSOR hAccessor;
hr = IAccessor_CreateAccessor(pIAccessor, DBACCESSOR_ROWDATA, 5, bindings, sizeof(struct chara_t), &hAccessor, bindStatus);
FAILED_BREAK(hr);
HROW hRow;
HROW* pRows = &hRow;
DBCOUNTITEM cRowsObtained;
struct chara_t character;
while (TRUE) {
hr = IRowset_GetNextRows(pIRowset, DB_NULL_HCHAPTER, 0, 1, &cRowsObtained, &pRows);
if (hr != S_OK) break;
hr = IRowset_GetData(pIRowset, hRow, hAccessor, &character);
FAILED_BREAK(hr);
// Print character info
printf("Name: %s\n", character.name);
printf("Attack: %d\n", character.stats.attack);
printf("Strength: %d\n", character.stats.strength);
printf("Defense: %d\n", character.stats.defense);
printf("Intelligence: %d\n\n", character.stats.intelligence);
IRowset_ReleaseRows(pIRowset, 1, pRows, NULL, NULL, NULL);
}
// Clean up
if (pIAccessor) {
IAccessor_ReleaseAccessor(pIAccessor, hAccessor, NULL);
IAccessor_Release(pIAccessor);
}
if (pIRowset) IRowset_Release(pIRowset);
if (pICommand) ICommand_Release(pICommand);
if (pIDBCreateCommand) IDBCreateCommand_Release(pIDBCreateCommand);
if (pIDBCreateSession) IDBCreateSession_Release(pIDBCreateSession);
if (pIDBProperties) IDBProperties_Release(pIDBProperties);
if (pIDBInitialize) IDBInitialize_Release(pIDBInitialize);
} while (0);
if (FAILED(hr)) {
printf("Error: 0x%08x\n", hr);
}
CoUninitialize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment