LPCSTR marshalling to unmanaged dll

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Lucas

    LPCSTR marshalling to unmanaged dll

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

    #define DllExport __declspec(dlle xport)
    DllExport int WINAPI DllAlcWrite(HAN DLE, 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 strOutgoingBuff er As String
    strOutgoingBuff er ="FOO"
    intReturn = DllAlcWrite(m_H andle, strOutgoingBuff er, 3)

    2.)
    <DllImport("alc sapi.dll")> _
    Private Shared Function DllAlcWrite(ByV al hConn As Long, ByVal
    strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
    End Function
    <VBFixedString( 255)> Private strOutgoingBuff er As String
    strOutgoingBuff er ="FOO"
    intReturn = DllAlcWrite(m_H andle, strOutgoingBuff er, 3)

    3.)
    <DllImport("alc sapi.dll", CharSet:=CharSe t.Ansi,
    CallingConventi on:=CallingConv ention.Cdecl)> _
    Private Shared Function DllAlcWrite(ByV al hConn As Long, ByVal
    strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
    End Function
    <VBFixedString( 255)> Private strOutgoingBuff er As String
    strOutgoingBuff er ="FOO"
    intReturn = DllAlcWrite(m_H andle, strOutgoingBuff er, 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!
  • Mattias Sjögren

    #2
    Re: LPCSTR marshalling to unmanaged dll

    [color=blue]
    >( i know the handle is long, but it is working for other functions in
    >this dll)[/color]

    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

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • John Lucas

      #3
      Re: LPCSTR marshalling to unmanaged dll

      Mattias,

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

      <DllImport("alc sapi.dll", CharSet:=CharSe t.Ansi,
      CallingConventi on:=CallingConv ention.Cdecl)> _
      Private Shared Function DllAlcWrite(ByV al hConn As Long, ByVal
      strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
      End Function
      <DllImport("alc sapi.dll")> _
      Private Shared Function DllInitAlc(ByVa l 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=DllIni tAlc(CInt(lngBo ard), CInt(lngLine), strServer,
      lngIAHex, lngTAHex)
      '(got a non zero handle)


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

      intReturn = DllAlcWrite(m_H andle, strOutgoingBuff er, 3)
      '(exception)





      Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote in message news:<eOvr0yePE HA.2820@TK2MSFT NGP10.phx.gbl>. ..[color=blue][color=green]
      > >( i know the handle is long, but it is working for other functions in
      > >this dll)[/color]
      >
      > 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[/color]

      Comment

      • Mattias Sjögren

        #4
        Re: LPCSTR marshalling to unmanaged dll

        John,
        [color=blue]
        ><DllImport("al csapi.dll", CharSet:=CharSe t.Ansi,
        >CallingConvent ion:=CallingCon vention.Cdecl)> _
        >Private Shared Function DllAlcWrite(ByV al hConn As Long, ByVal
        >strGWOutgoin g As String, ByVal intStringLength As Integer) As Integer
        >End Function[/color]

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



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • John Lucas

          #5
          Re: LPCSTR marshalling to unmanaged dll

          I changed it to intPtr and stilll have the same problem.


          Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote in message news:<eRg314oPE HA.556@tk2msftn gp13.phx.gbl>.. .[color=blue]
          > John,
          >[color=green]
          > ><DllImport("al csapi.dll", CharSet:=CharSe t.Ansi,
          > >CallingConvent ion:=CallingCon vention.Cdecl)> _
          > >Private Shared Function DllAlcWrite(ByV al hConn As Long, ByVal
          > >strGWOutgoin g As String, ByVal intStringLength As Integer) As Integer
          > >End Function[/color]
          >
          > Did you change anything? hConn is still declared as Long.
          >
          >
          >
          > Mattias[/color]

          Comment

          • Tom Shelton

            #6
            Re: LPCSTR marshalling to unmanaged dll

            On 19 May 2004 14:48:51 -0700, John Lucas wrote:
            [color=blue]
            > I wish to call an unmanaged function from vb delacred as so:
            >
            > #define DllExport __declspec(dlle xport)
            > DllExport int WINAPI DllAlcWrite(HAN DLE, 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[/color]


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

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

            HTH
            --
            Tom Shelton [MVP]

            Comment

            • John Lucas

              #7
              Re: LPCSTR marshalling to unmanaged dll

              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_S abreHandle, buffer, buffer.Length)


              THANK YOU.
              -john

              Tom Shelton <tom@YOUKNOWTHE DRILLmtogden.co m> wrote in message news:<1294ovn4y 9q94$.ldmzatpna 8tc$.dlg@40tude .net>...[color=blue]
              > On 19 May 2004 14:48:51 -0700, John Lucas wrote:
              >[color=green]
              > > I wish to call an unmanaged function from vb delacred as so:
              > >
              > > #define DllExport __declspec(dlle xport)
              > > DllExport int WINAPI DllAlcWrite(HAN DLE, 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[/color]
              >
              >
              > Private Declare Ansi Function DllAlcWrite Lib "alcsapi.dl l" _
              > (ByVal hConn As IntPtr, _
              > ByVal strGWOutgoing As String, _
              > ByVal intStringLength As Integer) As Integer
              >
              > ' air code :)
              > Dim buffer As String = new String(vbNullCh ar, 255)
              > DllAlcWrite (hConn, buffer, buffer.Length)
              >
              > HTH[/color]

              Comment

              Working...