This is my code:
#include <windows.h>
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")
_ConnectionPtr myCon;
_RecordsetPtr recordset;
#define GetItemA() (char*)(_bstr_t)recordset->GetCollect("DestCity")
char* GetItemB() { return (char*)(_bstr_t)recordset->GetCollect("DestCity"); }
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
long count;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
recordset->get_RecordCount(&count);
recordset->AbsolutePosition = (PositionEnum)1;
for(long y=30,i=0;i<count;y+=20,i++)
{
//Column one
TextOut(hdc, 30,y,GetItemA(),6);
//Column two
TextOut(hdc,130,y,GetItemB(),6);
}
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE,LPTSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX wcex = {sizeof(WNDCLASSEX),0,(WNDPROC)WndProc,0,0,hInstance,0,0,(HBRUSH)6,0,"communicator",0};
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow("communicator","title",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,0,0,hInstance,0);
CoInitialize(0);
myCon.CreateInstance(__uuidof(Connection));
myCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Linkdata.mdb","","",adModeUnknown);
recordset.CreateInstance(__uuidof(Recordset));
recordset->CursorLocation = adUseClient;
recordset->Open("SELECT DestCity FROM Leads",(IDispatch*)myCon,adOpenDynamic,adLockUnspecified,adCmdUnknown);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
recordset->Close();
myCon->Close();
CoUninitialize();
return (int) msg.wParam;
}
This is my output:
The bottom line is
(char*)(_bstr_t)recordset->GetCollect("DestCity")
doesn't seem to work unless it is used in the same function that called it and I would like to know why.