2

I am building a DLL that is used by Office. When Office runs with it, I would like to identify where it is located. Is that possible?

ex. of code within the DLL when it is run within Office:

// should return C:\tmp\officeaddin.dll, 
// currently C:\Program Files\Microsoft Office\Office 12
MessageBox.Show(Application.StartupPath)   
Gad
  • 41,526
  • 13
  • 54
  • 78

4 Answers4

3

The following should work even if running inside of Office:

Getting the path of the current assembly

(It basically boils down to Assembly.GetExecutingAssembly().Location, but see the link above for more detailed information.)

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2

If it is a .NET library, you should use Assembly.GetExecutingAssembly().Location. Application.StartupPath shows the path to the main app.

wRAR
  • 25,009
  • 4
  • 84
  • 97
1

This works for me:

Environment.CurrentDirectory

So you could just set a string to it such as:

Dim location as String = Environment.CurrentDirectory & "\"
TheRyan722
  • 1,029
  • 13
  • 37
  • I see that `Environment.CurrentDirectory` returns the base path of calling executable. if your are in a DLL, being invoked as part of windows service, you would get the path to system root folder.. – r.sumesh Jan 30 '17 at 13:50
0

When the DLL loads, It calls DllMain with the instance handle. If you implement this function, you can then record the instance handle of the DLL. From this, you can then call GetModuleFileName.

In .NET this is taken care of for you. See this question and selected answer for details.

Community
  • 1
  • 1
mdma
  • 56,943
  • 12
  • 94
  • 128