Monday, July 11, 2011

Microsoft Visual C++

Steps to create DLL
 The following steps illustrate how to create a Microsoft Visual C++ DLL project and then add and export the talkToObject() routine.

Create a new Microsoft Visual C++ 5.0 "MFC AppWizard (dll)" project named "vcvbdll" and accept all of the default options.
Add the following code to the end of the vcvbdll.cpp file.
 // Helper message function...
      void ShowMsg(char *msg, HRESULT hr) {
            char buf[1024];
            if((long)hr) {
               sprintf(buf, "%s, HRESULT = %08lx", msg, (long)hr);
            }
            else {
               sprintf(buf, "%s", msg);
            }
            ::MessageBox(NULL, buf, "C/C++ DLL message",
                         MB_SETFOREGROUND | MB_OK);
      }

      // The exported function, called from Microsoft Visual Basic...
      void _stdcall talkToObject(IUnknown *pUnk) {
            // QueryInterface for a IDispatch pointer...
            IDispatch *pDisp;
            HRESULT hr = pUnk->QueryInterface(IID_IDispatch,
                                             (void **)&pDisp);
            if(FAILED(hr)) {
               ShowMsg("QueryInterface() failed", hr);
            }
            else {
               ShowMsg("We got the dispatch pointer!!!", hr);

               // Attach dispatch to a COleDispatchDriver class.
               COleDispatchDriver disp(pDisp); // Uses constructor

               // Set visible to FALSE...
               static BYTE parms[] = VTS_BOOL;
               disp.InvokeHelper(0x17, DISPATCH_PROPERTYPUT, VT_EMPTY,
                                 NULL, parms, FALSE);


               ShowMsg("Microsoft Word 97 shouldn't be visible now.", 0);

               // Set visible to TRUE...
               disp.InvokeHelper(0x17, DISPATCH_PROPERTYPUT, VT_EMPTY,
                                 NULL, parms, TRUE);
               ShowMsg("Microsoft Word 97 should now be visible again!",
                       0);
         }
      }

Add the following line tothe end of the vcvbdll.def file so that the talkToObject function is exported:      talkToObject
                   
Compile and then copy the .dll to the \Windows\System directory for testing.

No comments:

Post a Comment

 
THANK YOU FOR VISITING