Click here to Skip to main content
15,898,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
[DllImport("DriveLibrary.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint =_TogglePause)]
public static extern bool _TogglePause(bool b);

public bool TogglePause(bool b)
{
debug("TogglePause");
return _TogglePause(b);
}

What I have tried:

string filepath=Environment.CurrentDirectory + @"\DLL\" + DriveLibrary.dll;

[DllImport(filepath, CallingConvention = CallingConvention.Cdecl, EntryPoint =_TogglePause)]
public static extern bool _TogglePause(bool b);

public bool TogglePause(bool b)
{
debug("TogglePause");
return _TogglePause(b);
}
Posted
Updated 14-Apr-16 20:31pm
Comments
Sergey Alexandrovich Kryukov 15-Apr-16 2:12am    
This is possible, but why? It would be something unstable, not working on some other computer. Simple change in number of removable drive would break it. Better don't do it.

—SA
Garth J Lancaster 15-Apr-16 2:20am    
:-) I've re-iterated this at the bottom of my reply
Sergey Alexandrovich Kryukov 15-Apr-16 2:30am    
Good point!
—SA
Member 10958630 15-Apr-16 2:28am    
All I want is to Import Dll From Usb Devices becasue My dll is not same for all the computers.
Sergey Alexandrovich Kryukov 15-Apr-16 2:36am    
Bad idea. Very bad. One day your removable drive is "E" and all works, another day it's "F". And you cannot dynamically scan drives, find the "right" one, as the path should be static. Just a waste.

Here is the problem: you have to formulate your ultimate goals. What you do is mixing the problem with your views on how it should be solved; we don't know your ultimate goals and cannot suggest a reasonable alternative...

You did not provide any reasonable motivation. Why, for example you cannot create an installed doing different deployment for different machines? Yes, installer is a bad thing, but not where it is important; and you are falling into the opposite extreme...

—SA

in a word, 'no', not as you have tried it it above - there's a defined route that's taken to locate DLL's :-

- starting from the directory the Exe is located in,
- then the system directory,
- windows directory,
- current directory,
- then directories listed in the PATH variable

the one way to short circuit the search route I mention is to use the SetDllDirectory, which means a P/Invoke

C#
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);


All that being said, some corporates block things being run from USB drives, for security reasons

[edit] I'd have issues running things from a usb for lots of reasons - I'd want to make sure it was there for a start. Why dont you set Environment.CurrentDirectory 'down' to the DLL directory you indicate and then set it back before your program exits ? [/edit]

[edit2... and as SAK points out, and I'll paraphrase, 'just because you can, doesnt mean you should' [/edit2]
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 15-Apr-16 2:27am    
I'm not sure. At the moment one calls this function, other P/Invoked functions are already bound. Don't you think so? Have I missed something?

I suggested another idea in Solution 2, but doubt the whole problem makes enough practical sense to bother about...

—SA
Garth J Lancaster 15-Apr-16 3:16am    
well, it seems its possible as long as its (obviously) set before you call the imported function - which implies a late loading - ok, I'd expect that given my history with P/Invoke .. still, its not a great solution, I'd be worried about the status quo after my program was finished, so if a gun was pointed to my head and I had no choice but to use it, I'd be doing a GetDllDirectory first, restoring that value at program exit https://msdn.microsoft.com/en-us/library/windows/desktop/ms686203(v=vs.85).aspx
It is possible as it is also a disk space (through it is removable).
But why you want to do that ?

Just a suggestion:
it looks in your code you have use, Environment.CurrentDirectory but do not use it, since it can be changed in a number of ways (shotrtcut settings, etc). Try one of these options instead:
C#
System.IO.Path.GetDirectoryName(Application.ExecutablePath);

or
C#
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

I have seen Environment.CurrentDirectory changed their path inadvertently if use something like an OpenFileDialog.
 
Share this answer
 
What you trying to do is doubly wrong.

First of all, a .NET attribute needs static parameters. That said, it has to be hard-coded path name.
Also, this attribute is totally unrelated not only to current directory, but is not directly related to the executable module location.

In my comment to the question, I tried to explain that if you successfully achieve loading a native module from a removable drive, the situation will become even worse than now when you cannot solve this problem. :-)
If your solved the problem, the solution would work only for some certain environment, when, say, the name of the USB device's volume matches what you expected.

The only reasonable use of this attribute is fixed location of the native module to the .NET assembly using it, say, in the same directory. That said, if you should have the module on the USB drive, the assembly should be there, too.

Yes, there is one apparent, more complicated solution. You can have "small" assembly developed for the sole purpose of using some native module and exposing its functions to the managed. It should be in the same volume come with that native module. And using assembly should load this "small" middleman assembly using Reflection.

Yes, this is certainly the solution, an ugly one, but… why? All I can imagine looks totally insane to me. :-)

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900