Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

Help! Question with marshalling

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
f.bai - 21 Sep 2004 19:31 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.