1
+ // This sample code shows a bug in the .NET marshaller.
2
+ // Native C++ functions returning a string (char*) sometimes
3
+ // are not marshalled correctly throwing an exception.
4
+ // This is the reason why the .NET wrapper converts
5
+ // the char-pointer manually into a .NET string.
6
+
7
+ using System ;
8
+ using System . Text ;
9
+ using System . Runtime . InteropServices ;
10
+
11
+ public static class FreeImage
12
+ {
13
+ private const string dllName = "FreeImage.dll" ;
14
+
15
+ [ DllImport ( dllName , EntryPoint = "FreeImage_GetFormatFromFIF" ) ]
16
+ [ return : MarshalAs ( UnmanagedType . LPStr ) ]
17
+ public static extern string GetFormatFromFIF ( int format ) ;
18
+
19
+ [ DllImport ( dllName , EntryPoint = "FreeImage_GetFIFExtensionList" ) ]
20
+ [ return : MarshalAs ( UnmanagedType . LPStr ) ]
21
+ public static extern string GetFIFExtensionList ( int format ) ;
22
+
23
+ [ DllImport ( dllName , EntryPoint = "FreeImage_GetFIFDescription" ) ]
24
+ [ return : MarshalAs ( UnmanagedType . LPStr ) ]
25
+ public static extern string GetFIFDescription ( int format ) ;
26
+
27
+ [ DllImport ( dllName , EntryPoint = "FreeImage_GetFIFRegExpr" ) ]
28
+ [ return : MarshalAs ( UnmanagedType . LPStr ) ]
29
+ public static extern string GetFIFRegExpr ( int format ) ;
30
+
31
+ [ DllImport ( dllName , EntryPoint = "FreeImage_GetFIFMimeType" ) ]
32
+ [ return : MarshalAs ( UnmanagedType . LPStr ) ]
33
+ public static extern string GetFIFMimeType ( int format ) ;
34
+
35
+ [ DllImport ( dllName , EntryPoint = "FreeImage_GetFIFCount" ) ]
36
+ public static extern int GetFIFCount ( ) ;
37
+ }
38
+
39
+ public class Program
40
+ {
41
+ public static bool [ , ] getmatrix ( int fifCount )
42
+ {
43
+ bool [ , ] matrix = new bool [ 5 , fifCount ] ;
44
+
45
+ int j = 0 ;
46
+
47
+ for ( int i = 0 ; i < fifCount ; i ++ )
48
+ {
49
+ bool success = true ;
50
+ try
51
+ {
52
+ FreeImage . GetFormatFromFIF ( i ) ;
53
+ }
54
+ catch
55
+ {
56
+ success = false ;
57
+ }
58
+ matrix [ j , i ] = success ;
59
+ }
60
+ j ++ ;
61
+
62
+ for ( int i = 0 ; i < fifCount ; i ++ )
63
+ {
64
+ bool success = true ;
65
+ try
66
+ {
67
+ FreeImage . GetFIFExtensionList ( i ) ;
68
+ }
69
+ catch
70
+ {
71
+ success = false ;
72
+ }
73
+ matrix [ j , i ] = success ;
74
+ }
75
+ j ++ ;
76
+
77
+ for ( int i = 0 ; i < fifCount ; i ++ )
78
+ {
79
+ bool success = true ;
80
+ try
81
+ {
82
+ FreeImage . GetFIFDescription ( i ) ;
83
+ }
84
+ catch
85
+ {
86
+ success = false ;
87
+ }
88
+ matrix [ j , i ] = success ;
89
+ }
90
+ j ++ ;
91
+
92
+ for ( int i = 0 ; i < fifCount ; i ++ )
93
+ {
94
+ bool success = true ;
95
+ try
96
+ {
97
+ FreeImage . GetFIFRegExpr ( i ) ;
98
+ }
99
+ catch
100
+ {
101
+ success = false ;
102
+ }
103
+ matrix [ j , i ] = success ;
104
+ }
105
+ j ++ ;
106
+
107
+ for ( int i = 0 ; i < fifCount ; i ++ )
108
+ {
109
+ bool success = true ;
110
+ try
111
+ {
112
+ FreeImage . GetFIFMimeType ( i ) ;
113
+ }
114
+ catch
115
+ {
116
+ success = false ;
117
+ }
118
+ matrix [ j , i ] = success ;
119
+ }
120
+
121
+ return matrix ;
122
+ }
123
+
124
+ public static void Main ( )
125
+ {
126
+ int fifCount = FreeImage . GetFIFCount ( ) ;
127
+ bool [ , ] matrix = getmatrix ( fifCount ) ;
128
+
129
+ Console . Clear ( ) ;
130
+ Console . WriteLine ( "1: GetFormatFromFIF" ) ;
131
+ Console . WriteLine ( "2: GetFIFExtensionList" ) ;
132
+ Console . WriteLine ( "3: GetFIFDescription" ) ;
133
+ Console . WriteLine ( "4: GetFIFRegExpr" ) ;
134
+ Console . WriteLine ( "5: GetFIFMimeType" ) ;
135
+ Console . Write ( Environment . NewLine ) ;
136
+
137
+ Console . Write ( "FIF:\t " ) ;
138
+ for ( int x = 1 ; x < 6 ; x ++ )
139
+ Console . Write ( "{0}:\t " , x ) ;
140
+
141
+ Console . Write ( Environment . NewLine ) ;
142
+
143
+ for ( int k = 0 ; k < fifCount ; k ++ )
144
+ {
145
+ Console . Write ( "{0}:\t " , k ) ;
146
+ for ( int l = 0 ; l < 5 ; l ++ )
147
+ {
148
+ Console . Write ( matrix [ l , k ] ? "1\t " : "0\t " ) ;
149
+ }
150
+ Console . Write ( Environment . NewLine ) ;
151
+ }
152
+ }
153
+ }
0 commit comments