We help IT Professionals succeed at work.

getting hInstance in an ocx

cmandan
cmandan asked
on
1,269 Views
Last Modified: 2013-12-03
Hi All:

I want to get hInstance in an ocx so that I can pass it to DialogBoxParam() function to create a dialog box in my ocx..how do I get this hInstance........???

In an exe, you get hInstance from the argument of WinMain(HINSTANCE hInstance...),
but how do you get it in ocx???? there is no winmain here.....

Really appreciate your responses here...

thanks,
chirag
Comment
Watch Question

Author

Commented:
ok...I am NOT using MFC.......I guess, in mfc you could use AfxGetInstanceHandle() to get the instance of the application, though I am not 100% sure...but I would like to not include stdafx.h in my program...

pls assist..

thanks,
chirag

Commented:
An OCX is a DLL. So there is a DllMain which gives you hInstance.

Author

Commented:
Hi Chensu:

I tried doing a "Find in files" in my project for DllMain, but could not find anything....where is this dllmain located, i mean , which file??? Also, how do i get his hInstance into my function, just by regular passing as an argument or some in-built function will do it for me?? Please guide me through this.....I am willing to increase points for this....

thanks,
chirag

Author

Commented:
Chensu,

this is something that I have, if this would help.....this file was automatically created when you create an ocx from wizard....


#include "stdafx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CApp NEAR theApp;

const GUID CDECL BASED_CODE _tlid =
            { 0xaa5b4e4c, 0x1375, 0x437d, { 0xa4, 0x7e, 0xb8, 0xd7, 0xbc, 0x65, 0x4e, 0x32 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;


////////////////////////////////////////////////////////////////////////////
// CApp::InitInstance - DLL initialization

BOOL CApp::InitInstance()
{
      BOOL bInit = COleControlModule::InitInstance();

      if (bInit)
      {
            // TODO: Add your own module initialization code here.
      }

      return bInit;
}


////////////////////////////////////////////////////////////////////////////
// CApp::ExitInstance - DLL termination

int CApp::ExitInstance()
{
      // TODO: Add your own module termination code here.

      return COleControlModule::ExitInstance();
}


/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
      AFX_MANAGE_STATE(_afxModuleAddrThis);

      if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
            return ResultFromScode(SELFREG_E_TYPELIB);

      if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
            return ResultFromScode(SELFREG_E_CLASS);

      return NOERROR;
}


/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
      AFX_MANAGE_STATE(_afxModuleAddrThis);

      if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
            return ResultFromScode(SELFREG_E_TYPELIB);

      if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
            return ResultFromScode(SELFREG_E_CLASS);

      return NOERROR;
}

while registering dll, it does use AfxGetInstanceHandle() to get the instance handle...I don't know if this could be used....I searched for dllmain, but there is no dllmain in my ocx.....

thanks,
chirag
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You could just use

HINSTANCE hInst = GetModuleHandle ( "mycontrol.ocx");

Commented:
Aren't you using MFC? You can use AfxGetInstanceHandle() to get the instance handle. But since you are using MFC, why don't you use the MFC CDialog class?

Commented:
jkr>You could just use
jkr>HINSTANCE hInst = GetModuleHandle ( "mycontrol.ocx");

Yes, you could. But it is not a good practice. Your binary file name is hard-coded in your program. It will be broken once the file name is changed.

By the way, the DllMain in an MFC DLL is wrappered by the MFC library code.

Author

Commented:
I really liked....HINSTANCE hInst = GetModuleHandle ( "mycontrol.ocx");....

but I want to specify "this" instead of the file name...can i do that??? file path can change and that can create problems....can u suggest a way to solve this???

Commented:
No.

You haven't answered my questions. Why don't you use AfxGetInstanceHandle()?

Author

Commented:
because i cannot include stdafx.h in my program, it gives me errors...

Author

Commented:
chensu, u can also guide me with HINSTANCE hInst = GetModuleHandle ( "mycontrol.ocx")....I am willing to give u the points as well, if you can make this path independant......but i would not like to use mfc here.....

thanks,
chirag
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
ok so i tried this.....in the mfc ocx files, i tried,

extern HINSTANCE hInstance = AfxGetInstanceHandle();

and in the file where I want to use hInstance that does not have stdafx.h, i just do

HINSTANCE hInstance.....and use it....

however, I don't get the right hInstance and so I don't get the right results......I got it working with GetModuleHandle...so i am more inclined towards it...

thanks,
chirag

Author

Commented:
what about HINSTANCE hInstance = GetModuleHandle (NULL);??

though, its still not giving the right handle......

chirag

Author

Commented:
HINSTANCE hInstance = GetModuleHandle ( "webcontrol.ocx");
HINSTANCE hInst = GetModuleHandle (NULL);

hInstance value is 0x10000000 and hInst is 0x00400000....so they are both different......somewhere i read that GetModuleHandle (NULL) should give you the current application handle....though it does not seem to be the case....wierd!!

I would appreciate suggestions from u......

thanks,
chirag

Commented:
One is the DLL handle while another is the EXE handle.
Howdy howdy, if you don't want to use MFC (and who could blame you :) then DllMain isn't going to be hidden away anywhere. This means you can stick it in yourself. DllMain is optional most of the time. When you make a DLL you can just export the functions you want to, but if you add DllMain then you can make the DLL do some init stuff when a process (another prog) connects to it and you can then do cleanup when it's being disconnected (closed down). Here's the default DllMain:

BOOL APIENTRY DllMain( HINSTANCE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        // init stuff
    case DLL_THREAD_ATTACH:
        // thread attach specific init stuff
    case DLL_THREAD_DETACH:
        // thread detach specific cleanup stuff
    case DLL_PROCESS_DETACH:
        // cleanup stuff
        break;
    }
    return TRUE;
}

Anyway, you can just plonk the above into your code, and abracadabra! ...windows automatically hands you the DLL module's handle (...HINSTANCE hModule...), which I believe is what you wanted :)

See this link for the MSDN library page on DllMain:
http://msdn.microsoft.com/library/en-us/dllproc/base/dllmain.asp

Don't forget to export DllMain amongst your other DLL functions!

=me=

Author

Commented:
though I got the HInstance using some other custom program, I am giving chensu points for this because of his active participation in this question...

thanks,
chirag

Author

Commented:
https://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20880566.html

chensu and jkr....pls look into it...and give your suggestions!!

thanks,
chirag

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.