Link to home
Start Free TrialLog in
Avatar of Waldir-PRG
Waldir-PRGFlag for Brazil

asked on

How to identify the location of the dll and call dllimport?

I have a class that uses the DLLImport().
In the first parameter, you must pass the path + name of the dll.

 Example:

 c:\\project \\bin\\[name of dll]

 When I run in the local environment, the process runs successfully.

 How to access it with web application?

 How to identify the application folder and pass the first parameter of DllImport?

 Something like ...

 _dllLocation const string = "c: \\ project \\ bin \ [dll name] ";

 [DllImport (_dllLocation, CharSet = CharSet.Ansi, CallingConvention CallingConvention.Cdecl =)]

 How to pass the first parameter without passing a fixed path?
Avatar of Waldir-PRG
Waldir-PRG
Flag of Brazil image

ASKER

a correction in the statement...

const string _dllLocation = "c:\\projeto\\Bin\\[nome da dll]";

...
Avatar of AngryBinary
AngryBinary

From the context of a web application, you can get the current application path with this:

HttpContext.Current.Request.ApplicationPath

This is a virtual path (URL), and not a file system path. To convert it to a file system path, use this:

HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath)

That gives you the absolute path to the current web application root folder. So, assuming the DLL you want to load is in the app's "bin" folder, this would be the path to your DLL:

HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath) + "\\bin\\" + dllName
Avatar of Todd Gerbert
I'm not entirely sure you can use a variable in the DllImport attribute, it might need to be hard coded path (I'm going off  memory though, and can't be positive without looking it up). Though if the DLL's in the system PATH or in your web app's Bin folder you should be able to specify just the file name.
Ok. I understood that I can use

HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath) + "\\bin\\" + dllName

to identify the path.

However, DLLimport just receives in the first parameter a field "const string" and I can't save this call in a field "const" .

If a use this code:

const string _dllLocation = System.Web.HttpContext.Current.Request.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "\\bin\\nameofdll";

[DllImport(_dllLocation, ....

I generate a mistake:

The expression needs to be const.

Do you understant ?

How can I save or convert  "System.Web.HttpContext.Current.Request.MapPath...." to a "const string" ?
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America 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
I tried to use this command, starting the project in localhost, but a received an error when I called the function that uses DLLImport

It's not possible to load DLL 'bin\nameLibrary.dll' (Exception HRESULT: 0x8007007E)

I just got to call the dll when I specified the full path

"c:\\\project\\bin\\nameLibrary.dll"

Same putting @, the result was equals

Putting:
@"c:\\\project\\bin\\nameLibrary.dll"
the dll was loaded

Same putting two bars between the folders at the path, with a simple path
@"bin\\nameLibrary.dll"
It was showed the error (Exception HRESULT: 0x8007007E)

I will try to save the dll inside the path system32..
Perfect tgerbert!

I saved just the name of dll in the first parameter of DLLImport.

To load the project at the localhost, I needed do save the dll at the path

Windows\syswow64 (Windows 64 bits).

To load at the web, just the name of ddl it was sufficient.

Thanks a lot.