Ejemplo n.º 1
0
int main( int argc, char *argv[] ) 
  { 
  printf( "Initializing COM \n"); 
  if ( FAILED( CoInitialize( NULL ))) 
  { 
	  printf( "Unable to initialize COM \n"); return -1; 
  } 
  char* szProgID = "Math.Component.1"; 
  WCHAR szWideProgID[128]; 
  CLSID clsid; 
  long lLen = MultiByteToWideChar( CP_ACP, 0, szProgID,strlen( szProgID ), szWideProgID, sizeof( szWideProgID ) ); 
  szWideProgID[ lLen ] = '\0'; 
  HRESULT hr = ::CLSIDFromProgID( szWideProgID, &clsid ); 
  if ( FAILED( hr )) 
  { 
	  printf("Unable to get CLSID from ProgID. HR = %X \n",hr); return -1;
  } 
  IClassFactory* pCF; 
  // Получить фабрику класса для CMath 
  hr = CoGetClassObject( clsid, CLSCTX_INPROC, NULL,IID_IClassFactory, (void**) &pCF ); 
  if ( FAILED( hr )) 
  { 
	  printf("Failed to GetClassObject server instance. HR = %X \n",hr); return -1; 
  } 
  
  // с помощью фабрики класса создать экземпляр 
  // компонента и получить интерфейс IUnknown. 
  
  IUnknown* pUnk; 
  hr = pCF->CreateInstance( NULL, IID_IUnknown, (void**) &pUnk ); 
  
  // Release the class factory pCF->Release(); 
  if ( FAILED( hr )) 
  { 
	  printf("Failed to create server instance. HR =%X \n",hr); return -1; 
  } 
  printf("Instance created \n"); 
  IMath* pMath = NULL; 
  hr = pUnk->QueryInterface( IID_IMath, (LPVOID*)&pMath ); 
  pUnk->Release(); 
  if ( FAILED( hr )) 
  { 
	  printf("QueryInterface() for IMath failed \n"); return -1; 
  } 
  
  long result; 

  pMath->Add(255,125,&result);
  printf("Func - Add: 255 + 125 is %d \n",result);

  pMath->Multiply( 30, 8, &result ); 
  printf("Func - Mltiply: 30 * 8 is %d \n",result); 

  pMath->Subtract( 200, 123, &result ); 
  printf("Func - Substract: 200 - 123 is %d \n",result);

  pMath->Divide(21,3,&result);
  printf("Func - Divide: 21 / 3 is %d \n",result);
  printf("Releasing instance \n"); 

  pMath->Release(); 
  printf("Shuting down COM\n"); 
  CoUninitialize();
  _getch();
  return 0; 
} 
Ejemplo n.º 2
0
int main()
{
	cout << "init com \n";

	if(FAILED(CoInitialize(NULL)))
	{
		cout << "init failed\n";
	}

	CLSID clsid;
	HRESULT hr = ::CLSIDFromProgID(L"Chapter2.Math.1", &clsid);
	if(FAILED(hr))
	{
		cout << "get CLISI from ProgId failed \n";
	}

	IClassFactory* pcf = NULL;

	hr = CoGetClassObject(
		clsid, 
		CLSCTX_INPROC,
		NULL,
		IID_IClassFactory,
		(void**)&pcf);

	if(FAILED(hr))
	{
		cout << "get IClass Factory failed \n";
	}

	IUnknown* pUnk = NULL;
	hr = pcf->CreateInstance(NULL, IID_IUnknown, (void**)&pUnk);
	
	pcf->Release();

	if(FAILED(hr))
	{
		cout << "get Iunkdown failed \n";
	}


	IMath* pMath = NULL;
	hr = pUnk->QueryInterface(IID_IMath, (void**)&pMath);
	pUnk->Release();
	
	if(FAILED(hr))
	{
		cout <<"get Imath failed \n";
	}

	long op = 1;
	long op2 = 3;
	long res = 0;
	pMath->Add(op, op2, res);

	cout << "Imath add : res = " << res << " \n";

	pMath->Subtract(op, op2, res);

	cout << "Imath subtract : res = " << res << " \n";


	system("pause");
}
Ejemplo n.º 3
0
int main( int argc, char *argv[] )
{
   cout << "Initializing COM" << endl;

   if ( FAILED( CoInitialize( NULL )))
   {
      cout << "Unable to initialize COM" << endl;
      return -1;
   }

   char* szProgID = "Math.Component.1";
   WCHAR  szWideProgID[128];
   CLSID  clsid;
   long lLen = MultiByteToWideChar( CP_ACP,
                        0,
                        szProgID,
                        strlen( szProgID ),
                        szWideProgID,
                        sizeof( szWideProgID ) );

   szWideProgID[ lLen ] = '\0';
   HRESULT hr = ::CLSIDFromProgID( szWideProgID, &clsid );
   if ( FAILED( hr ))
   {
      cout.setf( ios::hex, ios::basefield );
      cout << "Unable to get CLSID from ProgID. HR = " << hr << endl;
      return -1;
   }

   IClassFactory* pCF;
   // Get the class factory for the Math class
   hr = CoGetClassObject( clsid,
                          CLSCTX_INPROC,
                          NULL,
                          IID_IClassFactory,
                          (void**) &pCF );
   if ( FAILED( hr ))
   {
      cout.setf( ios::hex, ios::basefield );
      cout << "Failed to GetClassObject server instance. HR = " << hr << endl;
      return -1;
   }

   // using the class factory interface create an instance of the
   // component and return the IExpression interface.
   IUnknown* pUnk;
   hr = pCF->CreateInstance( NULL, IID_IUnknown, (void**) &pUnk );

   // Release the class factory
   pCF->Release();

   if ( FAILED( hr ))
   {
      cout.setf( ios::hex, ios::basefield );
      cout << "Failed to create server instance. HR = " << hr << endl;
      return -1;
   }

   cout << "Instance created" << endl;

   IMath* pMath = NULL;
   hr = pUnk->QueryInterface( IID_IMath, (LPVOID*)&pMath );
   pUnk->Release();
   if ( FAILED( hr ))
   {
      cout << "QueryInterface() for IMath failed" << endl;
      return -1;
   }

   long result;
   pMath->Multiply( 100, 8, &result );
   cout << "100 * 8 is " << result << endl;

   pMath->Subtract( 1000, 333, &result );
   cout << "1000 - 333 is " << result << endl;

   cout << "Releasing instance" << endl;
   pMath->Release();

   cout << "Shuting down COM" << endl;
   CoUninitialize();

   scanf("");
   return 0;
}