Utiliser DLL C en VB

ncyshaolin Messages postés 6 Date d'inscription mercredi 21 novembre 2007 Statut Membre Dernière intervention 4 janvier 2008 - 21 nov. 2007 à 15:30
lesdis Messages postés 403 Date d'inscription mercredi 19 avril 2006 Statut Membre Dernière intervention 7 août 2020 - 21 nov. 2007 à 17:21
Bonjour,


Je pense que ce probleme a deja etait aborde plusieurs fois mais apres
avoir cherche partout je ne trouve toujours pas la reponse a mon
probleme. Je souhaite utiliser un dll detaillant une methode de calcul
pour l implementer dans un programme en VB. Je cherche donc a utiliser
les fonctions qui se trouvent dans le dll. Le probleme est la
correspondance des types.

Je souhaite pour l instant simplement adapter un simple exemple d utilisation du dll en C dans un environnement VB.

L exemple est tres simple en C :

<!-- BEGIN TEMPLATE: bbcode_code -->

Code :
<!--[if !IE]><--><!----><!--[endif]--><!--[if IE]>
<![endif]-->void* p2p ;
double att[27] ;
// initialize the PointToPoint module
p2p = P2P_Create() ;
if(p2p == NULL)
{
... // error handling : could not create a P2P structure
}
// setup the cross-section
// remember : the origin = point on the road surface below the source
P2P_Clear(p2p) ; // source foot point (0,0)
P2P_AddSegment (p2p, 10.0, 0.0, 7) ; // top of embankment
P2P_AddSegment (p2p, 14.0, -3.0, 5) ; // bottom of embankment
P2P_AddSegment (p2p, 50.0, -3.0, 4) ; // receiver foot point
// set the source and receiver heights relative to the local terrain
// heights at the first and last points of the cross-section
P2P_SetSourceHeight (p2p, 0.3, 0.0) ;
P2P_SetReceiverHeight (p2p, 5.0, 0.0) ;
// calculate the excess attenuation and return the result in “att”
P2P_GetResult (p2p, att) ;
// do something with the excess attenuation values...

<!-- END TEMPLATE: bbcode_code -->Mon code VB est le suivant :

<!-- BEGIN TEMPLATE: bbcode_code -->

Code :
<!--[if !IE]><--><!----><!--[endif]--><!--[if IE]>
<![endif]-->Private Declare Function Create Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_Create"() As Integer
Private Declare Function Clear Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_Clear"(ByRef p2p_struct As Integer) As Integer
Private Declare Function AddSegment Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_AddSegment"(ByRef p2p_struct As Integer, ByVal D As Double, ByVal Z As Double, ByVal impedanceModel As Integer) As Integer
Private Declare Function SetSourceHeight Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_SetSourceHeight"(ByRef p2p_struct As Integer, ByVal hSource As Double, ByVal delta_hs As Double) As Integer
Private Declare Function SetReceiverHeight Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_SetReceiverHeight"(ByRef p2p_struct As Integer, ByVal hRec As Double, ByVal delta_hRec As Double) As Integer
Private Declare Function GetResults Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_GetResults"(ByRef p2p_struct As Integer, ByRef att_dB As Double) As Integer
Private Declare Function Delete Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_Delete"(ByRef p2p_struct As Integer)
 
Sub Main()
Dim p2p As Integer
p2p = Create()
Clear(p2p)
AddSegment(p2p, 10.0, 10.0, 1)
'AddSegment(p2p, 14.0, -3.0, 5)
'AddSegment(p2p, 50.0, -3.0, 4)
'SetSourceHeight(p2p, 0.3, 0.0)
'SetReceiverHeight(p2p, 5.0, 0.0)
 
' Dim att(0 To 27) As Double
 
'GetResults(p2p, att(0))
 
End Sub
 

<!-- END TEMPLATE: bbcode_code -->J obtiens un message d erreur du type :
<!-- BEGIN TEMPLATE: bbcode_quote -->

Citation:
The runtime has encountered a fatal error. The address of the error was
at 0x79e784b6, on thread 0xc78. The error code is 0xc0000005. This
error may be a bug in the CLR or in the unsafe or non-verifiable
portions of user code. Common sources of this bug include user
marshaling errors for COM-interop or PInvoke, which may corrupt the
stack.

<!-- END TEMPLATE: bbcode_quote -->La fonction AddSegment fait planter
le tout. Je pense que cela provient du fait que toutes les fonctions
attendent un void* et que moi je passe un Integer Byref. Je ne sais pas
quoi faire. Si quelqu un peut m aider


Please....

2 réponses

ncyshaolin Messages postés 6 Date d'inscription mercredi 21 novembre 2007 Statut Membre Dernière intervention 4 janvier 2008
21 nov. 2007 à 17:15
J ai change mon code en :
Imports System.Runtime.InteropServices
Module Module1
    Private Declare Function Create Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_Create" () As Integer
    Private Declare Function Clear Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_Clear" (<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal p2p_struct As Object) As Integer
    Private Declare Function AddSegment Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_AddSegment" (<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal p2p_struct As Object, ByVal D As Double, ByVal Z As Double, ByVal impedanceModel As Integer) As Integer
    Private Declare Function SetSourceHeight Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_SetSourceHeight" (<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal p2p_struct As Object, ByVal hSource As Double, ByVal delta_hs As Double) As Integer
    Private Declare Function SetReceiverHeight Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_SetReceiverHeight" (<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal p2p_struct As Object, ByVal hRec As Double, ByVal delta_hRec As Double) As Integer
    Private Declare Function GetResults Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_GetResults" (<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal p2p_struct As Object, ByVal att_dB As Double) As Integer
    Private Declare Function Delete Lib "C:\Documents and Settings\guerini-grossc\My Documents\PointToPoint.dll" Alias "P2P_Delete" (<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal p2p_struct As Object) As Integer

    Sub Main()
        Dim p2p As Object
        Dim att(0 To 100) As Double

        p2p = Create()
        Clear(p2p)
        AddSegment(p2p, 10.0, 10.0, 1)
        AddSegment(p2p, 14.0, -3.0, 5)
        AddSegment(p2p, 50.0, -3.0, 4)
        SetSourceHeight(p2p, 0.3, 0.0)
        SetReceiverHeight(p2p, 5.0, 0.0)
        'GetResults(p2p, att(0))
        Delete(p2p)

        Dim i As Integer
        For i = 0 To att.GetUpperBound(0)
            Console.WriteLine("att(" & i & ") = " & att(i))
        Next i
        Dim ligne As String = Console.In.ReadLine()
    End Sub

End Module

Il semble que cela marche sauf la fonction principale GetResults qui cause une erreur de type : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Les changements que j ai effectue proviennent de ce que j ai pu trouver sur les aides de microsoft bien que je ne comprenne pas ce qui se passe vraiment.
J attends de l aide de votre part.
0
lesdis Messages postés 403 Date d'inscription mercredi 19 avril 2006 Statut Membre Dernière intervention 7 août 2020
21 nov. 2007 à 17:21
Bonjour,

Si je ne m'abuse, il faut aller voir du coté de la classe MarshalLes pointeurs en Vb sont normalement définis par la structure IntPtrBonne Prog
0
Rejoignez-nous