none
VB.Net 調用 DLL 的問題 RRS feed

  • 問題

  •  

    請問在 VB.Net 下可以正常的調用 _cdecl 宣告的 DLL 嗎?

     

    因為 VB6.0 只能調用 _stdcall 宣告的 DLL

     

    DLL 檔是用 VC 開發的

     

    謝謝

     

     

     

     

     

    2008年11月12日 上午 04:27

解答

  • CallingConvention := CallingConvention.Cdecl

     

     

    下列範例示範如何套用 Cdecl 呼叫慣例,因為呼叫端清除了堆疊,所以您必須使用這個慣例。

    Code Snippet

    Imports System Imports Microsoft.VisualBasic
    Imports System.Runtime.InteropServices

    Public Class LibWrap
    ' Visual Basic does not support varargs, so all arguments must be
    ' explicitly defined. CallingConvention.Cdecl must be used since the stack
    ' is cleaned up by the caller.
    ' int printf( const char *format [, argument]... )

    <DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
    Overloads Shared Function printf ( _
        format As String, i As Integer, d As Double) As Integer
    End Function

    <DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
    Overloads Shared Function printf ( _
        format As String, i As Integer, s As String) As Integer
    End Function
    End Class 'LibWrap

    Public Class App
        Public Shared Sub Main()
            LibWrap.printf(ControlChars.CrLf + "Print params: %i %f", 99, _
                           99.99)
            LibWrap.printf(ControlChars.CrLf + "Print params: %i %s", 99, _
                           "abcd")
        End Sub 'Main
    End Class 'App

     

     

    2008年11月12日 上午 06:24

所有回覆

  • CallingConvention := CallingConvention.Cdecl

     

     

    下列範例示範如何套用 Cdecl 呼叫慣例,因為呼叫端清除了堆疊,所以您必須使用這個慣例。

    Code Snippet

    Imports System Imports Microsoft.VisualBasic
    Imports System.Runtime.InteropServices

    Public Class LibWrap
    ' Visual Basic does not support varargs, so all arguments must be
    ' explicitly defined. CallingConvention.Cdecl must be used since the stack
    ' is cleaned up by the caller.
    ' int printf( const char *format [, argument]... )

    <DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
    Overloads Shared Function printf ( _
        format As String, i As Integer, d As Double) As Integer
    End Function

    <DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
    Overloads Shared Function printf ( _
        format As String, i As Integer, s As String) As Integer
    End Function
    End Class 'LibWrap

    Public Class App
        Public Shared Sub Main()
            LibWrap.printf(ControlChars.CrLf + "Print params: %i %f", 99, _
                           99.99)
            LibWrap.printf(ControlChars.CrLf + "Print params: %i %s", 99, _
                           "abcd")
        End Sub 'Main
    End Class 'App

     

     

    2008年11月12日 上午 06:24
  • 程式可以順利的執行

     

    非常感謝你的幫忙

     

     

     

    2008年11月12日 上午 11:22