Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages
Started by Chris Buys
Mon, 31 Mar 2003 06:39
Dynamically loaded DLL problem
Author: Chris Buys
Date: Mon, 31 Mar 2003 06:39
Date: Mon, 31 Mar 2003 06:39
102 lines
3769 bytes
3769 bytes
Hi everyone.
I'm have problems unloading dynamically loaded DLLs. I get memory read
errors in Win2000.
I'm using BCB5 Enterprise Edition.
My application reads an INI file and builds a menu system from the contents
of the INI file. The INI
file holds references to DLLs. Each menu-item is thus "linked" to a DLL.
When the user clicks on a
menu-item, that DLL is loaded and the user uses it. After the user exits
the form in the DLL, I
try to unload it. It seems that the unloading process succeeds, but when
the Menu OnClick-event
exits, I receive 4 to 5 read errors, all memory related.
This is the OnClick Event of the Menu-Item:
typedef void *MYPROC(TADOConnection *, String);
// MYPROC is the typedef name, pointer
HINSTANCE Library_Handle;
MYPROC *Procedure;
BOOL fFreeResult, fRunTimeLinkSuccess, result;
Procedure = NULL;
String file =
GetCurrentDir()+"\\"+MenuData(Data_Array[ClickedItem->Tag])->DLL;
try
{ Library_Handle = LoadLibrary(file.c_str()); }
catch (Exception& e)
{ ShowMessage(e.Message+" frmMain::ActivateReport()"); }
if (Library_Handle == NULL)
{
try
{ Library_Handle = LoadLibrary(file.c_str()); }
catch (Exception& e)
{
ShowMessage(e.Message+" frmMain::ActivateReport() - Loading of
DLL");
return;
}
}
if (Library_Handle != NULL)
{
try
{
Procedure = (MYPROC *) GetProcAddress(Library_Handle,
MenuData(Data_Array[ClickedItem->Tag])->ExportedCallingFunction.c_str()); //
create pointer to that procedure
if (Procedure != NULL)
{
Procedure(frmConnection->ADOConnection,"");
}
else
{ ShowMessage("Couldn't hook procedure:
"+MenuData(Data_Array[ClickedItem->Tag])->ExportedCallingFunction); }
}
catch (Exception& e)
{ ShowMessage(e.Message+" frmMain::ActivateReport() - Activating of
DLL"); }
}
// close the Library
try
{
if (Library_Handle != NULL)
{
Procedure = NULL;
FreeLibrary(Library_Handle);
Library_Handle = NULL;
}
}
catch (Exception& e)
{
ShowMessage(e.Message+" frmMain::ActivateReport() freeing of DLL");
}
Library_Handle = NULL;
}
The function code called in the DLL is this:
{
frmStockPriceList = new TfrmStockPriceList(NULL,Conn);
frmStockPriceList->ShowModal();
delete frmStockPriceList;
}
This is just a VCL form with ADOQueries and a QuickReport-componet.
I am sure that the closing code of the form is called correctly.
Any ideas?
Thanks in advance
Chris
--------------------------------------
Chris Buys
Development
HAS Software (Pty) Ltd
Web: www.h-a-s.co.za
Re: Dynamically loaded DLL problem
Author: Greg Brewer
Date: Wed, 02 Apr 2003 03:23
Date: Wed, 02 Apr 2003 03:23
121 lines
4586 bytes
4586 bytes
1 suggestion -- try creating a dll that does nothing when called. This
will tell you if it is something you are doing in the dll. I don't see
anything that should cause errors. You might try simplifying the code. I
don't think the try/catch's are doing anything for you because the Windows
API doesn't throw exceptions. For right now, don't worry about all the
error cases. Assume all the parts will work. Your goal is to get it to
work; not to handle everything that could go wrong.
Greg Brewer
"Chris Buys" <chris@h-a-s.co.za> wrote in message
news:3e87e2cc@newsgroups.borland.com...
> Hi everyone.
>
> I'm have problems unloading dynamically loaded DLLs. I get memory read
> errors in Win2000.
> I'm using BCB5 Enterprise Edition.
>
> My application reads an INI file and builds a menu system from the
contents
> of the INI file. The INI
> file holds references to DLLs. Each menu-item is thus "linked" to a DLL.
> When the user clicks on a
> menu-item, that DLL is loaded and the user uses it. After the user exits
> the form in the DLL, I
> try to unload it. It seems that the unloading process succeeds, but when
> the Menu OnClick-event
> exits, I receive 4 to 5 read errors, all memory related.
>
> This is the OnClick Event of the Menu-Item:
>
> typedef void *MYPROC(TADOConnection *, String);
> // MYPROC is the typedef name, pointer
>
> HINSTANCE Library_Handle;
> MYPROC *Procedure;
> BOOL fFreeResult, fRunTimeLinkSuccess, result;
>
> Procedure = NULL;
> String file =
> GetCurrentDir()+"\\"+MenuData(Data_Array[ClickedItem->Tag])->DLL;
> try
> { Library_Handle = LoadLibrary(file.c_str()); }
> catch (Exception& e)
> { ShowMessage(e.Message+" frmMain::ActivateReport()"); }
>
> if (Library_Handle == NULL)
> {
> try
> { Library_Handle = LoadLibrary(file.c_str()); }
> catch (Exception& e)
> {
> ShowMessage(e.Message+" frmMain::ActivateReport() - Loading of
> DLL");
> return;
> }
> }
>
> if (Library_Handle != NULL)
> {
> try
> {
> Procedure = (MYPROC *) GetProcAddress(Library_Handle,
> MenuData(Data_Array[ClickedItem->Tag])->ExportedCallingFunction.c_str());
//
> create pointer to that procedure
> if (Procedure != NULL)
> {
> Procedure(frmConnection->ADOConnection,"");
> }
> else
> { ShowMessage("Couldn't hook procedure:
> "+MenuData(Data_Array[ClickedItem->Tag])->ExportedCallingFunction); }
> }
> catch (Exception& e)
> { ShowMessage(e.Message+" frmMain::ActivateReport() - Activating
of
> DLL"); }
> }
>
> // close the Library
> try
> {
> if (Library_Handle != NULL)
> {
> Procedure = NULL;
> FreeLibrary(Library_Handle);
> Library_Handle = NULL;
> }
> }
> catch (Exception& e)
> {
> ShowMessage(e.Message+" frmMain::ActivateReport() freeing of DLL");
> }
> Library_Handle = NULL;
> }
>
>
> The function code called in the DLL is this:
> {
> frmStockPriceList = new TfrmStockPriceList(NULL,Conn);
> frmStockPriceList->ShowModal();
> delete frmStockPriceList;
> }
> This is just a VCL form with ADOQueries and a QuickReport-componet.
> I am sure that the closing code of the form is called correctly.
>
> Any ideas?
> Thanks in advance
> Chris
>
> --------------------------------------
> Chris Buys
> Development
>
> HAS Software (Pty) Ltd
> Web: www.h-a-s.co.za
>
>
>
Re: Dynamically loaded DLL problem
Author: Remy Lebeau (TeamB)
Date: Wed, 02 Apr 2003 03:30
Date: Wed, 02 Apr 2003 03:30
12 lines
1380 bytes
1380 bytes
"Greg Brewer" <Greg@Brewer.net> wrote in message news:3e8a582a@newsgroups.borland.com... > I don't think the try/catch's are doing anything for you because > the Windows API doesn't throw exceptions. The lower-level ones may, especially those related to hardware. But they throw SEH exceptions, which try...catch can't handle, so you'd have to use __try...__except() instead. Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads