home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / sqldmo / c / soc / soc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  1.7 KB  |  57 lines

  1. #include <stdio.h>
  2.  
  3. #define INITGUID
  4. #define INC_OLE2
  5. #include <windows.h>
  6.  
  7. #include <sqloleid.h>
  8. #include <sqlole.h>
  9.  
  10. // This sample demonstrates how to program SQLOLE in C.  Differences from C++ are noted.
  11. int main (void)
  12. {
  13.     // Variables must be defined at beginning of block, rather than at point of initialization.
  14.     // (If this is done later, the compiler error message is very unhelpful in this case).
  15.     LPSQLOLESERVER pSQL = NULL;
  16.     HRESULT hr;
  17.     if FAILED(hr = CoInitialize (NULL))
  18.     {
  19.         printf("CoInitialize Failed\n");
  20.         return (0);
  21.     }
  22.     printf("Hello\n");
  23.  
  24.     // Must use '&' operator on IID's in C; C++ uses references for these.
  25.     if FAILED(hr = CoCreateInstance(&CLSID_SQLOLEServer, NULL, CLSCTX_INPROC_SERVER,
  26.         &IID_ISQLOLEServer, (LPVOID*)&pSQL))
  27.     {
  28.         printf("CoCreateInstance Failed\n");
  29.         return (0);
  30.     }
  31.  
  32.     // The returned pointer from SQLOLE is interpreted as a pointer to an array of function pointers,
  33.     // which are the C equivalent of C++'s "virtual function table", or vtbl.  Hence in this code,
  34.     // pSQL is a pointer to a pointer to a function table, and the additional indirection of including
  35.     // the actual vtbl pointer (->lpVtbl) is necessary.  Also, the pSQL pointer must be explicitly passed
  36.     // to the called function in C; in C++, this is done implicitly as the "this" pointer.
  37.     pSQL->lpVtbl->SetLoginTimeout(pSQL, 10);
  38.  
  39.     if FAILED(hr = pSQL->lpVtbl->Connect(pSQL, "tedhar2","sa",""))
  40.     {
  41.         HRESULT h = HRESULT_CODE(hr);
  42.         printf("Connect failed\n");
  43.     }
  44.     else
  45.     {
  46.         SQLOLE_BSTR name;
  47.         pSQL->lpVtbl->GetName(pSQL, &name);
  48.         printf("%s\n", name);
  49.         SQLOLEFreeString(name);
  50.     }
  51.  
  52.     pSQL->lpVtbl->Release(pSQL);
  53.     CoUninitialize ();
  54.     printf("Goodbye\n");
  55.     return (0);
  56. }
  57.