Click here to Skip to main content
16,006,749 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Help with Tab controls Pin
Tomasz Sowinski6-Aug-01 11:16
Tomasz Sowinski6-Aug-01 11:16 
GeneralAdjustWindowRect problems Pin
Peter Andersson6-Aug-01 2:16
Peter Andersson6-Aug-01 2:16 
GeneralRe: AdjustWindowRect problems Pin
Not Active6-Aug-01 3:10
mentorNot Active6-Aug-01 3:10 
GeneralRe: AdjustWindowRect problems Pin
Peter Andersson6-Aug-01 3:34
Peter Andersson6-Aug-01 3:34 
GeneralAutomating Visual C++ Project Creation Pin
Steve Thresher6-Aug-01 1:34
Steve Thresher6-Aug-01 1:34 
GeneralRe: Automating Visual C++ Project Creation Pin
J Patel6-Aug-01 11:10
J Patel6-Aug-01 11:10 
GeneralRe: Automating Visual C++ Project Creation Pin
Steve Thresher6-Aug-01 23:09
Steve Thresher6-Aug-01 23:09 
GeneralRe: Automating Visual C++ Project Creation Pin
Tim Deveaux6-Aug-01 12:38
Tim Deveaux6-Aug-01 12:38 
I think this has something to do with IDispatch vs IUnknown - IApplication is a dual interface, so I decided to try using IDispatch::Invoke instead of calling the AddProject interface directly:

#include <stdio.h>
#include <atlbase.h>
 
extern CComModule _Module;
 
#include <atlcom.h>
#include <initguid.h>
#include <comdef.h>
  
#include <ObjModel\appauto.h>
#include <ObjModel\appdefs.h>
#include <ObjModel\appguid.h>
 
// some others left out - bldauto, textauto, dbgauto et al
 
int main(int argc, char* argv[])
{  
    // Initialize COM libraries  
    HRESULT hresult = CoInitialize(NULL);  
    if(FAILED(hresult))  
    {    
        printf("Failed to initialize the COM libraries\n");    
        return FALSE;  
    }  
 
    IUnknown FAR* punk;
    IDispatch FAR* pdisp = (IDispatch FAR*)NULL;
    DISPID dispid;
 
    hresult = CoCreateInstance(CLSID_Application, NULL, CLSCTX_SERVER,
                    IID_IUnknown, (void FAR* FAR*)&punk);
    if(FAILED(hresult))  
    {    
        printf("Failed to create an instance of MSDEV\n");    
        CoUninitialize();  
    }  
 
    // get IDispatch
    hresult = punk->QueryInterface(IID_IDispatch,
                    (void FAR* FAR*)&pdisp);
 
    // get dispatch ID for AddProject
    OLECHAR FAR* szMember = L"AddProject";
    hresult = pdisp->GetIDsOfNames(IID_NULL, &szMember, 1,
                    LOCALE_USER_DEFAULT, &dispid);
    
    VARIANT varResult;
    EXCEPINFO excepInfo;
    unsigned int uArgErr;
    DISPPARAMS dispparams; 
 
    dispparams.rgvarg = new VARIANT[4];
 
    CComBSTR bstrProjectName("project");  
    CComBSTR bstrPath("c:\\myproject\\project");  
    CComBSTR bstrType("Application");  
 
    // set up params - note reverse order - is this why people love COM ???
    dispparams.rgvarg[0].vt = VT_BOOL;
    dispparams.rgvarg[0].boolVal = VARIANT_TRUE;
    dispparams.rgvarg[1].vt = VT_BSTR;
    dispparams.rgvarg[1].bstrVal = bstrType;
    dispparams.rgvarg[2].vt = VT_BSTR;
    dispparams.rgvarg[2].bstrVal = bstrPath;
    dispparams.rgvarg[3].vt = VT_BSTR;
    dispparams.rgvarg[3].bstrVal = bstrProjectName;
    dispparams.cArgs = 4;
    dispparams.cNamedArgs = 0;
 
    // create project...
    hresult = pdisp->Invoke(
            dispid,
            IID_NULL,
            LOCALE_USER_DEFAULT,
            DISPATCH_METHOD,
            &dispparams, &varResult, &excepInfo, &uArgErr);
    
    // not full err handling here. Declare string rgvargs as VT_BSTRT to see this fire...
    if(DISP_E_EXCEPTION == hresult) {
        _bstr_t bt(excepInfo.bstrDescription);
        printf("VC Exception: %s\n", (char*)bt);
    }
 
    // get dispatch ID for Quit
    szMember = L"Quit";
    hresult = pdisp->GetIDsOfNames(IID_NULL, &szMember, 1,
                    LOCALE_USER_DEFAULT, &dispid);
 
    // Quit from Visual C++  
    dispparams.cArgs = 0;
    hresult = pdisp->Invoke(
            dispid,
            IID_NULL,
            LOCALE_USER_DEFAULT,
            DISPATCH_METHOD,
            &dispparams, &varResult, &excepInfo, &uArgErr);
    
 
    pdisp->Release();
    punk->Release();
 
    delete [] dispparams.rgvarg;
 
    // Uninitialize COM libraries  
    CoUninitialize();  
 
    return 0;
}


