Type convertion and Unmanaged code
-
:doh:Hi guys, Im trying to convert some visual basic code into c#, dose anybody know what's the equal things in c# for following function declaration and type. any help is appreciated. Public Declare Function Test Lib "x.dll" _ Alias "#3" (ByVal F1 As String, ByVal F2 As Long, ByVal F3 As Long, CDCAr As CDCAResult) As Long Type CDCAResult ID_0 As Byte ID_1 As String * 17 ID_2(0 To 11) As Long ID_3(0 To 7) As String * 41 End Type
-
:doh:Hi guys, Im trying to convert some visual basic code into c#, dose anybody know what's the equal things in c# for following function declaration and type. any help is appreciated. Public Declare Function Test Lib "x.dll" _ Alias "#3" (ByVal F1 As String, ByVal F2 As Long, ByVal F3 As Long, CDCAr As CDCAResult) As Long Type CDCAResult ID_0 As Byte ID_1 As String * 17 ID_2(0 To 11) As Long ID_3(0 To 7) As String * 41 End Type
For information on P/Invoking native functions, see the
DllImportAttribute
documentation in the .NET Framework SDK. An example follows:[DllImport("x.dll")]
private static extern long Test(
string f1, long f2, long f3, CDCAResult CDCAr);For the struct, you create the managed version just like you would re-create it in VB:
[StructLayout(LayoutKind.Sequential)]
public struct CDCAResult
{
public byte id0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=17)]public string id1;
public long id2;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]public string id3;
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
For information on P/Invoking native functions, see the
DllImportAttribute
documentation in the .NET Framework SDK. An example follows:[DllImport("x.dll")]
private static extern long Test(
string f1, long f2, long f3, CDCAResult CDCAr);For the struct, you create the managed version just like you would re-create it in VB:
[StructLayout(LayoutKind.Sequential)]
public struct CDCAResult
{
public byte id0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=17)]public string id1;
public long id2;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]public string id3;
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
thanx for reply, I have 2 question: 1- Is it the same declaration for Fixed-Length String and arrays which contains Fixed-Length String for exapmle: F1 As String * 31 F2(0 To 7) As String * 41 2- when i use like this [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, CDCAResult CDCAr); i get error, but if i use by reference like this: [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, [MarshalAs(UnmanagedType.Struct)] ref CDCAResult CDCAr); it works without error, but still dosen't return correct value for Fixed-Length String in that structure. any help really appreciated. best regards
-
thanx for reply, I have 2 question: 1- Is it the same declaration for Fixed-Length String and arrays which contains Fixed-Length String for exapmle: F1 As String * 31 F2(0 To 7) As String * 41 2- when i use like this [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, CDCAResult CDCAr); i get error, but if i use by reference like this: [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, [MarshalAs(UnmanagedType.Struct)] ref CDCAResult CDCAr); it works without error, but still dosen't return correct value for Fixed-Length String in that structure. any help really appreciated. best regards
Sorry, didn't notice that the last param in your Function was a ByRef. Yes, the
ref
keyword would be necessary then. You could also use theout
keyword if you don't need to pass an initialized struct into the function. That would save you from having to instantiate the struct before calling the method. As far as the fixed-length string question, you'll have to jog my memory on theF2(0 To 7) As String * 41
syntax. It's been a LONG time since I programmed in VB.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
thanx for reply, I have 2 question: 1- Is it the same declaration for Fixed-Length String and arrays which contains Fixed-Length String for exapmle: F1 As String * 31 F2(0 To 7) As String * 41 2- when i use like this [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, CDCAResult CDCAr); i get error, but if i use by reference like this: [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, [MarshalAs(UnmanagedType.Struct)] ref CDCAResult CDCAr); it works without error, but still dosen't return correct value for Fixed-Length String in that structure. any help really appreciated. best regards
I also got to thinking that the encoding could be the problem. Does this function that you're wanting to P/Invoke only support ANSI characters, Unicode characters, or either ANSI or Unicode depending on the operating system? Depending on what encoding it uses, you should add the
CharSet
property in theStructLayoutAttribute
:[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct CDCAResult
{
// ...
}The
SizeConst
property in theMarshalAsAttribute
will make sure that the offsets are correct for theString
fields, so assuming that the rest of your fields where the values you were expecting the character encoding is most likely the problem.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
I also got to thinking that the encoding could be the problem. Does this function that you're wanting to P/Invoke only support ANSI characters, Unicode characters, or either ANSI or Unicode depending on the operating system? Depending on what encoding it uses, you should add the
CharSet
property in theStructLayoutAttribute
:[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct CDCAResult
{
// ...
}The
SizeConst
property in theMarshalAsAttribute
will make sure that the offsets are correct for theString
fields, so assuming that the rest of your fields where the values you were expecting the character encoding is most likely the problem.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----