mfc >> Loading DLLs having classes

by gopal » Mon, 27 Mar 2006 18:43:45 GMT

I need to explicitly load MFC entension DLL - DDAO35 & DDAO36
dynamically using AFXLoadlibrary or LoadLibrary. These DLLs are written
in C++, my question is how to
use them with all those decorated name?

Can any one please help me on this.Is it possible to load these DLLs in
first place?

I tried using DDAO350.DLL & DDAO360.DLL and i was sucessfull using COM

Any help

Regards
Gopal



mfc >> Loading DLLs having classes

by Ajay Kalra » Mon, 27 Mar 2006 21:45:41 GMT


Since these are MFC Extension DLLs, you should have MFC clients for
these modules. What problems do you have when you call AfxLoadLibrary?
Names of exported methods are mangled but that is expected. It should
work fine once it is loaded.

----
Ajay Kalra
XXXX@XXXXX.COM




mfc >> Loading DLLs having classes

by gopal » Mon, 27 Mar 2006 22:21:38 GMT

i have the following code

#include <AFX.H>

void main()
{
typedef UINT (*LPDLLFUNC)(char const*,int,int, char const*);
LPDLLFUNC lpfnDllFunc = NULL;
HINSTANCE hDLL = NULL;
hDLL = LoadLibrary("d:\\daosdk\\dll\\ddao35d.dll");
char const* char1 = "d:\\testdb.mdb";


if(hDLL)
{
lpfnDllFunc = (LPDLLFUNC)GetProcAddress(hDLL,"?
OpenDatabase@CdbDBEngine@@QAE?AVCdbDatabase@@PBDJH0@Z");

if (lpfnDllFunc)
{
lpfnDllFunc(char1,1,1,""); //Crash, Access violation
printf("Function found in DLL");

}
FreeLibrary(hDLL);
}
else
{
printf("Dll not found!");
}

Access violation occurs when the function is called and the control
goes to method Open Database in dbdao.cpp

CdbDatabase CdbDBEngine::OpenDatabase(
DAOMFC_CALL(peng->OpenDatabase(STB(pstrName), OLTV(lOptions),
BTV(bReadOnly), STV(pstrConnect), &pdb));
}



Loading DLLs having classes

by Ajay Kalra » Mon, 27 Mar 2006 22:41:54 GMT

Why use GetProcAddress? Why not call the function directly? You will
need the header file to see what all is exported.

---
Ajay Kalra
XXXX@XXXXX.COM



Loading DLLs having classes

by gopal » Mon, 27 Mar 2006 22:45:45 GMT

I need to load the DLL dynamically either dao35 or dao36 depending on
my application version



Loading DLLs having classes

by Ajay Kalra » Mon, 27 Mar 2006 22:50:06 GMT

I dont know much about dao, but can you not use it thru COM instead. I
would not touch GetProcAddress, especially with a MFC extension DLL.

---
Ajay Kalra
XXXX@XXXXX.COM



Loading DLLs having classes

by gopal » Mon, 27 Mar 2006 22:56:22 GMT

i have successfully done it using COM..but the requiremnets have
changed suppose to check with MFC Extension DLLs

any examples?



Loading DLLs having classes

by Ajay Kalra » Mon, 27 Mar 2006 23:24:38 GMT

Sorry, I cannot be of further help. Perhaps a database related
newsgroup would be of more help.

----
Ajay Kalra
XXXX@XXXXX.COM



Loading DLLs having classes

by gopal » Mon, 27 Mar 2006 23:31:12 GMT

ok.




Loading DLLs having classes

by gopal » Tue, 28 Mar 2006 01:05:22 GMT

any idea whether finally we can use the AfxLoadLibrary and load the
DLLs dynamically

Is it too diffcult, please can some one tell me out regarding this MFC
DAO DLLs loading dynamically

Regards
JK



Loading DLLs having classes

by Ajay Kalra » Tue, 28 Mar 2006 01:11:57 GMT

AfxLoadLibrary works fine in loading libraries. I dont know whether you
should load DAO libraries the way you are proposing it.

---
Ajay Kalra
XXXX@XXXXX.COM



Loading DLLs having classes

by gopal » Tue, 28 Mar 2006 01:23:44 GMT

But the requirements are such that i have to use this. i have tried but
somehow i am taking some wrong steps at some point in the code.

Really i have no choice i should say..

Regards
JK



Loading DLLs having classes

by Ajay Kalra » Tue, 28 Mar 2006 01:29:20 GMT

> Really i have no choice i should say..

Why not have two versions of your app, one for each version of this
DLL. If you can use this thru COM, you can then do it at run time by
instatiating the correct Dao DLL based on whatever version you want.

----
Ajay Kalra
XXXX@XXXXX.COM



Loading DLLs having classes

by gopal » Tue, 28 Mar 2006 01:32:42 GMT

My code is working fine when i am using DAO350 or DAO360, its just 10
lines of code and working very nicely,

But now i am facing this problem, since i am expected to use this way.

No we will have one version of the app in system and based on that we
will load the DLL dao35 or dao36 and connection of access database is
established



Loading DLLs having classes

by Ajay Kalra » Tue, 28 Mar 2006 01:44:36 GMT

>No we will have one version of the app in system and based on that we

Using GetProcAddress to call evey function still does not make sense to
me. Anyway, Good luck.

---
Ajay Kalra
XXXX@XXXXX.COM



Similar Threads

1. Dynamically load a class out of a DLL and create an instance of it - CSharp/C#

2. how to load classes from DLL

I used an utility called Excelsior JET to convert Java class files into a dll

My question is, how do i use a class in the DLL in vc++?

I am used to calling functions from DLLs using loadLibrary() but 
class i have never used.

Help me.
Harkeman

3. singleton in a DLL loaded class

4. Dynamically Loading/Unloading extension DLLs (MFC) - VC 2003 causes application loss of CRuntime classes

I seem to have a problem unloading MFC extension DLL's with VC2002 (it may
also exist on VC2002).
The following code is the DLLMain for a DLL which is loaded/unloaded
dynamically from a VC7 MFC application.
===================================
#include "stdafx.h"
#include <afxdllx.h>
...
static AFX_EXTENSION_MODULE ETVFDDLL = { NULL, NULL };
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        AfxInitExtensionModule(ETVFDDLL, hInstance);
        new CDynLinkLibrary(ETVFDDLL);
    }