Pretty ugly, but seems to work. The docs and samples for this are a bit lacking, and somebody stole borrowed my copy of Essential Com, so I don't know if this is the only way this can be done.

I guess the theory here is that the interface is intended to be invoked through IDispatch, and problems show up if parameters need to be passed when using AddProject directly.

Would appreciate if someone could verify or debunk said theory.

-----
"They put the COM in COMplicated"
GeneralRe: Automating Visual C++ Project Creation Pin
Steve Thresher6-Aug-01 23:06
Steve Thresher6-Aug-01 23:06 
GeneralRe: Automating Visual C++ Project Creation Pin
Nick Van den Abbeele7-Aug-01 0:51
Nick Van den Abbeele7-Aug-01 0:51 
GeneralRe: Automating Visual C++ Project Creation Pin
Tim Deveaux7-Aug-01 9:34
Tim Deveaux7-Aug-01 9:34 
GeneralRe: Automating Visual C++ Project Creation Pin
Steve Thresher7-Aug-01 22:31
Steve Thresher7-Aug-01 22:31 
GeneralLine style and color Pin
6-Aug-01 1:26
suss6-Aug-01 1:26 
GeneralRe: Line style and color Pin
Not Active6-Aug-01 3:11
mentorNot Active6-Aug-01 3:11 
GeneralCAsyncSocket Pin
6-Aug-01 0:38
suss6-Aug-01 0:38 
GeneralMulti Document Interface question Pin
Ashman6-Aug-01 0:22
Ashman6-Aug-01 0:22 
GeneralRe: Multi Document Interface question Pin
Not Active6-Aug-01 3:13
mentorNot Active6-Aug-01 3:13 
GeneralVirus Scanners Pin
Steve Thresher6-Aug-01 0:00
Steve Thresher6-Aug-01 0:00 
Generalmfc windows explorer insantiy Pin
karmatosed5-Aug-01 23:56
karmatosed5-Aug-01 23:56 
QuestionHow can i .. Pin
5-Aug-01 23:55
suss5-Aug-01 23:55 
Generaldebug error Pin
Gérald Mercet5-Aug-01 23:51
Gérald Mercet5-Aug-01 23:51 
GeneralRe: debug error Pin
6-Aug-01 6:32
suss6-Aug-01 6:32 
GeneralGetting from CDocument to frame Pin
Malcolm McMahon5-Aug-01 22:27
Malcolm McMahon5-Aug-01 22:27 
GeneralRe: Getting from CDocument to frame Pin
#realJSOP6-Aug-01 1:30
professional#realJSOP6-Aug-01 1:30 
GeneralRemoving ActiveX of resource !!! Pin
Hadi Rezaee5-Aug-01 20:16
Hadi Rezaee5-Aug-01 20:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.