home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / oct_dll.pak / DLLRUN.CPP next >
C/C++ Source or Header  |  1997-07-23  |  4KB  |  95 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectConnections - (C) Copyright 1994 by Borland International
  3. //   Simple EXE to load and run a DLL server
  4. //----------------------------------------------------------------------------
  5.  
  6. #define STRICT
  7. #include <windows.h>
  8. #include <shellapi.h>
  9. #include <ole2.h>
  10.  
  11. #define _IFUNC  STDMETHODCALLTYPE
  12. typedef HRESULT STDAPICALLTYPE (*TDllGetClassObject)(const GUID far& clsid,
  13.                                                      const GUID far& iid,
  14.                                                      void far* far* retObj);
  15. class TDummy : public IUnknown {
  16.   HRESULT _IFUNC QueryInterface(const GUID far& iid, void far* far* pif)
  17.   {*pif = 0; return iid==IID_NULL ? NOERROR : ResultFromScode(E_NOINTERFACE);}
  18.   unsigned long _IFUNC AddRef() {return 1;}
  19.   unsigned long _IFUNC Release(){return 0;}
  20. };
  21.  
  22. int PASCAL
  23. WinMain(HINSTANCE/*hInst*/, HINSTANCE/*prev*/, char far* cmdLine, int/*show*/)
  24. {
  25.   char* error = 0;
  26.   const int guidChars = 38;
  27.   const int guidKeySize = sizeof("CLSID\\")-1;
  28.   char guidBuf[guidKeySize + guidChars + 1] = "CLSID\\";
  29.   const int pathSize = 260;
  30.   char path[pathSize];
  31.   long guidBufSize = guidChars + 1;
  32.   long pathBufSize = pathSize;
  33.   long rstat;
  34.   HKEY key;
  35.   if (!cmdLine || !cmdLine[0]) {
  36.     cmdLine = "Error";
  37.     error = "Command line must have DLL ProgId";
  38.   } else if (::RegOpenKey(HKEY_CLASSES_ROOT, cmdLine, &key) != ERROR_SUCCESS) {
  39.      error = "ProgId not found in Registry";
  40.   } else {
  41.     rstat = ::RegQueryValue(key, "CLSID", guidBuf+guidKeySize, &guidBufSize);
  42.      ::RegCloseKey(key);
  43.     if (rstat != ERROR_SUCCESS ||
  44.         ::RegOpenKey(HKEY_CLASSES_ROOT, guidBuf, &key) != ERROR_SUCCESS) {
  45.       error = "CLSID not found in Registry";
  46.     } else {
  47.       rstat = ::RegQueryValue(key, "InprocServer", path, &pathBufSize);
  48.          ::RegCloseKey(key);
  49.       if (rstat != ERROR_SUCCESS) {
  50.         error = "No InprocServer specified in Registry";
  51.       } else {
  52.         ::OleInitialize(0);
  53.         HINSTANCE hLib = ::LoadLibrary(path);
  54.         if (hLib < HINSTANCE_ERROR) {
  55.           error = "Could not load library";
  56.         } else {
  57.           TDllGetClassObject entry = (TDllGetClassObject)::GetProcAddress(hLib,
  58.                                                            "DllGetClassObject");
  59.           if (!entry) {
  60.             error = "Could not find entry point";
  61.           } else {
  62.             IClassFactory* factory;
  63.             GUID guid;
  64.             ::CLSIDFromString(guidBuf+guidKeySize, &guid);
  65.             HRESULT stat = (*entry)(guid, IID_IClassFactory,
  66.                                     (void far*far*)&factory);
  67.             if (stat) {
  68.               error = "Could not obtain factory";
  69.             } else {
  70.               IUnknown* ifc;
  71.               stat = factory->CreateInstance(&TDummy(), IID_IUnknown,
  72.                                              (void far*far*)&ifc);
  73.               factory->Release();
  74.               if (stat) {
  75.                 error = "Could not create object";
  76.               } else {
  77.                 long refs = ifc->Release();
  78.                 char buf[30];
  79.                 wsprintf(buf, "Reference count = %li", refs);
  80.                 if (refs != 0)
  81.                   error = buf;
  82.               }
  83.             }
  84.           }
  85.           ::FreeLibrary(hLib);
  86.         }
  87.         ::OleUninitialize();
  88.       }
  89.     }
  90.   }
  91.   if (error)
  92.     ::MessageBox(0, error, "DLLRUN - (c) Borland 1994", MB_OK);
  93.   return error != 0;
  94. }
  95.