Tuesday, August 23, 2011

C++ program that uses the Quality Center API

How to create a C++ program that uses the Quality Center API?

Description:
This example adds a new defect to a project using Visual C++.
Usage:
1. Open Microsoft Visual C++ 6.0.
2. From the menu, select File -> New.
3. Navigate to the Projects tab.
4. Select Win32 Application.
5. Enter a name for your project. Example:
AddDefect
6. Select a location for your project.
7. Click <OK>.
8. Select the "A Simple Win32 application" radio button.
9. Click <Finish>.
10. Click <OK>.
11. Select the File View tab in the Workspace window.
12. Under the Source Files folder, open AddDefect.cpp.
13. Add the code below.
Code:
#import "C:\Program Files\Common Files\Mercury Interactive\TD2000_80\OTAClient80.dll" no_namespace named_guids
void AddDefect();
int APIENTRY WinMain(HINSTANCE hInstance,
   HINSTANCE hPrevInstance,
   LPSTR lpCmdLine,
   int nCmdShow)
{
   // !!! You are working with COM -every thread (main thread also) must initialize COM run-time library
   CoInitialize(NULL);
   // Call the AddDefect function here
   AddDefect();
   // Release COM run-time library
   CoUninitialize();
   return 0;
}
void AddDefect()
{
    try
    {
      _bstr_t mydata;
      // Creating a new TDConnection object and all the required objects for a TestDirector connection.

      ITDConnectionPtr sp_td;
      IBugFactoryPtr bFact;
      IBugPtr mybug;
      variant_t myBugData(mydata);
      //Connect to the server and project
      sp_td.CreateInstance(CLSID_TDConnection);
      sp_td->InitConnectionEx("http://<servername>/tdbin");

      //Note: For Quality Center, connect by using the URL below:
      sp_td->InitConnectionEx("http://<servername>:port/qcbin");
      sp_td->ConnectProjectEx("DomainName", "ProjectName","UserName", "Password");
      MessageBox(NULL,"Connected to project !","",0);
      //TODO: Place code here to add defect
      //The following code will add a new defect.
      bFact = sp_td->GetBugFactory();
      mybug = bFact->AddItem(myBugData);
      //Adding value to the Assigned To and the Project field for this new defect
      //To add information to other fields of this defect, just enter the information here.
      mydata = "admin";
      mybug->PutAssignedTo(mydata);
      mydata ="Project";
      mybug->PutProject(mydata);
      mybug->Post();
      MessageBox(NULL,"A new defect added !","",0);
      //Disconnect from project and release connection from the server.
      sp_td->DisconnectProject();
      sp_td->ReleaseConnection();
      MessageBox(NULL,"Done !","",0);
   }
   //Catch exception by reference (the most optimal way to catch exceptions)
   catch(_com_error& err)
   {
      MessageBox(NULL,err.Description(),"",0);
   }

No comments:

Post a Comment