Link to home
Start Free TrialLog in
Avatar of cmandan
cmandan

asked on

getting hInstance in an ocx

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
Avatar of cmandan
cmandan

ASKER

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
An OCX is a DLL. So there is a DllMain which gives you hInstance.
Avatar of cmandan

ASKER

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
Avatar of cmandan

ASKER

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
Avatar of jkr
You could just use

HINSTANCE hInst = GetModuleHandle ( "mycontrol.ocx");
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?
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.
Avatar of cmandan

ASKER

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???
No.

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

ASKER

because i cannot include stdafx.h in my program, it gives me errors...
Avatar of cmandan

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cmandan

ASKER

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
Avatar of cmandan

ASKER

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

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

chirag
Avatar of cmandan

ASKER

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
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=
Avatar of cmandan

ASKER

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
Avatar of cmandan

ASKER

https://www.experts-exchange.com/questions/20880566/exporting-from-vc-ocx-to-vb.html

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

thanks,
chirag