Click to See Complete Forum and Search --> : CCW and smart pointer - Help!


nfung
August 24th, 2003, 04:05 AM
I've created this simple managed c++ class library:

namespace pkFactorialLib
{

public __gc __interface IFactorial
{
int ComputeFactorial(int n);

};

public __gc class Factorial : public IFactorial
{

public:

Factorial()
{

}

public:
int ComputeFactorial(int n)
{
int intFactorial=n;

for(int i=1; i<n; i++)
{
intFactorial*=i;
}
return intFactorial;

}

private:
int doublenum(int n)
{
return n*2;
}

};
}

I wrap it up in a CCW using RegASM.exe so I can call it via COM. In my client code:

#include "stdafx.h"
#import "pkFactorialLib.tlb"

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

USES_CONVERSION;

//Crash!!!
pkFactorialLib::IFactorialPtr pobj(__uuidof(pkFactorialLib::IFactorialPtr));

return 0;
}

So, my question is:

QUESTION 1:
pkFactorialLib::IFactorialPtr pobj(__uuidof("What can I put here?"));

The hint I have from Intellisense is:
1. pkFactorialLib::_Factorial
2. pkFactorialLib::_FactorialPtr
3. pkFactorialLib::IFactorial
4. pkFactorialLib::IFactorialPtr

QUESTION 2:
Can I make do without having an interface? Can I just expose a regular class instead?

pkFactorialLib::IFactorialPtr interfaceobj( __uuid(..) ); //an interface pointer
pkFactorialLib::Factorial classobj( __uuid(..) ); //pointer to a class?

I understand with COM, you talk to COM library, pass a CLSID and COM library returns only the interface pointer - not the object itself. But is it possible that I retrieve the object instead...?

Thanks.

Andreas Masur
August 24th, 2003, 04:13 AM
[Moved thread]