I have a DLL in unmanaged C++ :
EditArticleManagerFactory.h:
class __declspec(dllexport) EditArticleManagerFactory : public NamedClassFactory<SCEditArticleManager>,
public SCBLEditArticle:ICOMEditArticleManagerFactory
{
public:
STDMETHODIMP CreateManager(BSTR bstrName, SCBLEditArticle::ICOMEditArticleManager** pEditArticleManager);
}
interface ICOMEditArticleManagerFactory : IUnknown
{
HRESULT CreateManager([in]BSTR bstrName, [out]ICOMEditArticleManager** pEditArticleManager);
}
EditArticleManagerFactory.cpp:
STDMETHODIMP EditArticleManagerFactory::CreateManager(BSTR bstrName, SCBLEditArticle::ICOMEditArticleManager** pEditArticleManager)
{
manager = factory->createManager(bstrName);
return manager->QueryInterface(__uuidof(SCBLEditArticle::ICOMEditArticleManager), (void**)&pEditArticleManager);
}
I'd like to call this method from Delphi and it should return an interface to a created manager.
Delphi:
function CreateManager(bstrName: wideString; pEditArticleManager: ICOMEditArticleManager): HResult; stdcall; external 'SCBLEditArticle.dll';
procedure CreateManager;
var
hr:HResult;
mCOMEditArticleManager: ICOMEditArticleManager;
begin
hr := CreateManager('MANAGER1', mCOMEditArticleManager);
end;
The problem is I get an access violation when it reaches the end; in this delphi method.
Do you have any ideea what could be wrong?
Thx, rufusz
Edit:
But I'm using a macro for implementing Release and
EditArticleManagerFactory.h :
IMPLEMENT_UNKNOWN_NODELETE(EditArticleManagerFactory) BEGIN_INTERFACE_TABLE(EditArticleManagerFactory) IMPLEMENTS_INTERFACE(SCBLEditArticle::ICOMEditArticleManagerFactory) END_INTERFACE_TABLE()
Inttable.cpp:
define IMPLEMENT_UNKNOWN_NODELETE(ClassName) \
STDMETHODIMP QueryInterface(REFIID riid, void **ppv) \ { \ HRESULT hr = InterfaceTableQueryInterface(this, GetInterfaceTable##ClassName(), riid, ppv);\ __if_exists(InheritedQueryInterface##ClassName) { if ( FAILED(hr) ) hr = InheritedQueryInterface##ClassName(riid, ppv); } \ return hr; \ }\ STDMETHODIMP_(ULONG) AddRef(void) { return 2; } \ STDMETHODIMP_(ULONG) Release(void) { return 1; }
Furthermore :
When I debug from Delphi, I get the access violation in the UnsetExceptionHandler
004046F7 3901 cmp [ecx],eax.
Maybe this can help to diagnose the problem.
Also if I declared an external function outside my C++ class, and called that from Delphi, I didn't get the access violation, but didn't get the interface pointer neither.
Also:
If I do nothing in the C++ method, I still get the AccessViolation.
Best Answer
Should be
Notice the "OUT", you somehow dropped a indirection. (an interface is only one *, not two)