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?
Printable View
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?
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.
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?
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.
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?
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.
Here is my code:
Header File
Source FileCode:DLLIMPORT void AddArgument(int count, char *arg);
DLLIMPORT void DoGif(int count);
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;
}
now Im not totally sure, but I think you can call the DLLs function just like you'd call an API function:
Give that a try...VB.Net Code:
<System.Runtime.InteropServices.DllImport("DllHere")> _ Private Shared Sub AddArgumentByVal count As Integer, ByRef arg As Char) End Sub
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.Quote:
Originally Posted by Redneck
ok I will try that.
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
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
You missed the line continuator after the atribute....
-tgCode:<System.Runtime.InteropServices.DllImport("gifdll")> _
Private Shared Sub AddArgument(ByVal argc As Integer, ByRef arg As Char)
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
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
Alright, I got it working.
Do I need to put:
<System.Runtime.InteropServices.DllImport("gifdll")> _
Before every function I am declaring followed by "EndSub"?
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.
I figured it out.
I needed to declare it as ByVal as string not ByRef as Char