else if (dwReason==DLL_PROCESS_DETACH)
    {
   AfxTermExtensionModule(ETVFDDLL, hInstance);
    }
}

When the termination is called, the application loses the classlist causing
subsequent failures in locating the CRuntimeClasses of various objects.
I believe this problem is caused by failing to set the AFX_MODULE_STATE
m_pClassInit to its m_classList head before AfxInitExtensionModule is run.

The problem seems corrected by inserting:
       ExtRawDllMain(hInstance, dwReason, lpReserved);
prior to the AfxInitExtensionModule call.

There seems to be a disconnect between the runtime library routine
_DllMainCRTStartup and the mfc afxdllx.h file.
ExtRawDllMain never gets called from _DllMainCRTStartup because the pointer
_pRawDllMain is NULL.

Please note that afxdllx.h (on VC7)  initializes _pAtlChainRawDllMain to
point to  ExtRawDllMain.
On VC6, the pointer initialization was to  _pRawDllMain.

Thanks,
Bill


5. Question: Dynamic DLL loading / Class export

6. Return Class instance from dynamically loaded DLL?

I use dynamically loaded dll's a lot in my programs.  The one thing i cannot
figure out how do is too to sucessfully pass the address of a class instance
created within the dynamically loaded dll.  I step thru the code and confirm
that the class is correctly created in the dll, but as soon as it steps out
of the dll i loose the address.  For example:

      hLib = LoadLibrary("RasterIO.dll");

      ImageIOClass *ImageIO = (ImageIOClass
*)GetProcAddress((HINSTANCE)hLib, "_GetImageIOClass");
      if (ImageIO != NULL)
      {
         TRasterIO *IO;
         ImageIO(IO, ...);   // instance created with new in dll.
         if (IO)
         {
             // do something with IO
             ....
         }
         else
         {
              return;
          }

         delete IO;
      }
      UnloadPackage(hLib);

That is the basic idea of what i am trying to do.  If i statically link the
dll i can get an instance of the class no problem.  I suppose this is all
something to do with the way dlls handle and share memory - something i
obviously do not understand!

Peter


7. Loading classes from DLL

8. Having problem with class decaration when Building a class librara

Have you referrenced the other library?   If so, have you added a using 
statement to import the appropriate namespace from the other library?

What does the error message say when you build?

"tony" wrote:

> Hello!
> 
> I try to build a class library but I run into some problems.
> 
> In the code for this class library there are some rows that have this kind
> of look
> public bool IsBottomMixValid(MeltPracDataGmix.BottomBlowing mix, int
> maxBottomFlow)
> 
> As you can see here we have a class named MeltPracDataGmix that exist in the
> code for the project
> that build the actual exe file. So the class definition does not exist in
> this project for this class library.
> 
> My qustion is what is the solution to this kind of problem.
> 
> I assume if this was C++ it would be enough to include the class declaration
> which is the header file.
> 
> But in C# we don't have any header files. So how do I do here?
> 
> //Tony
> 
> 
> 
>