Sign in to follow this  
littlekid

How to use dynamically linked DLL and _COM_SMARTPTR_TYPEDEF

Recommended Posts

I have been trying to get _COM_SMARTPTR_TYPEDEF to work with my DLL library which is link dynamically. However when the program runs _COM_SMARTPTR_TYPEDEF breaks saying that the interface internally is invalid something along the lines breaks at comip.h First-chance exception at 0x01142aea in Test.exe: 0xC0000005: Access violation reading location 0x655fdd70. Unhandled exception at 0x01142aea in Test.exe: 0xC0000005: Access violation reading location 0x655fdd70. Here is how my stripped down header file looks
#define INITGUID

#include <windows.h>
#include <comdef.h>

//GUID
DEFINE_GUID(IID_ITest, 0xf58c50af, 0x68cb, 0x4fbc, 0x86, 0xf6, 0xfd, 0x45,
                       0xca, 0x54, 0x15, 0x35);

interface __declspec(uuid("F58C50AF-68CB-4fbc-86F6-FD45CA541535")) ITest;

//sample interface
interface __declspec(novtable) ITest : public IUnknown
{
};

//com smart pointer declaration
_COM_SMARTPTR_TYPEDEF(ITest, __uuidof(ITest));

//function pointer to use in dynamically loading the DLL via LoadLibraryEx
//CreateTestInterface function is exported in another file with extern "C" and
//using a .def file to prevent name mangling.
typedef HRESULT (__stdcall* CreateTestInterfaceFnPtr)(ITest** testInterface);

in the cpp file if i do something like it breaks at comip.h with the error:
HMODULE testDLL= LoadLibraryEx(TEXT("test.dll"), NULL, NULL);

if (testDLL == NULL) return;

CreateTestInterfaceFnPtr createTestFnPtr =
       reinterpret_cast< CreateTestInterfaceFnPtr>(GetProcAddress(coreDll, "CreateTestInterface"));

if (createTestFnPtr == NULL) return;

ITestPtr testPtr;
createTestFnPtr(&testPtr);

Is there something I did wrongly which cause _COM_SMARTPTR_TYPEDEF to not work? If I do static linking everything works fine. regards

Share this post


Link to post
Share on other sites
Just to add even if i dynamically load the DXGI.dll I can't seems to use _COM_SMARTPTR_TYPEDEF with it. my smart com pointer is defined as

_COM_SMARTPTR_TYPEDEF(IDXGIFactory1, __uuidof(IDXGIFactory1));

and similarly when the program runs it breaks at comip.h


comip.h:
// Releases only if the interface is not null.
// The interface is not set to NULL.
//
void _Release() throw()
{
if (m_pInterface != NULL) {
m_pInterface->Release(); //breaks at this line
}
}


First-chance exception at 0x01142aea in Test.exe: 0xC0000005: Access violation reading location 0x655fdd70.
Unhandled exception at 0x01142aea in Test.exe: 0xC0000005: Access violation reading location 0x655fdd70.

Is there anything i am missing out??

Share this post


Link to post
Share on other sites
When does testPtr go out of scope? Is it mistakenly after you call CoUninitialize?

What happens if you explicitly release it (by calling Release, or assigning NULL to the pointer) right after the call to createTestFnPtr?

Share this post


Link to post
Share on other sites
thanks for the replies. testPtr goes out of scope only after main() terminates.

here is how the main function look like:


void main()
{
HMODULE testDLL= LoadLibraryEx(TEXT("test.dll"), NULL, NULL);

if (testDLL == NULL) return;

CreateTestInterfaceFnPtr createTestFnPtr =
reinterpret_cast< CreateTestInterfaceFnPtr>(GetProcAddress(coreDll, "CreateTestInterface"));

if (createTestFnPtr == NULL) return;

ITestPtr testPtr;
createTestFnPtr(&testPtr);
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this