LPCSTR marshalling to unmanaged dll

J

John Lucas

I wish to call an unmanaged function from vb delacred as so:

#define DllExport __declspec(dllexport)
DllExport int WINAPI DllAlcWrite(HANDLE, LPCSTR, int);


I have tried:

1.)
Private Declare Ansi Function DllAlcWrite Lib "alcsapi" (ByVal hConn
As Long, ByVal strGWOutgoing As String, ByVal intStringLength As
Integer) As Integer

<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"
intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)

2.)
<DllImport("alcsapi.dll")> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function
<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"
intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)

3.)
<DllImport("alcsapi.dll", CharSet:=CharSet.Ansi,
CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function
<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"
intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)

( i know the handle is long, but it is working for other functions in
this dll)

I have also tried setting the string length (3rd parm)to 255
I am able to call other functions in this dll ok
=================================

All of them generate the NullReference exception. Is there a way to
properly marshal LPCSTR?

Help!
 
M

Mattias Sjögren

( i know the handle is long, but it is working for other functions in
this dll)

Did you mean to say that it isn't a Long? Because it isn't, and you
should replace it with IntPtr or Integer.



Mattias
 
J

John Lucas

Mattias,

It doesn't seem to matter, I still get a nullreference exception with
this:

<DllImport("alcsapi.dll", CharSet:=CharSet.Ansi,
CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function
<DllImport("alcsapi.dll")> _
Private Shared Function DllInitAlc(ByVal Board As Integer, ByVal
lngLine As Integer, _
ByVal GWName As String, ByVal IA As Integer, ByVal TA As Integer) As
IntPtr
End Function

Dim m_handle as IntPtr
m_Handle=DllInitAlc(CInt(lngBoard), CInt(lngLine), strServer,
lngIAHex, lngTAHex)
'(got a non zero handle)


<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"

intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)
'(exception)
 
M

Mattias Sjögren

John,
<DllImport("alcsapi.dll", CharSet:=CharSet.Ansi,
CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function

Did you change anything? hConn is still declared as Long.



Mattias
 
T

Tom Shelton

I wish to call an unmanaged function from vb delacred as so:

#define DllExport __declspec(dllexport)
DllExport int WINAPI DllAlcWrite(HANDLE, LPCSTR, int);


I have tried:

1.)
Private Declare Ansi Function DllAlcWrite Lib "alcsapi" (ByVal hConn
As Long, ByVal strGWOutgoing As String, ByVal intStringLength As
Integer) As Integer


Private Declare Ansi Function DllAlcWrite Lib "alcsapi.dll" _
(ByVal hConn As IntPtr, _
ByVal strGWOutgoing As String, _
ByVal intStringLength As Integer) As Integer

' air code :)
Dim buffer As String = new String(vbNullChar, 255)
DllAlcWrite (hConn, buffer, buffer.Length)

HTH
 
J

John Lucas

Tom,

That was it! Null termination character.

Here's what I ended up with.

Dim buffer As String
buffer = strSendThis
buffer.PadRight(255, vbNullChar)
intReturn = DllAlcWrite(m_SabreHandle, buffer, buffer.Length)


THANK YOU.
-john
 

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