Jump to content
  • Advertisement
Sign in to follow this  
Cybrosys

[.net] DLL EntryPoint

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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[].

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!