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?
Waldir-PRGAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Waldir-PRGAuthor Commented:
a correction in the statement...

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

...
AngryBinaryCommented:
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
Todd GerbertIT ConsultantCommented:
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.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Waldir-PRGAuthor Commented:
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" ?
Todd GerbertIT ConsultantCommented:
Yes, that means you can't use a variable name in the DllImport, it needs to be a hard-coded string (as I suspected, above http:#a35356078).

// This doesn't work because you can't use a variable in DllImport
string dllLocation = @"C:\Folder\library.dll";
[DllImport(dllLocation)]
...

Open in new window


// The library location needs to be given directly to the DllImport attribute
[DllImport(@"C:\Folder\library.dll"]
...

Open in new window


I assume this is a DLL you've created yourself, and not a part of Windows?

You should be able to put the DLL in your application's Bin folder and just specifying it's name in the DllImport attribute, or using a relative path, e.g.:
[DllImport(@"Bin\library.dll")]
...

Open in new window


You can also put the DLL in the system path and use just it's name.

MSDN describes the values you can use:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.value(v=VS.100).aspx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Waldir-PRGAuthor Commented:
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..
Waldir-PRGAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.