Results 1 to 3 of 3

Thread: Public vs. Public Shared (Function/Sub) When Invoking DLL...

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    11

    Question Public vs. Public Shared (Function/Sub) When Invoking DLL...

    Hey guys, I have a DLL I'm using in a project, it's in the resources of the application. I'm getting errors when trying to Invoke it, that's why I'm here posting this thread. Does the Function/Sub I'm trying to call, have to be Shared or does it have to be just Public?!

    I believe from the error I'm getting, that is has to be Shared. Reason I say that is the error says somthing along the lines of....

    An exception has been thrown by the target of invocation, System.Reflection.Target.
    If it helps, I'm also trying to load the DLL from resources like so....

    Code:
    Assembly.Load(My.Resources.ClsLib).GetType("Clib.Run").GetMethod("Execute").Invoke _
    (Nothing, New Object() {Byte_Arg, String_Arg})
    Anyway, thanks for any future help guys, peace....
    Last edited by xblshun; Apr 27th, 2013 at 01:43 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,630

    Re: Public vs. Public Shared (Function/Sub) When Invoking DLL...

    A method, whether in a dll or not, has to be part of something. If it is part of a class, then it can't be called without an instance of the class unless it is Shared. If the function is part of a module, then it might appear that it doesn't have to be Shared, but that's because the Shared is hidden, since all members of a module are quietly turned into Shared members by the compilers. Therefore, if the method you are tying to invoke is a member of a class, then you either need to have an instance of the class, or the method has to be Shared. If the method is in a module, then it is already shared.
    My usual boring signature: Nothing

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Public vs. Public Shared (Function/Sub) When Invoking DLL...

    If it's not a shared method then you first need to create an instance of the class before calling one of the methods.
    Code:
    Dim a As Assembly = Assembly.Load(My.Resources.ClsLib)
    Dim o As Object = a.CreateInstance("Clib.Run")
    o.GetType().GetMethod("Execute").Invoke(o,  New Object() {Byte_Arg, String_Arg})
    However this is a really slow and costly process, why don't you just want to have the assembly distributed as file and have a reference to it in your project? It's a lot faster and safer to do so.
    Last edited by Joacim Andersson; Apr 27th, 2013 at 02:23 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width