Click here to Skip to main content
15,918,193 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: calling VC++ DLL from VB .NET Pin
Christian Graus16-Aug-05 16:20
protectorChristian Graus16-Aug-05 16:20 
GeneralRe: calling VC++ DLL from VB .NET Pin
Jiannong16-Aug-05 16:45
Jiannong16-Aug-05 16:45 
GeneralRe: calling VC++ DLL from VB .NET Pin
Christian Graus16-Aug-05 16:52
protectorChristian Graus16-Aug-05 16:52 
GeneralRe: calling VC++ DLL from VB .NET Pin
Jiannong16-Aug-05 17:02
Jiannong16-Aug-05 17:02 
GeneralRe: calling VC++ DLL from VB .NET Pin
Christian Graus16-Aug-05 17:04
protectorChristian Graus16-Aug-05 17:04 
GeneralRe: calling VC++ DLL from VB .NET Pin
Jiannong16-Aug-05 17:11
Jiannong16-Aug-05 17:11 
GeneralRe: calling VC++ DLL from VB .NET Pin
Christian Graus16-Aug-05 17:15
protectorChristian Graus16-Aug-05 17:15 
GeneralRe: calling VC++ DLL from VB .NET Pin
Jiannong17-Aug-05 15:56
Jiannong17-Aug-05 15:56 
Hi, Christian

Below is my VB .NET source code. There is one Click Button Sub. and other three test classes to wrap functions of DLL. 1st is using Declare Function. 2rd is using DLLImport. 3rd is a sample class for User32.dll. it's ok.

I think that the problem maybe parameter type mapping is error bewteen VC++ DLL and VB .NET. Please give me advise. thanks.

Best regars, jiannong




The VB .NET source code is below

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim gnNumOfDevice As Integer
Dim ErrCde As Long

Const SUCESS = &H0

Dim TestDll As TestDLL
Dim TestDll2 As TestDLL2
Dim devicelist As Long
Dim MaxEntries As Integer
Dim nOutEntries As Integer
Dim DeviceNum As Long
Dim DriverHandle As Integer
Dim ErrMsg As String

Dim ptDioWritePortByte As PT_WritePortByte1
Dim Port As Integer
Dim ByteData As Integer

DeviceNum = &H0
ErrCde = &H0


Dim testdll3 As TESTDLL3

testdll3.MessageBox(0, "Hello World", "Platform Invoke Sample", 0)

ErrCde = TestDll2.DRV_DeviceGetNumOfList(gnNumOfDevice)

ErrCde = TestDll.DRV_DeviceGetNumOfList(gnNumOfDevice)

ErrCde = TestDll.DRV_DeviceGetList(devicelist, MaxEntries, nOutEntries)

ptDioWritePortByte.Port = 0
ptDioWritePortByte.ByteData = &H55

ErrCde = TestDll2.DRV_DeviceOpen(DeviceNum, DriverHandle)
TestDll.DRV_GetErrorMessage(ErrCde, ErrMsg)

ErrCde = TestDll2.DRV_WritePortByte(DriverHandle, ptDioWritePortByte)

End Sub


Using Class to Declare DLL function

Imports System.Runtime.InteropServices

<structlayout(layoutkind.sequential)> Public Structure PT_WritePortByte
Dim Port As Integer
Dim ByteData As Integer
End Structure

<structlayout(layoutkind.sequential)> Public Structure PT_WritePortWord
Dim Port As Integer
Dim WordData As Integer
End Structure

'////////////////////////// V2.0C \\\\\\\\\\\\\\\\\\\\\\\\
<structlayout(layoutkind.sequential)> Public Structure PT_WritePortDword
Dim Port As Integer
Dim DwordData As Long
End Structure

'////////////////////////// V2.0C \\\\\\\\\\\\\\\\\\\\\\\\
<structlayout(layoutkind.sequential)> Public Structure PT_ReadPortByte
Dim Port As Integer
Dim ByteData As Long ' USHORT far *ByteData
End Structure

<structlayout(layoutkind.sequential)> Public Structure PT_ReadPortWord
Dim Port As Integer
Dim WordData As Long ' USHORT far *WordData
End Structure
'////////////////////////// V2.0C \\\\\\\\\\\\\\\\\\\\\\\\
<structlayout(layoutkind.sequential)> Public Structure PT_ReadPortDword
Dim Port As Integer
Dim DwordData As Long
End Structure

