[.net] DLL EntryPoint

Started by
9 comments, last by Rob Loach 18 years, 8 months ago
Is it possible to specify an entry point for a dll within the dll's assembly? Why would i want to do that you might ask, well, seeing as i'm loading the DLL dynamically, it would be great to be able to extract the EntryPoint from the dll's assembly instead of wishing the author created a public static void DLLMain() { .. } function somewhere. ("Wishing", as in, being too lazy to go through the Assembly to find a function with the name DLLMain or Main, what if the author misspelled it? hehe)
Advertisement
What do you mean by "within the assembly"? I'm not that familar with .net, but it seems to me that the compiler should provide the option to stipulate which function serves as the entry point to the module (DllMain or what have you). The specific value is actually encoded into the PE file header for the module, specifically IMAGE_OPTIONAL_HEADER.AddressOfEntryPoint. Consult: An In-Depth Look into the Win32 Portable Executable File Format. If that doesn't do it for you, google: pietrek+pe, should point towards more info.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Assembly a = Assembly.LoadFile("plugin.dll");

"a.EntryPoint" is null for .dll files.

So, i was wondering if it's possible to specify a custom entrypoint, and if so, how.

Edit:

I can't, for the life of me, seem to be able to invoke a Main that takes parameters through MethodInfo.Invoke.

(DLL)
public static void Main(string[] args)

(EXE)
entryPoint.Invoke(this, null); <- doesn't work
entryPoint.Invoke(this, new object[]{""}); <- doesn't work either

I've tried using the other invoke overloads but none work, i get the exception:
Unhandled Exception: System.Reflection.TargetParameterCountException: Parameter count mismatch.

It does work if in the dll i replace string[] with string.

[Edited by - Cybrosys on July 25, 2005 3:39:49 PM]
I believe you want:

entryPoint.Invoke(null, new object[]{new string[]{}});

assuming you are trying to call your Main function.

Since Main is static, you want to pass null instead of this. Also, you need to pass it an array of the parameters you want to pass to the function. Since Main takes a string[], you need to pass it an array of objects whose only element is a string[].
Ahh, thank you, that made sense. Btw, the first parameter being passed into invoke, how exactly is it used or can be used? What's the meaning of it? If main wasn't static i should have passed in a ref to the class instance that the non static funtion resided in?
Quote:If main wasn't static i should have passed in a ref to the class instance that the non static funtion resided in?


Correct.
I'm still getting null for EntryPoint whenever I load the assembly. The DLL contains public static void Main(string[] args) and I'm using EntryPoint.Invoke(null, new object[]{new string[]{}}); but it doesn't even matter because EntryPoint is null. The code is compiled properly and everything....

Have any ideas what I'm doing wrong?
Rob Loach [Website] [Projects] [Contact]
I think you should read what this entire post is about and what it brings up. What it brings up is just the fact that .NET DLLs doesn't get their EntryPoint set, hence me asking wether or not it is possible to set it manually.

You'll also se that my solution was to iterate through a DLL's assembly looking for a function that matched the search criteria that the return type is Type.Void, public, static and the function name contains the word Main.

However, that's dangerous because if the author of the DLL has more than one function named *Main* it'll become unsafe, you won't know which function you'll get returned, hence bringing us back to the very thing i was asking about in the thread, is there a way to set a DLLs EntryPoint?

Edit:

Sure there are other "Design solutions", like a plugin dll implementing an interface.
I don't know if there is an "official" way to do this, but you could just create and interface "IRunnable" and have any dynamically loaded scripts/programs implement it. Then your program would just look for any class that implements IRunnable within the assembly, create a reference to it, and then call the Run function through the interface. Does this help at all with what you want to do?
Turring Machines are better than C++ any day ^_~
Quote:Original post by Cybrosys
Sure there are other "Design solutions", like a plugin dll implementing an interface.


Though, didn't know there existed a interface called IRunnable.

This topic is closed to new replies.

Advertisement