Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6533345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:06:50+00:00 2026-05-25T10:06:50+00:00

I need to hook on to the Application’s OnDeactivate event in C++ Builder. So

  • 0

I need to hook on to the Application’s OnDeactivate event in C++ Builder. So I need to write my own function to run when the OnDeactivate event fires for the application, but I don’t know where or how to define that function.

Ideally I would like my code to look something like this:

WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
  try
  {
     Application->Initialize();
     Application->OnDeactivate = myFunction;
     Application->Run();
   }

later:

 void myFunction(TObject *Sender)
 {
 //Do Stuff
 }

When I write it like this in my .cpp file, it complains

cannot convert ‘void(*)(TObject *)’ to ‘TNotifyEvent’

If I change my function to return a TNotifyEvent (which shouldn’t work anyway), it gives me the hilarious error of

Cannot convert ‘TNotifyEvent’ to ‘TNotifyEvent’

So, how should I go about writing a function to hook to my Application property?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:06:50+00:00Added an answer on May 25, 2026 at 10:06 am

    You are trying to assign a stand-alone function where a non-static class method is expected instead. You have two choices:

    1) Move your event handler into a helper class:

    class Helper
    {
    public:
        void __fastcall myFunction(TObject *Sender)
        {
            // ... do stuff
        }
    };
    
    WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
    {
        try
        {
            Application->Initialize();
    
            Helper helper;
            Application->OnDeactivate = &helper.myFunction;
            ...
        }
        ...
    }
    

    2) Leave the function as a non-class function, but give it an extra parameter to receive the compiler’s this pointer, and then use a TMethod struct to help you pass it to the event as a suitable TNotifyEvent:

    void __fastcall myFunction(void *pThis, TObject *Sender)
    {
        ...
    }
    
    WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
    {
        try
        {
            Application->Initialize();
    
            TMethod m;
            m.Data = NULL; // passed to the pThis parameter, can be whatever you want
            m.Code = &myFunction;
            Application->OnDeactivate = reinterpret_cast<TNotifyEvent&>(m);
            ...
        }
        ...
    }
    

    With that said, TApplication::Run() will exit immediately if a MainForm is not assigned, so the simpliest solution is to just drop a TApplicationEvents component onto your MainForm and then you can assign an OnDeactivate event handler to it at design-time.

    Update: alternatively, if your project has any TForm or TDataModule objects, you can simply drop a TApplicationEvents component on one of them, and assign an OnDeactivate event handler to it at design time. It will then hook into the Application’s OnDeactivate event for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Webkit-sharp to embed Webkit into an application and I need to hook
There are few string lists in my web application that i don't know where
I need to take an existing winforms application and drop into an event tracing
I know how to write and use windows hook DLLs to capture things such
I need to create plugins that hook into the functionality of my main application.
I need to write an agent for my java application, that does some specific
Need a function that takes a character as a parameter and returns true if
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
Need to call a filter function on some options based on a radio selected
I'd like to write an application which monitors a mailbox (ideally a Gmail mailbox).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.