Solved

Pointer to a function or something.

Posted on 2002-06-28
10
161 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now