DLL Call in VB.NET

  • Thread starter Thread starter Maximilian Hofer
  • Start date Start date
M

Maximilian Hofer

Hello NG,


I Have follow Declaration in my VB6 Code:

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Integer) As Integer

This is the Code where i call this Function:

Dim Length as Integer
Dim RecvData as String * 1021
Length = EPS48_Receive(RecvData, 1021)

Here I get a Value for RecvData. But when I use the same in VB.NET:

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Int16) As Int16



Dim RecvData As String



Dim Length As Int16

Length = bkeps48.EPS48_Receive(RecvData, 1021)



.... i get 'nothing' for RecvData.



What is wrang in my Code?



Thanks,



Max
 
I Have follow Declaration in my VB6 Code:
Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Integer) As Integer

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Int16) As Int16

What is wrang in my Code?


You need to marshall the string (VB.NET doesn't do it for you like VB6
did)...... Unfortunatly I didn't do it in a while and I don't have any code
example at hand so I can't help more than this (the syntax is a bit cryptic
the first time you use it).

Others will jump to your rescue I'm sure :) Meanwhile a seach for mashaling
and strings on google should turn up interesting links

Alex.
 
Maximilian Hofer said:
Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As
String, ByVal MaxLength As Int16) As Int16



Dim RecvData As String



Dim Length As Int16

Length = bkeps48.EPS48_Receive(RecvData, 1021)



... i get 'nothing' for RecvData.



What is wrang in my Code?

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmarshalingstrings.asp

See the "as Result" row.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
* "Maximilian Hofer said:
I Have follow Declaration in my VB6 Code:

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Integer) As Integer

This is the Code where i call this Function:

Dim Length as Integer
Dim RecvData as String * 1021
Length = EPS48_Receive(RecvData, 1021)

Here I get a Value for RecvData. But when I use the same in VB.NET:

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Int16) As Int16



Dim RecvData As String



Dim Length As Int16

Length = bkeps48.EPS48_Receive(RecvData, 1021)



... i get 'nothing' for RecvData.

Try this:

\\\
Dim MaxLength As Int16 = 1021
Dim RecvData As String = Strings.Space(MaxLength)
Dim Length As Int16 = EPS48_Receive(RecvData, MaxLength)
MsgBox(Left(RecvData, Length)
///
 
* "Sin said:
You need to marshall the string (VB.NET doesn't do it for you like VB6

When using VB.NET's 'Declare', I thin this should not make any
difference -- even VB.NET will do marshalling for the string.
 

Because using a StringBuilder to recieve data from external api calls is
lot more effiecient. I also think it is a good habbit to get into,
because it won't work in C# unless you use a stirngbuilder. So, if you
ever have to venture to the other side you won't be caught unaware :)
 
* Tom Shelton said:
Because using a StringBuilder to recieve data from external api calls is
lot more effiecient.

ACK, but that's IMO not the point. Fact is, that the code IMO should
work with 'As String' too.
I also think it is a good habbit to get into,
because it won't work in C# unless you use a stirngbuilder.

I wouldn't change my mind because of C#, but for reasons of efficiency.
Nevertheless, the VB.NET approach is IMO much more "high-level" and
that's why I defend the 'As String' solution ;-).
 
ACK, but that's IMO not the point. Fact is, that the code IMO should
work with 'As String' too.

It should in VB.NET. I haven't really looke at the original post. I
should do that.
I wouldn't change my mind because of C#, but for reasons of efficiency.
Nevertheless, the VB.NET approach is IMO much more "high-level" and
that's why I defend the 'As String' solution ;-).

That's ok. I tend to do it because that is the way it has to be done in
C#. And that's what I use mostly :)
 
Hello NG,


I Have follow Declaration in my VB6 Code:

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Integer) As Integer

This is the Code where i call this Function:

Dim Length as Integer
Dim RecvData as String * 1021
Length = EPS48_Receive(RecvData, 1021)

Here I get a Value for RecvData. But when I use the same in VB.NET:

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As String,
ByVal MaxLength As Int16) As Int16



Dim RecvData As String



Dim Length As Int16

Length = bkeps48.EPS48_Receive(RecvData, 1021)



... i get 'nothing' for RecvData.



What is wrang in my Code?



Thanks,



Max

Try it like this:

Dim RecvData As New String(vbNullChar, 1021)
Dim Length As Int16 = bkeps48.EPS48_Recieve(RecvData, )

Personally, I would use a stringbuilder in the declaration:

Declare Function EPS48_Receive Lib "bkeps48.dll"
(ByVal RecvData As System.Text.StringBuilder,
ByVal MaxLength As Int16) As Int16

Dim RecvData As New System.Text.StringBuilder(1021)
Dim Length As Int16 = bkeps48.EPS48_Recieve(RecvData, RecvData.Capacity)
Dim result As String = RecvData.ToString()
 
The solution for my Problem was the String Builder:

Declare Function EPS48_Receive Lib "bkeps48.dll" (ByVal RecvData As
System.Text.StringBuilder, ByVal MaxLength As Int16) As Int16

I call this Function:



Dim RecvData As New System.Text.StringBuilder(1021)

Dim Length As Int16

Length = bkeps48.EPS48_Receive(RecvData, 1021)

lbl_r_receive.Text = CStr(Length)

Empfangen.Text = RecvData.ToString



Thanks for your Help.



mfg



Max
 
Back
Top