Public Class TestDLL
'////////////////////////// V2.0C \\\\\\\\\\\\\\\\\\\\\\\\
Declare Function DRV_SelectDevice Lib "adsapi32.dll" (ByVal hCaller As Long, ByVal GetModule As Boolean, ByVal DeviceNum As Long, ByVal Description As String) As Long
Declare Function DRV_DeviceGetNumOfList Lib "adsapi32.dll" (ByVal NumOfDevices As Integer) As Long
Declare Function DRV_DeviceGetList Lib "adsapi32.dll" (ByVal devicelist As Long, ByVal MaxEntries As Integer, ByVal nOutEntries As Integer) As Long
Declare Function DRV_DeviceGetSubList Lib "adsapi32.dll" (ByVal DeviceNum As Long, ByVal SubDevList As Long, ByVal MaxEntries As Integer, ByVal nOutEntries As Integer) As Long
Declare Function DRV_DeviceOpen Lib "adsapi32.dll" (ByVal DeviceNum As Long, ByVal DriverHandle As Long) As Long
Declare Function DRV_DeviceClose Lib "adsapi32.dll" (ByVal DriverHandle As Long) As Long
' Declare Function DRV_DeviceGetFeatures Lib "adsapi32.dll" (ByVal DriverHandle As Long, ByVal lpDevFeatures As PT_DeviceGetFeatures) As Long

Declare Sub DRV_GetErrorMessage Lib "adsapi32.dll" (ByVal lError As Long, ByVal lpszszErrMsg As String)

Declare Function DRV_WritePortByte Lib "adsapi32.dll" (ByVal DriverHandle As Long, ByVal WritePortByte As PT_WritePortByte) As Long
Declare Function DRV_WritePortWord Lib "adsapi32.dll" (ByVal DriverHandle As Long, ByVal WritePortWord As PT_WritePortWord) As Long
Declare Function DRV_ReadPortByte Lib "adsapi32.dll" (ByVal DriverHandle As Long, ByVal ReadPortByte As PT_ReadPortByte) As Long
Declare Function DRV_ReadPortWord Lib "adsapi32.dll" (ByVal DriverHandle As Long, ByVal ReadPortWord As PT_ReadPortWord) As Long

End Class



Using Class to declare function by DLLImport

Imports System.Runtime.InteropServices

<structlayout(layoutkind.sequential)> Public Structure PT_WritePortByte1
Dim Port As Integer
Dim ByteData As Integer
End Structure

Public Class TestDLL2

<dllimport("adsapi32.dll")> _
Public Shared Function DRV_DeviceGetNumOfList(ByVal NumOfDevices As Integer) As Long
End Function

Public Shared Function DRV_DeviceOpen(ByVal DeviceNum As Long, ByVal DriverHandle As Integer) As Long
End Function

Public Shared Function DRV_WritePortByte(ByVal DriverHandle As Integer, ByVal WritePortByte As PT_WritePortByte1) As Long
End Function

End Class


Try the sample, it’s Ok

Imports System.Runtime.InteropServices

Public Class TESTDLL3
Declare Auto Function MessageBox Lib "user32.dll" _
(ByVal hWnd As Integer, _
ByVal txt As String, ByVal caption As String, _
ByVal Typ As Integer) As Integer
End Class
GeneralRe: calling VC++ DLL from VB .NET Pin
Christian Graus17-Aug-05 18:18
protectorChristian Graus17-Aug-05 18:18 
GeneralRe: calling VC++ DLL from VB .NET Pin
Jiannong17-Aug-05 19:09
Jiannong17-Aug-05 19:09 
GeneralNew To VB.Net Pin
mdcoker16-Aug-05 10:27
mdcoker16-Aug-05 10:27 
GeneralRe: New To VB.Net Pin
ToddHileHoffer16-Aug-05 10:38
ToddHileHoffer16-Aug-05 10:38 
GeneralText Format Pin
ryanchk16-Aug-05 9:01
ryanchk16-Aug-05 9:01 
GeneralRe: Text Format Pin
thealca16-Aug-05 14:13
thealca16-Aug-05 14:13 
GeneralRe: Text Format Pin
ryanchk17-Aug-05 3:38
ryanchk17-Aug-05 3:38 
Generaldll injection with vb.net Pin
deatos16-Aug-05 7:21
deatos16-Aug-05 7:21 
GeneralRe: dll injection with vb.net Pin
Christian Graus16-Aug-05 13:59
protectorChristian Graus16-Aug-05 13:59 
Generalvb.net winform move folder Pin
partt16-Aug-05 7:03
partt16-Aug-05 7:03 
GeneralRegular Expression in VBA Pin
alex_sleiborg16-Aug-05 4:58
alex_sleiborg16-Aug-05 4:58 
GeneralDynamic arrays Pin
macca2416-Aug-05 3:43
macca2416-Aug-05 3:43 
GeneralRe: Dynamic arrays Pin
toxcct16-Aug-05 3:47
toxcct16-Aug-05 3:47 
GeneralRe: Dynamic arrays Pin
macca2416-Aug-05 4:08
macca2416-Aug-05 4:08 
GeneralRe: Dynamic arrays Pin
Mandar Patankar17-Aug-05 10:13
Mandar Patankar17-Aug-05 10:13 
GeneralDatagrid Questions Pin
dptalt16-Aug-05 3:32
dptalt16-Aug-05 3:32 
GeneralRe: Datagrid Questions Pin
thebread16-Aug-05 9:47
thebread16-Aug-05 9:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.