Solved

Pointer to a function or something.

Posted on 2002-06-28
10
165 Views
Last Modified: 2010-04-04
I have a application and to that application I have a printing dll with a preview form in it.

What I like to do is on my Main Form (in the application)

set TMainForm.Printed := True if someone
Push the Print Button in the preview form.

Is this possible whitout include the TMainForm in the dll? I mean Can I send over a pointer to for example:
TMainForm.Printed ? (it's a boolean)

or eaven better. Can I send over a pointer to
TMainForm.SetPrinted (a procedure)

How?



0
Comment
Question by:wqclatre
  • 5
  • 4
10 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7115777
wqclatre,

Solution a)
----------------

1. Send MainForm.Handle as additional argument to your DLL
   printing procedure
2. Send custom message back from the DLL to the MainForm
   when you want to inform your MainForm that printing is  
   done.
3. Set MainForm.Printed := True inside event handler for
   this message


Solution b)
-----------------

1. Write and export DLL function IsPrinted()
2. Use TTimer in MainForm and periodically call IsPrinted()
0
 
LVL 2

Expert Comment

by:aubs
ID: 7115826
In program main form declare a variable FPrinted:

private
    { Private declarations }
    FPrinted: Boolean;
...

before you call the dll set FPrinted to false then send the address of FPrinted to the dl in the call using @:

  FPrinted := False;
  myPrintDllFunction(@FPrinted);


In the dll declare the function parameter as a Pointer:

  procedure myPrintDllFunction(aPrinted: Pointer); export;

You can view the value by dereferencing the value aPrinted:

  if Boolean(aPrinted^) then beep;


or change the value:

 aPrinted^:= True;


All from head and untested, but might put you in the right direction.

Aubs

0
 
LVL 2

Author Comment

by:wqclatre
ID: 7115940
Cynna

you don't think you could give me an example of a)
(I don't know how to use handles
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:wqclatre
ID: 7115986
when I try to do:

Printed^:= True;


ist says: operator is not applicable to this operand type;

(I have   Printed : Pointer;)
0
 
LVL 2

Author Comment

by:wqclatre
ID: 7116027
One problem I still will have is that I want to set a
checkbox when the item is printed.

It does not seems be possible to do:

  FPrinted: Boolean;
  cbPrinted : TCombobox;

  myPrintDllFunction(@FPrinted, @cbPrinted.checked);
0
 
LVL 2

Author Comment

by:wqclatre
ID: 7116038
Sorry I mean

cbPrinted : TCheckBox.

0
 
LVL 7

Expert Comment

by:Cynna
ID: 7116042
wqclatre,

1) In DLL:
*****************

1.1) Add:

const
  UM_PRINT = WM_USER+1; // Custom message ID

var
  MainFormHandle: HWND; // Global var stores MainFormHandle

(...)


1.2)

Assume your DLL print procedure is called Print.
Add additional argument to this procedure like this:

procedure Print(AHandle: HWND);
begin
  // Store handle of the MainForm widow:
  MainFormHandle:=AHandle;

  //... rest of your code (deal with the printing)...

end;

So, when you call print from your main form, you'll
send its handle to DLL via Print() procedure. There
it's stored in global var for later reuse.


1.3)

When the printing is finished, and you want to notify
the MainForm obout this, just send it message like this:

   // Inform MainForm that printing is finished:
   SendMessage(MainFormHandle, UM_PRINT, 0, 0);




2) In EXE (MainForm):
*****************************


2.1) Add custom message handler declaration like this:


const
  UM_PRINT = WM_USER+1; // Custom message ID (same as in DLL)

Type
  TForm1 = class(TForm)
   ...
   procedure SetPrinted(var Msg: TMessage); message UM_PRINT;
   ...
end;


2.2) In definition do whatever you like:

procedure TForm1.SetPrinted(var Msg: TMessage);
// Called from DLL using SendMessage
begin
  Caption:='Printing finished!';
end;

-----------------------------------

This method also allows you to pass more complex
information then just signal "I'm finished" using
Msg parameters (look at the SendMessage help in Win32
API) if you need to. It's not neccessary in your
example, but this is pretty usefull concept to
understand in communication between DLL and main App.
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7116045
To set cbPrinted just replace line:

   Caption:='Printing finished!';

with:

   cbPrinted.Checked:=TRUE;
0
 
LVL 7

Accepted Solution

by:
Cynna earned 100 total points
ID: 7116052
Sorry, I forgot to tell you how to pass
main form handle to DLL.

Assume you called your print procedure from MainForm like this:

procedure TForm1.Button11Click(Sender: TObject);
// (old way)
begin
  // Call DLL printing procedure:
  Print;
end;

If you change Print() procedure like I showed you in 1.2),
then you should change your calling code like this:

procedure TForm1.Button11Click(Sender: TObject);
// (new way)
begin
  // Call DLL printing procedure, and pass it MainForm handle:
  Print(Form1.Handle);
end;
0
 
LVL 2

Author Comment

by:wqclatre
ID: 7116071
THanks! Woundewrfoul!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 110
Delphi and Access based Enumeration 9 77
How to load 2 images in same column in Delphi 2 67
Using MMsystem To change audio input 1 14
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question