 |
|
|
 |
|
 |
Hi,
first of all : i'm new to C# and my skills are near the point zero.
That said, i was wondering if there's a way to declare and use dll functions knowing there names and parameters (type and numbers) at run time and not at design time ?
Thanks
--
Fred
|
|
|
|
 |
|
 |
Hi,
I got your function to work properly. The only problem i have is after i run your code the Unmanaged DLL i am using to execute is getting locked by application. Is there a way to unload a DLL?
|
|
|
|
 |
|
 |
[DllImport("DynaAPI32.dll",CallingConvention=CallingConvention.StdCall)]
unsafe public static extern int FN_DioReadBitDirect(long handler, byte bitAddress, byte* BitValue);
private void button1_Click(object sender, EventArgs e)
{
unsafe
{
byte* b = (byte*)(sizeof(byte)*1);
j = FN_DioReadBitDirect(l, 4, b);
}
}
when call this function its generated under given error??
An unhandled exception of type 'System.AccessViolationException' occurred in your application exe.
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
u have any ideas to solve this error???
|
|
|
|
 |
|
 |
can you send me how did you call the function?
|
|
|
|
 |
|
 |
I have tried the code. I am able to call a DLL function, arguments are passed properly, but I cannot get the returned value. When working with int-returing functions I get always 0, when working with strings I get always null. I have tried the DLL in the static way (DllImport attribute] and it works. Can somebody help me?
BTW. I found another solution of this problem here: http://www.codeproject.com/csharp/legacyplugins2.asp
but it has one limitation - name of the function to call must be known at compile-time.
--
Michal
|
|
|
|
 |
|
 |
i played around with this code and was able to get it to function *almost* the way i wanted. my problem is with sending a ref string so i can get something back. i changed the type to be something like
Type[] paramTypes = { Type.GetType( "System.String&" ) };
and my unmanaged dll gets the string, and changeds it correctly (i've verified this by printing the string within the dll), but i always get the same string i passed in the scope of the C# program that called the dll.
i had to define an implementation flag to get the return type to work, is there a similar flag to get referenced parameters back?
john
|
|
|
|
 |
|
 |
I am having a similar problem but do not understand how I am to define such a flag. Can you please share with me the code you have to get this to work? Also, were you able to get the referenced type back?
Nolan
|
|
|
|
 |
|
 |
I am having the same problem as both of you. Did you get it to work?
|
|
|
|
 |
|
 |
Found it, I guess! Look at IsByRef of Type class.
|
|
|
|
 |
|
 |
Um, why not just use the [DllImport("something.dll")] attribute?
like:
[DllImport("shell32.dll")]
public static extern Int32 SHGetMalloc(out IntPtr hObject);
It seems a lot easier.
Marc
(and no, I didn't give you the 1. I didn't vote at all!)
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
 |
|
 |
Well he did say "Dynamic Invoke", even if he didn't provide a very dynamic example (the function prototype is still basically hardcoded). DllImport is pretty static isn't it?
Chris Richardson
|
|
|
|
 |
|
 |
Chris Richardson wrote:
DllImport is pretty static isn't it?
I'm not sure. I should see if it loads the dll when loads the application, or if it loads the dll when the first P/Invoke is made to it. If .NET already does the dll loading dynamically, then it's a moot point.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
 |
|
 |
Marc Clifton wrote:
I'm not sure. I should see if it loads the dll when loads the application, or if it loads the dll when the first P/Invoke is made to it. If .NET already does the dll loading dynamically, then it's a moot point.
Ahh, I think our signals are crossed. By dynamic, I meant dynamically P/Invoking different functions (with possibly different prototypes). I think you mean dynamically switching the DLLs, correct?
BTW, after a quick experiment, I think the runtime loads the DLL after the first P/Invoke. However, I'm no expert in .NET; 99% of my work is still C++/VC.
Chris Richardson
|
|
|
|
 |
|
 |
Focus of this article makes you load dll file which name is critical for [dll import] but not for this implementaion. So you can easly load a dll file from with openfiledialog. But you must sure the name of the exported funtion in dll. So in my project i want process incoming datas with different formats but processing dll writtten by different group and they can write only c/c++. And end users can process different incoming datas with different dlls.
also 1 is good too. but 5 is better.
Cal.
|
|
|
|
 |
|
 |
The purpose of using dynamic imports is somewhat like plug-ins. The problem is the majority of plug-ins still reside in unmanaged DLLs. This is a good reason not to use DllImportAttribute, but it doesn't apply to most people.
About the way you do it, Reflection.Emit is a bit heavy, slow and little dynamic to my point of view. Some time ago I provided two article giving other ideas of how to call unmanaged DLLs dynamically using .NET 1.1 and .NET 2.0. For those interested, here are the links:
Using legacy plug-ins with .NET
Using legacy plug-ins with .NET - Part 2
[]'s
Harkos
---
"Money isn't our god, integrity will free our soul."
Cut Throat - Sepultura
|
|
|
|
 |
|
 |
The point is that with the method shown above, you can not only load the DLL dynamically but you can add the params dynamically.
I found similar sample code in MSDN that I looked at a while ago. Now if I could only figure out how to enumerate the methods in the dll and the parameters for those methods.
I know I can read the method list from the PE header, but thats a pain, there has to be a simpler way.
|
|
|
|
 |
|
 |
I hope you understand that passing the wrong set of parameters to a method can cause unpredictable results, from wrong operation to total failure of your system. Also, how will you change the set or arguments passed to the function during runtime?
I'm afraid there is no way, managed or unmanaged, to retrieve the list of parameters to an unmanaged function. Believe me, I spent a lot looking for it too. You will have to go through the PE header by yourself. This is the reason why I gave up using Reflection Emit; consumes too many resources and time for no benefits (at least for this purpose).
[]'s
Harkos
---
"Money isn't our god, integrity will free our soul."
Cut Throat - Sepultura
|
|
|
|
 |
|
 |
I know that a wrong set of parameters could bring the house down but the boss wants the product to load and execute modules dynamically.
There are two ways to retrieve the list of parameters from a method that I've found. If the module was compiled in debug mode or if the methods are exported using mangled names.
|
|
|
|
 |