W32 Interop Advice Needed

J

John Slagle

I have an old Win32 Dll that want to pass a string back to my app
via one of the parameters (not the return value).

I am seeing one way to do this is to dereference a StringBuilder
instance. Is this possible in VB.NET?

Another possible way might be
<MarshalAs (UnmanagedType.VBByRefStr)>

Does anyone have any experience with this?

The documentation for the DLL includes a VB6 decleration
containing keyword 'ByVal' for the string parameter, so I assume
its not passing a BSTR, but rather an address for a null terminated
series of ASCII characters.

I cannot locate an example within the DLL's documentation to
verify that a string has to be presized within VB6 prior to calling
the DLL.

Any insight appreciated.

-John Slagle
-Denver, CO
 
D

Dominique Vandensteen

can't you simply use byref?

Public Declare Sub TheFuncation Lib "thedll.dll" (ByRef theString as
String)


dominique
 
J

John Slagle

can't you simply use byref?

I was stuck in a VB6 mindset. I kept taking out the ByVal and VS.NET kept
putting it back in.

Doing this:

Declare Function error4describe Lib _
"C4FOX.DLL" Alias "error4describeVB" ( _
ByVal c4%, _
ByVal errCode As Int16, _
ByVal extraInfo%, _
ByRef desc1$, _
<MarshalAs(UnmanagedType.LPStr)> ByRef desc2 As String, _
<MarshalAs(UnmanagedType.VBByRefStr)> ByVal desc3 As String _
) As Int16

I Get This (from IL dump):

int16 error4describe(
int32 c4,
int16 errCode,
int32 extraInfo,
string& marshal( ansi bstr) desc1,
string& marshal( lpstr) desc2,
string marshal( byvalstr) desc3)
cil managed preservesig

Originally, I didn't think the BStr was what I wanted ('BYVAL' in the VB6
declaration). But the 'VB' suffix in the Alias hints otherwise.

It does appear as though I can do whatever I need. I just have to
find the right combination.

Thanks.
 
T

Tom Shelton

I have an old Win32 Dll that want to pass a string back to my app
via one of the parameters (not the return value).

I am seeing one way to do this is to dereference a StringBuilder
instance. Is this possible in VB.NET?

Another possible way might be
<MarshalAs (UnmanagedType.VBByRefStr)>

Does anyone have any experience with this?

The documentation for the DLL includes a VB6 decleration
containing keyword 'ByVal' for the string parameter, so I assume
its not passing a BSTR, but rather an address for a null terminated
series of ASCII characters.

I cannot locate an example within the DLL's documentation to
verify that a string has to be presized within VB6 prior to calling
the DLL.

Any insight appreciated.

-John Slagle
-Denver, CO

Pass a System.Text.StringBuilder ByVal...

Dim sb As New StringBuilder(YourLengthHere)
YourFunction(sb)
Return sb.ToString()

HTH
 

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