GetModuleFileName not working properly

GetModuleFileName(GetModuleHandle(L"Atx.ocx"),acControlPath,MAX_PATH);

In one of my activeX's this returns me the path that my OCX is registered from, in another activeX this returns the path of the program that the activeX is in.  WHY do I get the second result, the path of the program that the activeX is run in.  I don't want that at all.  How do I fix this????

Thanks,
cophiAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
>>WHY do I get the second result, the path of the program that the activeX is run in.

It seems that 'GetModuleHandle()' returns NULL in that case. When are you calling that function?

You could separate thet, e.g.

HMODULE hMod = GetModuleHandle(_T("Atx.ocx"));

if ( hMod != NULL) {
  GetModuleFileName(hMod,acControlPath,MAX_PATH);
} else {

    // error obtaining module handle
}
0
 
cophiAuthor Commented:
Ok I figured it out..... for some reason.... one of my ocx's is registered C:\Documents and Settings\name\programs\Atx.ocx.  Another one is registered C:\Documents~\na~\pro~\ATX~1.OCX.  Why would it do this???? Why for one it registers the full and complete path and the other one has the stupid ~
0
 
jkrCommented:
That depends how you are registering it. Technically, both paths are identical.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cophiAuthor Commented:
yes, but then I have to do HMODULE hMod = GetModuleHandle(_T("ATX~1.OCX"));  How do I know how I'm registering it.  I'm just building this within MVC++
0
 
jkrCommented:
Ah, now I see your problem. Store the handle in 'DllMain()', so you don't need to rely on the name, e.g.

HMODULE g_hThisModule = NULL;

BOOL
WINAPI
DllMain ( HINSTANCE hInst, DWORD dwReason, LPVOID) {

    g_hThisModule = (HMODULE) hInst;

//Rest of code.

return TRUE;
}
 
// ...

if ( g_hThisModule != NULL) {
  GetModuleFileName(g_hThisModule,acControlPath,MAX_PATH);
} else {

    // error
}
0
 
cophiAuthor Commented:
Ok now how do I get that name if its an activeX
0
 
jkrCommented:
Which name do you mean? There is no need to do so using the above code.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
0
 
cophiAuthor Commented:
This is the main part of my ActiveX

CAtxApp NEAR theApp;

const GUID CDECL BASED_CODE _tlid =
            { 0x836DBAF6, 0xDCE7, 0x4CEF, { 0x92, 0xE1, 0x6C, 0x99, 0x15, 0x99, 0x61, 0xA9 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;



// CAtxApp::InitInstance - DLL initialization

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

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

      return bInit;
}



// CAtxApp::ExitInstance - DLL termination

int CAtxApp::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;
}

Do I add the DLLMain function???  I was trying to get at the point that I dont have a DLLMain
0
 
jkrCommented:
>>Do I add the DLLMain function??

Yes, just add it.
0
 
cophiAuthor Commented:
I added it just like this, and then set a breakpoint and it never went into the code

HMODULE g_hThisModule = NULL;

BOOL WINAPI DllMain ( HINSTANCE hInst, DWORD dwReason, LPVOID) {

    g_hThisModule = (HMODULE) hInst;

    return TRUE;
}

0
 
Joerg_LehmannConnect With a Mentor Commented:
You can use the AfxGetStaticModuleState() function:

AFX_MODULE_STATE* state = AfxGetStaticModuleState();
GetModuleFileName(state->m_hCurrentInstanceHandle,acControlPath,MAX_PATH);

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.