5

I am writting a plugin for a program, and I want to put my code inside a DLL so I can share the plugin freely without exposing (giving away) my code.

Here is the basic structure i have access to :

using System;
public class Plugin
{
    public void Initialize()
    {
        //do stuff here
        doWork();
    }
}

Then i just reference the .cs file my code is at and the program "eats" up this Plugin. Now, i have put several logic in there, consisting mostly of functions that arent tied directly to "Initialize()", only on the doWork() function, that starts the whole system.

Now, I want to put all my code inside a DLL, and then just call from inside Initialize(), myDll.doWork() (or something like that).

PS: This dll would ofc be a compiled C# library (could it be called dynamic assembly import? it wouldnt really be dynamic since it would be compiled pre-execution anyways, right?)

PS2: This way I can also add custom resources like forms, images and such without much difficulty right?

PS3: Is there a free tool for protecting the code inside such DLL? (ie protect from beeing easily re engineered)

Thanks in advance =)

starblue
  • 55,348
  • 14
  • 97
  • 151
Joao Carlos
  • 749
  • 1
  • 11
  • 32

4 Answers4

7

Found exactly what I was looking for, here it is:

using System.Reflection;
using System.IO;

try
{
    Assembly a = null;

    a = Assembly.LoadFrom(Application.StartupPath startupPath + "MyAssembly.dll"); 

    Type classType = a.GetType("MyNamespace.MyClass");
    object obj = Activator.CreateInstance(classType);
    MethodInfo mi = classType.GetMethod("MyMethod");

    mi.Invoke(obj, null);
}
catch (Exception e)
{                 
    AddLog(e.Message);            
}
John Odom
  • 1,189
  • 2
  • 20
  • 35
Joao Carlos
  • 749
  • 1
  • 11
  • 32
  • 2
    If you define a interface in your application and have `MyNamespace.MyClass` implement it, you can cast `obj` to that interface and invoke methods without reflection. – dtb Sep 12 '09 at 03:41
  • Ah yes...the interface is exactly the way I need to do this. I've created a class in a separate .dll. I'm instancing this through the code above, but, of course, 'obj' isn't giving me the properties or functions within that class. I don't mind the new library referencing the an interface in the .exe, but the .exe will never have any clue as to what 'plug-ins' it would have to reference. Awesome!!! – IAbstract Jan 24 '10 at 04:24
3

The best option would be to use a framework for creating extensible applications:

If you want to do it manually, you can use the Assembly.Load*() methods to load an assembly at runtime. You can search the assembly for types that implement a specific interface and then create instances using Activator.CreateInstance. The assemblies can be compiled separately, you just have to reference an assembly that contains the interface shared by application and plugin.

For obfuscation, there are a couple of obfuscation tools available.

Community
  • 1
  • 1
dtb
  • 213,145
  • 36
  • 401
  • 431
  • I need one that works with Framework 4 already, any suggestions? – Joao Carlos Sep 12 '09 at 02:31
  • MEF is included in .NET 4.0. For versions before 4.0 you need to ship it with your application. Mono.Addins should work with .NET 4.0 as well, but is obviously not included. – dtb Sep 12 '09 at 03:43
1

Use the static Assembly.Load() / Assembly.LoadFile() / Assembly.LoadFrom() methods to dynamically load assemblies.

codymanix
  • 28,510
  • 21
  • 92
  • 151
0

FYI, remember to use a tool like Red-Gate's .Net Reflector to inspect your DLL to make sure it is properly obfuscated. This free, visual tool lets you see the DLL as your users will see it so you'll know if your code is exposed

alt text http://img149.imageshack.us/img149/2025/screenshotfullscreen.gif

.Net comes with Dotfuscator Community Edition which can be used to obfuscate your code. No coding is required, see their user guide to learn your way around the GUI if you need help.

Michael La Voie
  • 27,772
  • 14
  • 72
  • 92
  • How do I obfuscate it in the first place? – Joao Carlos Sep 12 '09 at 00:53
  • Found some documentation, but, cant seem to find one that works with Framework 4. Any suggestions? – Joao Carlos Sep 12 '09 at 02:30
  • Since .NET 4 is beta software (without any form of go live licence) you won't find much support. It currently only exists for testing (and learning). – Richard Sep 12 '09 at 09:25
  • The free version of Dotfuscator that ships with Visual Studio 2010 will obfuscate .NET 4.0 assemblies as will any commercial version of Dotfuscator from 4.5.12000 on up. – Joe Kuemerle Sep 14 '09 at 16:18