2

Heres the situation I'm trying to create a program that will read a string and use that string to dll a function in a dll.

See this

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void MessageBoXy(string a, string b);

UnmanagedLibrary test = new UnmanagedLibrary("Test.dll");
MessageBoXy testX = test.GetUnmanagedFunction<MessageBoXy>("Testy");
testX.DynamicInvoke("Hello there", "lol");

When i use DynamicInvoke my "Test.dll" is called and MessageBoxA is executed from the dll using the strings passed through c#....

Thats all well and good but heres where the thing i need help with comes in....

You see this private delegate void MessageBoXy(string a, string b); its static and cannot be changed..... Now i need the program to read a string and change return value and params for the Delegate.... For example

I want to read a string like "Call Test.dll RETURN string PARAMS int, int, char, string"

Now it should be possible to infinitely expand the params and create the delegate for it then call the function in the DLL.

Im making a kind of macro program and i want the user to be able to enter a DLL a return value and params then call the dll so i have no idea what params or return value will be required by the user so the c# code must be able to create any delegate i need.

What do I need?

I need a class or an example that shows me how to spawn the object set a return value, set params then call the object using an array of object[] which will become the params such as myFunc(string a, string b) will be created and object[0] will be param 1 and so on....

The bottom line is i need to be able to dynamically call dll files from user made scripts.

Stedy
  • 7,359
  • 14
  • 57
  • 77
user1057061
  • 61
  • 1
  • 4
  • probably duplicate of http://stackoverflow.com/questions/2470918/listing-functions-of-an-unmanaged-dll-at-runtime-in-c-sharp or http://stackoverflow.com/questions/1128150/win32-api-to-enumerate-dll-export-functions – BlackICE Nov 21 '11 at 03:36
  • Closing as per the 'answer' you posted. – Tim Post Nov 22 '11 at 02:56

2 Answers2

0

Here are a couple q/a that should get you started:

Listing functions of an unmanaged DLL at runtime in c#

Win32 API to enumerate dll export functions?

Community
  • 1
  • 1
BlackICE
  • 8,816
  • 3
  • 53
  • 91
0

So the user is going to give you a string something like "Call Test.dll RETURN string PARAMS int, int, char, string", and you want to generate the code to call it? And, I suppose, call it?

First, "Call Test.dll" doesn't say which function to call in Test.dll. I assume you meant that the user would say which function to call in the DLL? Or is your program supposed to enumerate the functions and determine which one to call?

I'll assume that the user specifies the function as well as the DLL name. If that's not the case, see one of the comments for information on how to enumerate functions in a DLL.

However you obtain the name of the function to be called, you still need to write code that will call that function.

There are at least two ways to go about this. You can create and call a DynamicMethod. Or you could use the CodeDOM to create a dynamic assembly.

Using a DynamicMethod is more memory efficient, but you have to emit IL, to do it. It would involve a little bit of a learning curve to figure out exactly what you need to generate, but it would probably be the best solution.

The CodeDOM is easier to use than DynamicMethod, but it will generate a new in-memory assembly for every function call. Since an assembly can't be unloaded unless the app domain is unloaded, you'll either have to create the assembly in a new app domain every time, or live with your program slowly eating away at memory. I guess it depends on how many of those calls you expect the user to generate in a session. If it's only a handful, then no problem. But if your program's expected to run for a very long time and make lots (hundreds? thousands?) of these calls (i.e. generate code for a lot of functions), then you'll want to make sure that each one is in its own app domain so that you can unload the app domains to reclaim memory.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • For example the user could enter the line: CallDLL("User32.dll", "int32", "MessageBoxA", "str", "Hello world", "str", "Hello yea"); Of course this is for a macro/scripting thing so the user could use one of them or 10 of them or 1000 of them with any number of parms and return specifications.... I can do this in C/C++ and assembly with no issues at all but with C# i cant just push the params onto a stack and call it.... – user1057061 Nov 21 '11 at 06:00