- INDEX VB TO NET
- KNOWLEDGE BASE
- EWIS-ISSUES
- ISSUE #1050
TODO #1050
Structure %1 may require marshalling attributes to be passed as an argument in this Declare statement.
Description
In Visual Basic 6.0, user-defined types could be passed as an argument in a Declare statement for a Windows API.
In Visual Basic .NET, a structure (user-defined type) passed as an argument in a Declare statement may require additional marshalling attributes in order to be passed correctly to the external function or subroutine. In particular, arrays and fixed-length strings may not function as expected without these attributes.
Recommendations
Add an Imports statement to reference the System.Runtime.InteropServices namespace and then modify the structure and the string declaration to include marshalling attributes.
Sample VB6
Private Type MyStructure
Name AsString
Size AsInteger
End Type
DeclareFunction FunctionName Lib"MyLibrary.DLL" (ByVal rs1 As MyStructure) AsLong
Target VB.NET
PrivateStructure MyStructure
Dim Name AsString
Dim Size AsInteger
PublicSharedFunction CreateInstance() As MyStructure
Dim result AsNew MyStructure
result.Name = String.Empty
Return result
EndFunction
EndStructure
'UPGRADE_WARNING: (1050) Structure MyStructure may require marshalling attributes to be passed as an argument in this Declare statement.
DeclareFunction FunctionName Lib"MyLibrary.DLL" (ByVal rs1 As MyStructure) AsInteger
Expected VB.NET
PrivateStructure MyStructure
<MarshalAs(UnmanagedType.ByValTStr)> Dim Name AsString
<MarshalAs(UnmanagedType.I4)> Dim Size AsInteger
PublicSharedFunction CreateInstance() As MyStructure
Dim result AsNew MyStructure
result.Name = String.Empty
Return result
EndFunction
EndStructure
DeclareFunction FunctionName Lib"MyLibrary.DLL" (ByVal rs1 As MyStructure) AsInteger
Target C#
publicstructMyStructure
{
publicstring Name;
publicint Size;
publicstaticMyStructure CreateInstance()
{
MyStructure result = newMyStructure();
result.Name = String.Empty;
return result;
}
}
//UPGRADE_WARNING: (1050) Structure MyStructure may require marshalling attributes to be passed as an argument in this Declare statement.
[DllImport("MyLibrary.DLL")]
publicexternstaticint FunctionName( MyStructure rs1);
Expected C#
publicstructMyStructure
{
[MarshalAs(UnmanagedType. ByValTStr)]
publicstring Name;
[MarshalAs(UnmanagedType.I4)]
publicint Size;
publicstaticMyStructure CreateInstance()
{
MyStructure result = newMyStructure();
result.Name = String.Empty;
return result;
}
}
[DllImport("MyLibrary.DLL")]
publicexternstaticint FunctionName( MyStructure rs1);