Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DispatchID Failed...

DispatchID Failed...

Scheduled Pinned Locked Moved C / C++ / MFC
databasetutorialwpfwcfcom
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yaxiya
    wrote on last edited by
    #1

    #include <windows.h>
    #include <stdio.h>

    void PlayWithAccess(IUnknown *p);
    void CallOpenDatabase(IDispatch *d);
    int main(int argc, char* argv[])
    {

    HRESULT hRet;
    CLSID clsid;
    IUnknown *p=NULL;
    hRet=CoInitialize(NULL);

    if(FAILED(hRet)) {
    printf("It Failed...\n");
    return 1;
    }
    hRet=CLSIDFromProgID(L"Access.Application",&clsid);

    if(FAILED(hRet)){
    printf("CLSID Illegal...\n");
    CoUninitialize();
    return 1;
    }

    hRet=CoCreateInstance(&clsid, NULL, CLSCTX_ALL, &IID_IUnknown, (IUnknown **)&p);
    if(FAILED(hRet)) {
    printf("Create Failed....");
    CoUninitialize();
    return 2;
    }
    PlayWithAccess(p); //////// Play with Access Application Com Object
    p->lpVtbl->Release(p);
    CoUninitialize();
    return 0;
    }
    void PlayWithAccess(IUnknown *p)
    {
    char buf[100];
    IDispatch *d=NULL;
    HRESULT hret;
    hret=p->lpVtbl->QueryInterface(p,&IID_IDispatch,(void **)&d);
    if(FAILED(hret)){
    printf("DispatchID Failed...\n");
    sprintf(buf,"DispatchID failed w/err 0x%08lx", hret);
    MessageBox(NULL, buf, "DispatchID", 0x10010);
    return;
    }

    printf("Got the IDispatch Interface from Object...\\n");
    CallOpenDatabase(d);  
    printf("Enter Return:"); 
    gets(buf);
    d->lpVtbl->Release(d);// Release Reference
    
    }
    

    void CallOpenDatabase(IDispatch *d) // Call Open database through IDispatch Interface
    { // Late IDispatch Binding....
    VARIANTARG args[2];

    BSTR DBLocation=SysAllocString(L"D:/project/C/c\_ado/connDB02/Example.mdb"); 
    OLECHAR \*MethodName=L"OpenCurrentDatabase";   // Got The Method Name from Type Library
    DISPID dispid=0;   
    HRESULT hret;
    DISPPARAMS params; 
    DISPID ids\[2\]={1,0};
    hret=d->lpVtbl->GetIDsOfNames(d,&IID\_NULL,&MethodName,1,LOCALE\_USER\_DEFAULT,&dispid);
    
    printf("Got a Dispatch Identifier....%x\\n",dispid);
    memset(args,0,sizeof(args));   // Build Up arguments
    
    args\[0\].vt=VT\_BOOL;   // Last Argument is Optional
    args\[0\].boolVal=FALSE;
    args\[1\].vt=VT\_BSTR;		// First argument is DB Location
    args\[1\].bstrVal=DBLocation;
    memset(&params,0,sizeof(params));
    params.cArgs=2;  
    params.rgvarg=args; 	
    
    hret=d->lpVtbl->Invoke(d,dispid,&IID\_NULL,LOCALE\_USER\_DEFAULT,DISPATCH\_METHOD,&params,NULL,NULL,NULL);
    if(FAILED(hret)) {
    	printf("Invoke Failed....\\n"); 
    }
    SysFreeString(DBLocation);
    }
    

    How to do?thanks!

    B 1 Reply Last reply
    0
    • Y yaxiya

      #include <windows.h>
      #include <stdio.h>

      void PlayWithAccess(IUnknown *p);
      void CallOpenDatabase(IDispatch *d);
      int main(int argc, char* argv[])
      {

      HRESULT hRet;
      CLSID clsid;
      IUnknown *p=NULL;
      hRet=CoInitialize(NULL);

      if(FAILED(hRet)) {
      printf("It Failed...\n");
      return 1;
      }
      hRet=CLSIDFromProgID(L"Access.Application",&clsid);

      if(FAILED(hRet)){
      printf("CLSID Illegal...\n");
      CoUninitialize();
      return 1;
      }

      hRet=CoCreateInstance(&clsid, NULL, CLSCTX_ALL, &IID_IUnknown, (IUnknown **)&p);
      if(FAILED(hRet)) {
      printf("Create Failed....");
      CoUninitialize();
      return 2;
      }
      PlayWithAccess(p); //////// Play with Access Application Com Object
      p->lpVtbl->Release(p);
      CoUninitialize();
      return 0;
      }
      void PlayWithAccess(IUnknown *p)
      {
      char buf[100];
      IDispatch *d=NULL;
      HRESULT hret;
      hret=p->lpVtbl->QueryInterface(p,&IID_IDispatch,(void **)&d);
      if(FAILED(hret)){
      printf("DispatchID Failed...\n");
      sprintf(buf,"DispatchID failed w/err 0x%08lx", hret);
      MessageBox(NULL, buf, "DispatchID", 0x10010);
      return;
      }

      printf("Got the IDispatch Interface from Object...\\n");
      CallOpenDatabase(d);  
      printf("Enter Return:"); 
      gets(buf);
      d->lpVtbl->Release(d);// Release Reference
      
      }
      

      void CallOpenDatabase(IDispatch *d) // Call Open database through IDispatch Interface
      { // Late IDispatch Binding....
      VARIANTARG args[2];

      BSTR DBLocation=SysAllocString(L"D:/project/C/c\_ado/connDB02/Example.mdb"); 
      OLECHAR \*MethodName=L"OpenCurrentDatabase";   // Got The Method Name from Type Library
      DISPID dispid=0;   
      HRESULT hret;
      DISPPARAMS params; 
      DISPID ids\[2\]={1,0};
      hret=d->lpVtbl->GetIDsOfNames(d,&IID\_NULL,&MethodName,1,LOCALE\_USER\_DEFAULT,&dispid);
      
      printf("Got a Dispatch Identifier....%x\\n",dispid);
      memset(args,0,sizeof(args));   // Build Up arguments
      
      args\[0\].vt=VT\_BOOL;   // Last Argument is Optional
      args\[0\].boolVal=FALSE;
      args\[1\].vt=VT\_BSTR;		// First argument is DB Location
      args\[1\].bstrVal=DBLocation;
      memset(&params,0,sizeof(params));
      params.cArgs=2;  
      params.rgvarg=args; 	
      
      hret=d->lpVtbl->Invoke(d,dispid,&IID\_NULL,LOCALE\_USER\_DEFAULT,DISPATCH\_METHOD,&params,NULL,NULL,NULL);
      if(FAILED(hret)) {
      	printf("Invoke Failed....\\n"); 
      }
      SysFreeString(DBLocation);
      }
      

      How to do?thanks!

      B Offline
      B Offline
      barneyman
      wrote on last edited by
      #2

      Couple of observations here what's the problem? what error code are you getting? what sort of help do you require? It's extremely unlikely anyone's going to copy your code, compile it and come back with the answer - sorry A general suggestion, don't use Dispatch interfaces, they are the most unwieldy method of calling com objects, use the 'real' interface Another one, if you have access to ATL, use CComPtr's to help wrap the pointers, and use OLE DB Client Templates to talk with Access

      Y 1 Reply Last reply
      0
      • B barneyman

        Couple of observations here what's the problem? what error code are you getting? what sort of help do you require? It's extremely unlikely anyone's going to copy your code, compile it and come back with the answer - sorry A general suggestion, don't use Dispatch interfaces, they are the most unwieldy method of calling com objects, use the 'real' interface Another one, if you have access to ATL, use CComPtr's to help wrap the pointers, and use OLE DB Client Templates to talk with Access

        Y Offline
        Y Offline
        yaxiya
        wrote on last edited by
        #3

        thank you answer my question I think use C to Implement the Dispatch interface.But I find it's not.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups