I have an old dll written in vc6 and have used it in vb6. Everything
works fine.
The vb6 declaration just and client code just like below:
Public Const DEF_D_OPERATER = 1
Public Type CRNO
No1 As String * 3
No2 As String * 5
End Type
Public Type DLG_IOSTR
keiban As CRNO
f10ban As CRNO
OpeID As String * 31
OpeName As String * 15
lpStr1 As Long
lpStr2 As Long
flag As Long
End Type
Public Declare Function dialog Lib "ktccomm" Alias "KTCDlgBox" (ByVal
hwnd As Long, ByVal kind As Long, dlg As DLG_IOSTR) As Long
dlg.lpStr1 = 0
dlg.lpStr2 = 0
ret = dialog(graphic_main.hwnd, DEF_D_OPERATER, dlg)
If ret = 1 Then
MsgBox dlg.OpeName
End If
now, I need to use this function in an .net application. But when i
run the .net program, a nullreference exception will be throwed.
My .net source code just like this:
Module OperatorDlg
Public Const DEF_D_OPERATER As Integer = 1
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure CRNO
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=3)> _
Public No1() As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=5)> _
Public No2() As Byte
Public Sub CRNO()
No1 = New Byte(3) {}
No2 = New Byte(5) {}
End Sub
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure DLG_IOSTR
Public keiban As CRNO
Public f10ban As CRNO
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=15)> _
Public OpeID() As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=31)> _
Public OpeName() As Byte
Public lpStr1 As String
Public lpStr2 As String
Public flag As Long
Public Sub dlg_iostr()
OpeID = New Byte(15) {}
OpeName = New Byte(31) {}
End Sub
End Structure
Declare Function OpInput Lib "ktccomm.dll" Alias "KTCDlgBox"
(ByVal hwnd As Long, ByVal kind As Long,
<MarshalAs(UnmanagedType.Struct)> ByRef dlg As DLG_IOSTR) As Long
Dim dlgs As DLG_IOSTR
dlgs.lpStr1 = "1"
dlgs.lpStr2 = "2"
dlgs.keiban.No1 = New Byte(2) {}
dlgs.keiban.No2 = New Byte(4) {}
dlgs.f10ban.No1 = New Byte(2) {}
dlgs.f10ban.No2 = New Byte(4) {}
dlgs.OpeID = New Byte(14) {}
dlgs.OpeName = New Byte(30) {}
Dim ret As Long = OpInput(0, DEF_D_OPERATER, dlgs) // This is the
statement that throws an exception
If ret = 1 Then
MessageBox.Show(dlgs.OpeName.ToString)
End If
Can anybody tell me what is the problem? Thanks a lot for your reply.
Mattias Sj?gren - 22 Sep 2004 07:01 GMT
> Declare Function OpInput Lib "ktccomm.dll" Alias "KTCDlgBox"
>(ByVal hwnd As Long, ByVal kind As Long,
><MarshalAs(UnmanagedType.Struct)> ByRef dlg As DLG_IOSTR) As Long
Change all occurances of Long to Integer.
Remove the MarshalAs attribute from the declaration above.
Mattias

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