Waldir-PRG
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?
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?
From the context of a web application, you can get the current application path with this:
HttpContext.Current.Reques t.Applicat ionPath
This is a virtual path (URL), and not a file system path. To convert it to a file system path, use this:
HttpContext.Current.Reques t.MapPath( HttpContex t.Current. Request.Ap plicationP ath)
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.Reques t.MapPath( HttpContex t.Current. Request.Ap plicationP ath) + "\\bin\\" + dllName
HttpContext.Current.Reques
This is a virtual path (URL), and not a file system path. To convert it to a file system path, use this:
HttpContext.Current.Reques
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.Reques
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.
ASKER
Ok. I understood that I can use
HttpContext.Current.Reques t.MapPath( HttpContex t.Current. Request.Ap plicationP ath) + "\\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.Cur rent.Reque st.MapPath (System.We b.HttpCont ext.Curren t.Request. Applicatio nPath) + "\\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.Cu rrent.Requ est.MapPat h...." to a "const string" ?
HttpContext.Current.Reques
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.Cur
[DllImport(_dllLocation, ....
I generate a mistake:
The expression needs to be const.
Do you understant ?
How can I save or convert "System.Web.HttpContext.Cu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\\nameLi brary.dll"
Same putting @, the result was equals
Putting:
@"c:\\\project\\bin\\nameL ibrary.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..
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\\nameLi
Same putting @, the result was equals
Putting:
@"c:\\\project\\bin\\nameL
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..
ASKER
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.
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.
ASKER
const string _dllLocation = "c:\\projeto\\Bin\\[nome da dll]";
...