home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / sqldmo / c / soc / soc.c next >
Encoding:
C/C++ Source or Header  |  1997-06-06  |  1.7 KB  |  58 lines

  1. #include <stdio.h>
  2.  
  3. #define INITGUID
  4. #define INC_OLE2
  5. #include <windows.h>
  6.  
  7. #include "sqldmoid.h"
  8. #include "sqldmo.h"
  9.  
  10. // This sample demonstrates how to program SQLDMO 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.     LPSQLDMOSERVER 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_SQLDMOServer, NULL, CLSCTX_INPROC_SERVER,
  26.         &IID_ISQLDMOServer, (LPVOID*)&pSQL))
  27.     {
  28.         printf("CoCreateInstance Failed\n");
  29.         return (0);
  30.     }
  31.  
  32.     // The returned pointer from SQLDMO 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, OLESTR(""),
  40.         OLESTR("sa"), OLESTR("")))
  41.     {
  42.         HRESULT h = HRESULT_CODE(hr);
  43.         printf("Connect failed\n");
  44.     }
  45.     else
  46.     {
  47.         SQLDMO_BSTR name;
  48.         pSQL->lpVtbl->GetName(pSQL, &name);
  49.         printf("%S\n", name);
  50.         SQLDMOFreeString(name);
  51.     }
  52.  
  53.     pSQL->lpVtbl->Release(pSQL);
  54.     CoUninitialize ();
  55.     printf("Goodbye\n");
  56.     return (0);
  57. }
  58.