[Help] Reading data from a DLL structure in C

E

eddom

I am trying to retrieve data from a function structured in C by using
VB.NET, can anyone help me to understand this? I am totally new to this
API or MarshalA.

Structure of the function:
int GetDLLVersion(char *data, int datasize)

the *data will return a 7 character word, but how can i store it into
the variable of VB.NET?

i tried this:
Dim VerNum as String
<DLLImport ("Vertx.dll")> Public Shared Function GetDLLVersion (ByRef
VerNum as String, ByVal data as Integer) as Integer
End Function

GetDLLVersion(VerNum, 10)

When ever i try to call this function, a error message prompt out which
tell me the problem of object, so i replace the code with:

Dim VerNum as Char
<DLLImport ("Vertx.dll")> Public Shared Function GetDLLVersion (ByRef
VerNum as Char, ByVal data as Integer) as Integer
End Function

GetDLLVersion(VerNum, 10)

it work, but only one character was returned, since char type only
store up to one character, this is not what i want, can somebody help
me to deal with this problem?
 
M

Mattias Sjögren

Dim VerNum as String
<DLLImport ("Vertx.dll")> Public Shared Function GetDLLVersion (ByRef
VerNum as String, ByVal data as Integer) as Integer
End Function

GetDLLVersion(VerNum, 10)

The string should be passed ByVal, and I believe it actually has to be
big enough to hold the data before the call (ie at least 10
characters). You can also use a StringBuilder as the parameter type.


Mattias
 
E

eddom

Ya, i had done some research in the net and i found that most ppl
provide 2 type of solution. One is using IntPtr to store the pointed
address, the other one is use the string builder. Unfortunely, i cant
get the full explanation on the usage of string builder, without any
reference, it is very hard for me to figure out the way to use the
string builder. Here is what i can get from net to change my code,
please guide me to correct it. Thanks very much

Imports System
Imports System.Runtime.InteropServices
Imports System.Text

Inherits System.Windows.Forms.Form
Public Ptr As IntPtr
<MarshalAs(UnmanagedType.LPStr)> Public VerNum As String
<DllImport("Vertx.dll")> Public Shared Function GetDLLVersion(ByVal
VerNum As String, ByVal DataSize As Integer) As Integer
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
GetDLLVersion(VerNum, 10)
MsgBox(VerNum)
End Sub

This code return an error: "object Reference not set to an instance of
object"
how can i solve it??
Really need the help!
 
M

Mattias Sjögren

This code return an error: "object Reference not set to an instance of
object"
how can i solve it??

<DllImport("Vertx.dll")> Public Shared Function GetDLLVersion(ByVal
VerNum As StringBuilder, ByVal DataSize As Integer) As Integer
End Function

....

Dim verNum As New StringBuilder(10)
GetDLLVersion(VerNum, verNum.Capacity)
MsgBox(VerNum.ToString())


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top