views:

46

answers:

3

I am using this code, I got it to work fine with any C# assembly that allows it to be ran from memory. Is there anyway I could get it to work with VB.net?

    private static void RunFromMemory()
    {
        try
        {
            byte[] bytes;
            using (WebClient client = new WebClient())
            {
                bytes = client.DownloadData(new Uri("http://example.com/program.exe"));
            }
            Assembly exeAssembly = Assembly.Load(bytes);
            exeAssembly.EntryPoint.Invoke(null, null);
        }
        catch
        {

        }
    }

The error I receive is "Parameter count mismatch"

A: 

Please note, I've not tried this out, so this answer is just a theory.

You're invoking it with the parameter variable set to null and it's complaining about parameter mismatch so I'd suggest trying to send in a parameter list instead of the 'null', so try something like:

object[] params = new object[0];
exeAssembly.EntryPoint.Invoke(null, params);

Or maybe create at least one parameter as:

object[] params = new object[1];
object[0] = "Param1";
exeAssembly.EntryPoint.Invoke(null, params);

If this works, it might be that your C# assemblies have their main methods declared without any parameters but the VB.Net assemblies have their Main methods declared to expect parameters.

ho1
A: 

I found this answer on another site:

http://www.dotnet247.com/247reference/msgs/32/165000.aspx

The problem is that the 2nd invoke param is the arguments which is an object array. Each item in that array is another argument.

main(string[]) has one argument of type string[].

You need to do this:

string[] str; /// Fill str

object[] obj = new object[1]; obj[0] =
str;

Invoke(null, obj);
David Stratton
I did basically what you said and now my VB.net Application opens but crashes whenever you click a button.
xZerox
A: 

Again I learnt something new on this site. Thanks.

AaronAdamic