?
Solved

GetModuleFileName not working properly

Posted on 2006-05-16
12
Medium Priority
?
1,038 Views
Last Modified: 2013-11-20
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,
0
Comment
Question by:cophi
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 16693709
>>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
 

Author Comment

by:cophi
ID: 16693851
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
 
LVL 86

Expert Comment

by:jkr
ID: 16693907
That depends how you are registering it. Technically, both paths are identical.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cophi
ID: 16693934
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
 
LVL 86

Expert Comment

by:jkr
ID: 16693975
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
 

Author Comment

by:cophi
ID: 16694302
Ok now how do I get that name if its an activeX
0
 
LVL 86

Expert Comment

by:jkr
ID: 16694379
Which name do you mean? There is no need to do so using the above code.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16696755
0
 

Author Comment

by:cophi
ID: 16698232
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
 
LVL 86

Expert Comment

by:jkr
ID: 16700979
>>Do I add the DLLMain function??

Yes, just add it.
0
 

Author Comment

by:cophi
ID: 16707949
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
 
LVL 1

Assisted Solution

by:Joerg_Lehmann
Joerg_Lehmann earned 1600 total points
ID: 16760685
You can use the AfxGetStaticModuleState() function:

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

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question