Jump to content
  • Advertisement
Sign in to follow this  
Carn

vc++ .net and dll's

This topic is 4892 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

I'm in proccess of rewriting an in house tool for a local engineering company that handles topographic data. The version they're using now has a horrible interface, is slow and is not very extensible at all. All three issues are pretty simple however in an effort to make it easier to add things to it without actually having to modify the source I've set it up so it's an editor with a lot of plugins in dll files all done using the .net frame work. Problem is I can't get figure out how to link to the dll's at runtime. Each one has an initialize function (only part of the dll that isn't managed) which needs to be called when all the dll's are loaded. So the questions is how do I load the dll at run time given its path and then call the init function to set everything up? Help with this is greatly appreciated as I've been scouring msdn and the net for answers for the last 2 days. Kevin

Share this post


Link to post
Share on other sites
Advertisement
I second the question why isn't the initialization in managed code?

But to answer your question, you'll need to P/Invoke the LoadLibrary, FreeLibrary and GetProcAddress functions. Btw, [google] is your friend.

Share this post


Link to post
Share on other sites
The only reason the init function isn't managed is because I'm not entirly sure how to export a managed function from a dll. This is my first time using dll's with managed code and I really only have the vaguest idea of how to go about it properly. A small example of how to go about exporting a managed function from a dll would be incredibly helpful.

Kevin

Share this post


Link to post
Share on other sites
There's three options for exporting a managed function from a DLL:

A) Your client program is .Net framework aware. You don't have to do anything other than make your class and method public.

B) Your client program is COM aware but not .Net aware. You need to create a type library among other things, and link to that type library in your client program. Look for "COM Callable Wrapper" and you'll probably find a how-to guide.

C) Your client program is neither COM nor .Net aware. You need to export an unmanaged class... which I'm not sure is possible from inside a .Net DLL because I haven't done it.

Share this post


Link to post
Share on other sites
some code showing what's going on

LoadPlugins(String::Concat(Application::StartupPath, "\\plugins"));

void LoadPlugins(String *path)
{
register int i;

String *f[], *d[];

f = Directory::GetFiles(path, S"*.dll");
d = Directory::GetDirectories(path);

for(i = 0; i < d->Count; i++)
LoadPlugins(d);

for(i = 0; i < f->Count; i++)
{
//Load dll from path stored in f and call its init
}
}

Share this post


Link to post
Share on other sites
Firstly, loose the unmanaged initialization code, you can do it all in managed code.

What you want is the System.Reflection.Assembly class, in particular the LoadFrom static member function. With this you can dynamically load an assembly (ie a managed dll). You can now call GetTypes on the returned assembly to discover all of the types that are available to use in that assembly, so just search for a type that identifies and initializes the plugin (for example, in the past I've searched for sealed classes that implemented an IPlugin interface, check the MSDN docs for how to do this). Once you've found the appropriate types, you can search them for whatever method you want to call (including constructors for creating instances).

Reflection is great [smile]

EDIT: A quick example in C#

Assembly assembly = Assembly.LoadFrom("myplugin.dll");
foreach(Type type in assembly.GetTypes())
{
if(type.IsSealed && type.IsSubclassOf(typeof(IPlugin)))
{
ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
IPlugin plugin = constructor.Invoke(null) as IPlugin;
// register the plugin instance
}
}


There could be a couple of errors as this was off the top of my head, but it should give you the general idea (there won't be that much difference doing it in C++).

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!