-
Nov 9th, 2007, 05:51 PM
#1
Thread Starter
Member
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?
-
Nov 9th, 2007, 06:08 PM
#2
Frenzied Member
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.
-
Nov 9th, 2007, 06:49 PM
#3
Thread Starter
Member
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?
-
Nov 9th, 2007, 06:59 PM
#4
Frenzied Member
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.
-
Nov 9th, 2007, 07:45 PM
#5
Thread Starter
Member
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.
-
Nov 9th, 2007, 08:38 PM
#6
Thread Starter
Member
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.
-
Nov 9th, 2007, 08:41 PM
#7
Thread Starter
Member
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;
}
-
Nov 9th, 2007, 08:49 PM
#8
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:
<System.Runtime.InteropServices.DllImport("DllHere")> _
Private Shared Sub AddArgumentByVal count As Integer, ByRef arg As Char)
End Sub
Give that a try...
-
Nov 9th, 2007, 08:54 PM
#9
Thread Starter
Member
Re: Calling a DLL from VB.NET
But I am having problem adding the dll to my project.
-
Nov 9th, 2007, 08:55 PM
#10
Re: Calling a DLL from VB.NET
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.
-
Nov 9th, 2007, 09:26 PM
#11
Thread Starter
Member
Re: Calling a DLL from VB.NET
-
Nov 9th, 2007, 09:32 PM
#12
Thread Starter
Member
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
-
Nov 9th, 2007, 09:36 PM
#13
Thread Starter
Member
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
-
Nov 9th, 2007, 09:41 PM
#14
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
-
Nov 9th, 2007, 09:54 PM
#15
Thread Starter
Member
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
-
Nov 9th, 2007, 09:55 PM
#16
Thread Starter
Member
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
-
Nov 9th, 2007, 10:05 PM
#17
Thread Starter
Member
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"?
-
Nov 9th, 2007, 10:35 PM
#18
Thread Starter
Member
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.
-
Nov 9th, 2007, 10:59 PM
#19
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|