Phoenix_s
asked on
DLL not allowing calling program to exit
Ladies & Gentlemen -
Here is my problem. I have just ventured into DLL creation and I am having issues with the stinking things.
I created a DLL to send email to one or more recipients. After a bunch of bashing around, I got it to actually send the email.... but after it does that and I exit the calling program, the calling program sits im memory wasting space.
I am including the source code for the dll and the calling program.
here's the DLL
-------------------------- ---------- ---------- ---------- ---
library emailit;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
Sharemem,
SysUtils,
Classes,
windows,
dialogs,
comobj, activex;
const
// for email capability
olFolderDeletedItems = $00000003;
olFolderOutbox = $00000004;
olFolderSentMail = $00000005;
olFolderInbox = $00000006;
olFolderCalendar = $00000009;
olFolderContacts = $0000000A;
olFolderJournal = $0000000B;
olFolderNotes = $0000000C;
olFolderTasks = $0000000D;
olMailItem = 0;
var
outlook, nmspace, mailitem: olevariant;
{$R *.RES}
procedure email(sendto, subject: string; body: tstrings);
var str1, str2, str3 : string;
pos1 : integer;
begin
if coinitialize(nil) >= 0 then
try
outlook := getactiveoleobject('outloo k.applicat ion');
except
outlook := createoleobject('outlook.a pplication ');
end
else
exit;
nmspace := outlook.getnamespace('MAPI ');
nmspace.logon(emptyparam, emptyparam, false, true);
mailitem := outlook.createitem(olmaili tem);
{more than one recipient in this field will be parsed into individual recipients and added seperately through this routine}
pos1 := pos(';',sendto);
if pos1 = 0 then
mailitem.recipients.add(se ndto)
else
begin
str3 := sendto;
repeat
str1 := copy(str3,1,pos1-1);
str2 := copy(str3,pos1+1,(length(s tr3)-lengt h(str1)));
mailitem.recipients.add(st r1);
str3 := str2;
pos1 := pos(';',str3);
until pos1 = 0;
mailitem.recipients.add(st r3);
end;
mailitem.subject := subject;
mailitem.body := body.text;
mailitem.send;
nmspace.logoff;
outlook := unassigned;
end;
exports
email;
begin
end.
-------------------------- ---------- ---------- ---------- ----------
The calling program is just a blank form with a button on it which sends an email
calling app's project file :
-------------------------- ---------- ---------- ---------- ----------
program emaildlltest;
uses
ShareMem,
Forms,
dlltest in 'dlltest.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TFo rm1, Form1);
Application.Run;
end.
-------------------------- ---------- ---------- ---------- ---------- -
Calling Program's form
-------------------------- ---------- ---------- ---------- ----------
object Form1: TForm1
Left = 192
Top = 123
Width = 870
Height = 640
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 76
Top = 92
Width = 75
Height = 25
Caption = 'Test Email'
TabOrder = 0
OnClick = Button1Click
end
end
-------------------------- ---------- ---------- ---------- ---------- ---
Calling Program's code
-------------------------- ---------- ---------- ---------- ---------- ---
unit dlltest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure email(sendto,subject: string; body: tstrings);
var
Form1: TForm1;
var emailto, emailsubject : string;
emailbody : tstrings;
implementation
{$R *.DFM}
procedure email ;
external 'EMAILIT.DLL' name 'email';
procedure TForm1.Button1Click(Sender : TObject);
begin
emailbody := tstringlist.Create;
emailto := 'somebody@somehost.com'; {change this to suit your purposes}
emailsubject := 'Testing email DLL';
emailbody.add('Testing dll emailer');
email(emailto,emailsubject ,emailbody );
emailbody.free;
end;
end.
-------------------------- ---------- ---------- ---------- ---------
if anyone can tell my why this thing keeps screwing up and not releasing the exe from memory, I would be greatly appreciative.
Thanks in advance
Here is my problem. I have just ventured into DLL creation and I am having issues with the stinking things.
I created a DLL to send email to one or more recipients. After a bunch of bashing around, I got it to actually send the email.... but after it does that and I exit the calling program, the calling program sits im memory wasting space.
I am including the source code for the dll and the calling program.
here's the DLL
--------------------------
library emailit;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
Sharemem,
SysUtils,
Classes,
windows,
dialogs,
comobj, activex;
const
// for email capability
olFolderDeletedItems = $00000003;
olFolderOutbox = $00000004;
olFolderSentMail = $00000005;
olFolderInbox = $00000006;
olFolderCalendar = $00000009;
olFolderContacts = $0000000A;
olFolderJournal = $0000000B;
olFolderNotes = $0000000C;
olFolderTasks = $0000000D;
olMailItem = 0;
var
outlook, nmspace, mailitem: olevariant;
{$R *.RES}
procedure email(sendto, subject: string; body: tstrings);
var str1, str2, str3 : string;
pos1 : integer;
begin
if coinitialize(nil) >= 0 then
try
outlook := getactiveoleobject('outloo
except
outlook := createoleobject('outlook.a
end
else
exit;
nmspace := outlook.getnamespace('MAPI
nmspace.logon(emptyparam, emptyparam, false, true);
mailitem := outlook.createitem(olmaili
{more than one recipient in this field will be parsed into individual recipients and added seperately through this routine}
pos1 := pos(';',sendto);
if pos1 = 0 then
mailitem.recipients.add(se
else
begin
str3 := sendto;
repeat
str1 := copy(str3,1,pos1-1);
str2 := copy(str3,pos1+1,(length(s
mailitem.recipients.add(st
str3 := str2;
pos1 := pos(';',str3);
until pos1 = 0;
mailitem.recipients.add(st
end;
mailitem.subject := subject;
mailitem.body := body.text;
mailitem.send;
nmspace.logoff;
outlook := unassigned;
end;
exports
email;
begin
end.
--------------------------
The calling program is just a blank form with a button on it which sends an email
calling app's project file :
--------------------------
program emaildlltest;
uses
ShareMem,
Forms,
dlltest in 'dlltest.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TFo
Application.Run;
end.
--------------------------
Calling Program's form
--------------------------
object Form1: TForm1
Left = 192
Top = 123
Width = 870
Height = 640
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 76
Top = 92
Width = 75
Height = 25
Caption = 'Test Email'
TabOrder = 0
OnClick = Button1Click
end
end
--------------------------
Calling Program's code
--------------------------
unit dlltest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure email(sendto,subject: string; body: tstrings);
var
Form1: TForm1;
var emailto, emailsubject : string;
emailbody : tstrings;
implementation
{$R *.DFM}
procedure email ;
external 'EMAILIT.DLL' name 'email';
procedure TForm1.Button1Click(Sender
begin
emailbody := tstringlist.Create;
emailto := 'somebody@somehost.com'; {change this to suit your purposes}
emailsubject := 'Testing email DLL';
emailbody.add('Testing dll emailer');
email(emailto,emailsubject
emailbody.free;
end;
end.
--------------------------
if anyone can tell my why this thing keeps screwing up and not releasing the exe from memory, I would be greatly appreciative.
Thanks in advance
Maybe you have to call CoUninitialize....
ASKER
Epsylon:
I tried that and it made the DLL crap out rather drastically ;)
I tried that and it made the DLL crap out rather drastically ;)
Try compiling the .EXE and the .DLL with packages enabled.
Cheers,
Raymond.
Cheers,
Raymond.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Madshi: setting the olevariants to unassigned made the hanging up problem go away.
Great! Thanks!
Great! Thanks!
ASKER
Madshi: setting the olevariants to unassigned made the hanging up problem go away.
Great! Thanks!
Great! Thanks!