doing some initialization before the constructor gets called

Discussion in 'C++' started by Vamsi, Jul 19, 2005.

  1. Vamsi

    Vamsi Guest

    Hello,

    I am working in VC++ environment.while using gdi+, i want to use a
    public member variable of Bitmap. which needs to be initialized at the
    time of creation. and i m using this gdi+ in a Dll application. i am
    trying to initialize the bitmap object at the constructor. like

    Bitmap m-bmpobj; // Bitmap Obj which is a private member of the
    TextEffect Class

    // TextEffect is the Class name.

    CTextEffect::CTextEffect(LPUNKNOWN pUnk, HRESULT *phr)
    :bitmap(L"C:\\logo1.bmp")
    {
    // initializing width and height mem var.
    m_nWidth=0;
    m_nHeight=0;

    }

    but the application is not initializing the bitmap with the given file.
    because before using gdi+ classes we need to initialize the Gdiplus in
    our application with the following two statements.

    // Initializing gdi+
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

    So, these two statements are to processed before the constructor gets
    called. But the constructor is the first function which gets called
    when the dll is used.

    So Please provide some tips and help to solve this problem.

    Waiting for ur reply soon.

    vamsi
     
    Vamsi, Jul 19, 2005
    #1
    1. Advertising

  2. Vamsi

    benben Guest

    > Hello,
    >
    > I am working in VC++ environment.while using gdi+, i want to use a
    > public member variable of Bitmap. which needs to be initialized at the
    > time of creation. and i m using this gdi+ in a Dll application. i am
    > trying to initialize the bitmap object at the constructor. like
    >
    > Bitmap m-bmpobj; // Bitmap Obj which is a private member of the
    > TextEffect Class
    >
    > // TextEffect is the Class name.
    >
    > CTextEffect::CTextEffect(LPUNKNOWN pUnk, HRESULT *phr)
    > :bitmap(L"C:\\logo1.bmp")
    > {
    > // initializing width and height mem var.
    > m_nWidth=0;
    > m_nHeight=0;
    >
    > }
    >
    > but the application is not initializing the bitmap with the given file.
    > because before using gdi+ classes we need to initialize the Gdiplus in
    > our application with the following two statements.
    >
    > // Initializing gdi+
    > Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    > Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
    >
    > So, these two statements are to processed before the constructor gets
    > called. But the constructor is the first function which gets called
    > when the dll is used.
    >
    > So Please provide some tips and help to solve this problem.
    >
    > Waiting for ur reply soon.
    >
    > vamsi
    >


    Without considering multi-threading:

    void init_gdiplus(void)
    {
    static bool initialized = flase;

    if (!initialized)
    {
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput,
    NULL);

    initialized = true;
    }
    }

    call it in all constructors needing gdiplus resources.

    Regards,
    Ben
     
    benben, Jul 19, 2005
    #2
    1. Advertising

  3. Vamsi

    Me Guest

    Vamsi wrote:
    > CTextEffect::CTextEffect(LPUNKNOWN pUnk, HRESULT *phr)
    > :bitmap(L"C:\\logo1.bmp")
    > {

    ....
    > }
    >
    > but the application is not initializing the bitmap with the given file.
    > because before using gdi+ classes we need to initialize the Gdiplus in
    > our application with the following two statements.
    >
    > // Initializing gdi+
    > Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    > Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
    >
    > So, these two statements are to processed before the constructor gets
    > called. But the constructor is the first function which gets called
    > when the dll is used.


    So then execute those statements before the bitmap constructor gets
    called. Since you told us no context about this problem, I'm guessing
    you have a global variable somewhere so as a lame hack put this above
    the definition of it:

    int dummy_init()
    {
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
    return 0;
    }

    static int dummy = dummy_init();
    ....
    CTextEffect someProblematicGlobalTextEffect;
     
    Me, Jul 19, 2005
    #3
  4. Vamsi

    AbdulMunaf Guest

    Call the gdi+ initialize functions in DllMain() function of dll. This
    functions gets called by the Winodws when the dll is loaded in the
    memory.

    Or you can do the following

    CTextEffect::CTextEffect(LPUNKNOWN pUnk, HRESULT *phr)
    {
    /*---------------------------------
    gdi+ intialize code here
    -----------------------------------*/
    //Bitmap* m_pBitmap
    m_pBitmap = new Bitmap( L"C:\\logo1.bmp" );

    // initializing width and height mem var.
    m_nWidth=0;
    m_nHeight=0;
    }

    Regards
     
    AbdulMunaf, Jul 19, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lucas
    Replies:
    28
    Views:
    6,169
    Dale King
    Dec 5, 2003
  2. Replies:
    2
    Views:
    307
    Alan Johnson
    Feb 1, 2007
  3. Boris
    Replies:
    6
    Views:
    1,370
    Patricia Shanahan
    Apr 21, 2007
  4. balaji
    Replies:
    3
    Views:
    946
    Jens Thoms Toerring
    Aug 10, 2011
  5. avasilev
    Replies:
    23
    Views:
    695
    avasilev
    Dec 9, 2011
Loading...

Share This Page