Results 1 to 19 of 19

Thread: Calling a DLL from VB.NET

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Calling a DLL from VB.NET

    I was wondering how to call a DLL from VB.NET.

    Also, I am making a DLL with some C code and I need to be able to pass the following variables:

    int var1
    char *var2[]

    How do I pass var2 with VB since it has no pointers?

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Calling a DLL from VB.NET

    Reference the dll in the project; then
    Import the dll into your code using the Imports key word; then
    Call your function by the name of the dll followed by the function name; as in:

    MyDLLName.MyFunctionName

    Edit: Actually, you don't have to Import the library name if you don't want to.
    You just might want to if the path to the function is long.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    Thanks man. Do you have a code snippet of that being done and where I put the code?

    Also, do you know anything about my pointer question?

  4. #4
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Post Re: Calling a DLL from VB.NET

    To Add the DLL as a reference to the project...
    Using Menus:
    Project-Add Reference-(Browse Tab & Find the DLL)-Click OK.

    From that point you can use the Imports statement (just look it up in the online help it's real easy).
    But that's not a necessary step.
    You could go straight to just using the functions within the dll as I mentioned before just as soon as the dll has been added as a reference.

    For the second part of the problem, once you have referenced the dll you get to use the dll just like any other .NET library function--you just call the function and pass the required variables.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    Thanks again, but my question regarding passing the variable is how to pass a variable that is a pointer array since VB doesn't support pointers.

    In C I would do this:

    char *stuff[numitems]; // or char **stuff;
    int count=0;

    stuff[count++]="blah";
    stuff[count++]="blah";
    stuff[count++]="blah";
    stuff[count++]="blah";
    stuff[count++]="blah";

    MyDLLCall(count, &stuff);

    How would I do that same thing in VB?
    Last edited by Redneck; Nov 9th, 2007 at 08:11 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    Ok, I am having problems adding my DLL.

    "A reference to 'C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\bin\Release\gifdll.dll" could not be added. Please make sure tha tthe file is accessible, and that it is a valid assembly or COM component.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    Here is my code:

    Header File
    Code:
    DLLIMPORT void AddArgument(int count, char *arg);
    DLLIMPORT void DoGif(int count);
    Source File
    Code:
    char *strsave(char *s);
    
    char *argstack[256];
    
    char *strsave(char *s)
    {
        char *p;// =  (char*)calloc(strlen(s),sizeof(char));
        int l;
    
        l = strlen (s);
        if ((p = (char*)calloc(l, 1)) != NULL)
            strcpy(p, s);
        return (p);
    }
    
    DLLIMPORT void DoGif(int count)
    {
        int i;
        
        gifmain(count, (char**)&argstack);
        
        for(i=0; i<count; i++)
        {
            free(argstack[i]);
        }
    }
    
    DLLIMPORT void AddArgument(int count, char *arg)
    {
        argstack[count]=strsave(arg);
    }
    
    
    BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                           DWORD reason        /* Reason this function is being called. */ ,
                           LPVOID reserved     /* Not used. */ )
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Calling a DLL from VB.NET

    now Im not totally sure, but I think you can call the DLLs function just like you'd call an API function:

    VB.Net Code:
    1. <System.Runtime.InteropServices.DllImport("DllHere")> _
    2.     Private Shared Sub AddArgumentByVal count As Integer, ByRef arg As Char)
    3.     End Sub
    Give that a try...
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    But I am having problem adding the dll to my project.

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Calling a DLL from VB.NET

    Quote Originally Posted by Redneck
    But I am having problem adding the dll to my project.
    You wouldnt add the Dll into your project at all if you where using my method.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    ok I will try that.

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    I tried that and this is the error I got. I put it at the top of my form code.


    Error 1 Attribute specifier is not a complete statement. Use a line continuation to apply the attribute to the following statement. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 1 52 dlltest
    Error 2 Statement is not valid in a namespace. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 2 1 dlltest
    Error 3 Statement cannot appear within a method body. End of method assumed. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 3 1 dlltest
    Error 4 Statement is not valid in a namespace. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 3 1 dlltest

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    This is the code I used


    Code:
    <System.Runtime.InteropServices.DllImport("gifdll")>
    Private Shared Sub AddArgument(ByVal argc As Integer, ByRef arg As Char)
    Private Shared Sub DoGif(ByVal argc As Integer)
    End Sub
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Calling a DLL from VB.NET

    You missed the line continuator after the atribute....
    Code:
    <System.Runtime.InteropServices.DllImport("gifdll")> _
    Private Shared Sub AddArgument(ByVal argc As Integer, ByRef arg As Char)
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    Still get these errors...

    Error 1 Statement is not valid in a namespace. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 1 1 dlltest
    Error 2 Statement cannot appear within a method body. End of method assumed. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 3 1 dlltest
    Error 3 Statement is not valid in a namespace. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 3 1 dlltest

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    I moved it and now I get:

    Error 1 Statement cannot appear within a method body. End of method assumed. C:\Documents and Settings\Damon\Desktop\dll test\dlltest\dlltest\Form1.vb 5 5 dlltest

  17. #17

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    Alright, I got it working.

    Do I need to put:
    <System.Runtime.InteropServices.DllImport("gifdll")> _
    Before every function I am declaring followed by "EndSub"?

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    For some reason the code isn't doing what it is supposed to...The string isn't getting passed to the DLL correctly. Is my declaration incorrect? It's defined as a Char, but in my code it is a char pointer.
    Last edited by Redneck; Nov 9th, 2007 at 10:45 PM.

  19. #19

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    43

    Re: Calling a DLL from VB.NET

    I figured it out.

    I needed to declare it as ByVal as string not ByRef as Char

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