Thread: GetProcAddress problem

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    Angry GetProcAddress problem

    HI all, this is my first post here.

    I have to call an ActiveX dll (made in VB), When I explore the dll this is what I get

    ----------------------------------------------------------------------
    // Generated .IDL file (by the OLE/COM Object Viewer)
    //
    // typelib filename: RETRIEVE.dll

    [
    uuid(CC3FB919-B773-11D7-9E25-0001021851E5),
    version(2.0)
    ]
    library RETRIEVE
    {
    // TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("Stdole2.tlb");

    // Forward declare all types defined in this typelib
    interface _clsRetrieve;

    [
    odl,
    uuid(CC3FB925-B773-11D7-9E25-0001021851E5),
    version(1.0),
    hidden,
    dual,
    nonextensible,
    oleautomation
    ]
    interface _clsRetrieve : IDispatch {
    [id(0x60030000)]
    HRESULT GetDocFromArchive(
    [in, out] BSTR* DA_DocGroupID,
    [in, out] BSTR* Ext_DocID,
    [in, out] BSTR* DADocArchivingDate,
    [in, out] BSTR* DADocArchivingTime,
    [in, out] BSTR* DAFileIDs,
    [in, out, optional] BSTR* DADocModifyDate,
    [in, out, optional] BSTR* DADocModifyTime,
    [out, retval] BSTR* );
    };

    [
    uuid(CC3FB926-B773-11D7-9E25-0001021851E5),
    version(1.0)
    ]
    coclass clsRetrieve {
    [default] interface _clsRetrieve;
    };
    };
    --------------------------------------------------------------------

    and I am trying to use that procedure from C++

    This is the part of the code. Please read the comments..

    // here might be the my problem, is this typedef right?
    // I know that the 2 fist vars are byval.
    typedef BSTR ( CALLBACK* myGetDocFromArchive) (
    BSTR DA_DocGroupID,
    BSTR Ext_DocID,
    BSTR* DADocArchivingDate,
    BSTR* DADocArchivingTime,
    BSTR* DAFileIDs,
    BSTR* DADocModifyDate,
    BSTR* DADocModifyTime );

    myGetDocFromArchive GetDocFromArchive;

    // Load the DLL into memory.
    HINSTANCE hLib = LoadLibrary("RETRIEVE.dll");

    // Make sure to check if it was loaded successfully.
    if(hLib == NULL)
    {
    cout << "ERROR: Unable to load library!" << endl;
    getch();
    return -1;
    }

    // Finds the path of the client application.
    char mod[100];
    GetModuleFileName((HMODULE)hLib,(LPTSTR)mod,100);
    cout << "Library Loaded: " << mod << endl;

    //UNTILL NOW THE LIBRARY LOADED WELL

    // Finds the addresses of all the DLL's functions.
    // HERE IS THE PROBLEM GetDocFromArchive is NULL WHY?
    GetDocFromArchive = (myGetDocFromArchive)GetProcAddress((HMODULE)hLib, "clsRetrieve.GetDocFromArchive");

    .
    .
    .
    .


    // Free the DLL from memory.
    FreeLibrary((HMODULE)hLib);
    -----------------------------------------------------------------

    The problem is that it never finds the Procedure Address so GetDocFromArchive always is 0x0000000 (NULL).

    The question is What is wrong here, is it is my typedef?

    I have registered the dll.

    Thank you.

    froque

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You wont get that proc address easilly due to name mangling. GetProcAddress will naturally lookup "extern C" functions.

    If you are useing a com dll, then use com to call it. Even if you got the address, there may be something going on in the dll that wont be executed due to you using the dll this way

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    5
    I have been trying to use this dll. I will try to read it using com, and I report back, but any clues and hits are welcome.

    MSDN site is not working properly nowadays (they are updating the site) and it is hard to get info from there.

    BR

    Felipe Roque.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    As this is an activeX dll implementing IDispatch, and assuming you have a copy of VC++.......the easiest way to proceed is to use classwizard in vc++ and use the typelib from the dll (it maybe a seperate file, or even be part of the dll itself) to allow vc++ to create wrapper classes around the dll. Then you use the classes just like those created from any other C++ class

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The prototype is wrong, it doesn't take the this pointer into account. And you can't retrieve the address this way anyway.

    The problem with the wrapper class is that it (AFAIK) only works with MFC.

    So here's to you, the plain API version
    Code:
    // header file:
    struct _clsRetrieve extends IDispatch
    {
      STDMETHOD(GetDocFromArchive)(
        BSTR* DA_DocGroupID, 
        BSTR* Ext_DocID, 
        BSTR* DADocArchivingDate, 
        BSTR* DADocArchivingTime, 
        BSTR* DAFileIDs, 
        BSTR* DADocModifyDate, 
        BSTR* DADocModifyTime, 
        BSTR* ret) = 0;
    };
    
    DEFINE_GUID(IID__clsRetrieve, 0xCC3FB925, 0xB773, 0x11D7,
      0x9E, 0x25, 0x00, 0x01, 0x02, 0x18, 0x51, 0xE5);
    DEFINE_GUID(CLSID_clsRetrieve, 0xCC3FB926, 0xB773, 0x11D7,
      0x9E, 0x25, 0x00, 0x01, 0x02, 0x18, 0x51, 0xE5);
    
    // how to use:
    _clsRetrieve *createRetrieve()
    {
      // Assumes that CoInitialize(Ex) was called.
      _clsRetrieve *pRet;
      CoCreateInstance(CLSID_clsRetrieve, NULL,
        CLSCTX_INPROC_SERVER, IID__clsRetrieve, (void**)&pRet);
      return pRet;
    }
    For calling, simply do
    HRESULT hr = pRet->GetDocFromArchive(blablabla);
    The last parameter is the "return value".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    5
    Hi

    I have say THANK YOU to all of you specially to CornedBee.

    After a few changes I could make the code work, used what gently CornedBee wrote and I could solve the problem.

    I had to change the extends, used :
    also call the fuction to create the pointer:
    _clsRetrieve * pRet = createRetrieve();

    and that whas it.

    Thank you

    froque

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM