[Mono-list] Problems with DllImport attribute and calling libuser32.so
Steinar Herland
steinar.herland@gecko.no
Fri, 12 Jul 2002 10:48:15 +0200
This is a multi-part message in MIME format.
------_=_NextPart_001_01C22980.DCC51BBE
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
I'm trying to call the MessageBox function in the WineLib libuser32.so
The attached Class1.cs file works on WinXP (using native user32.dll),
but fails on mono (linux)
I found that if I compile Class1.cs on mono (linux) it does not create
the same method-definition.
There seems to be problems (at least) with the DllImport.CharSet and
DllImport.ExactSpelling attributes.
From my Class1.cs-file:
class Win32Methods{
//mcs & csc il: .method public hidebysig static pinvokeimpl("user32"
ansi)=20
[DllImport("user32",
CharSet=3DCharSet.Ansi,ExactSpelling=3Dfalse,CallingConvention=3DCallingC=
onven
tion.StdCall)]
public extern static int MessageBox(IntPtr hWnd, string text, string
caption, int type);
//csc il: .method public hidebysig static pinvokeimpl("user32" unicode)
//mcs il: .method public hidebysig static pinvokeimpl("user32" nomangle
ansi)=20
[DllImport("user32",
CharSet=3DCharSet.Unicode,ExactSpelling=3Dfalse,CallingConvention=3DCalli=
ngCon
vention.StdCall)]
public extern static int MessageBoxW(IntPtr hWnd, string text, string
caption, int type);
//mcs & csc il: .method public hidebysig static pinvokeimpl("user32"
ansi)=20
[DllImport("user32",
CharSet=3DCharSet.Ansi,ExactSpelling=3Dfalse,CallingConvention=3DCallingC=
onven
tion.StdCall)]
public extern static int MessageBoxA(IntPtr hWnd, string text, string
caption, int type);
}
class Win32Methods2{
//csc: .method public hidebysig static pinvokeimpl("user32" nomangle
ansi)=20
//mcs: .method public hidebysig static pinvokeimpl("user32" ansi)=20
[DllImport("user32",
CharSet=3DCharSet.Ansi,ExactSpelling=3Dtrue,CallingConvention=3DCallingCo=
nvent
ion.StdCall)]
public extern static int MessageBox(IntPtr hWnd, string text, string
caption, int type);
}
-----------------------------
Output running mono on linux:
-----------------------------
** (process:11827): WARNING **: Failed to load function MessageBox from
user32
** (process:11827): WARNING **: Failed to load function MessageBox from
user32
err:heap:HEAP_GetPtr Invalid heap 00000000!
err:heap:HEAP_GetPtr Invalid heap 00000000!
Failure calling Win32Methods.MessageBoxW System.NullReferenceException
A null value was found where an object instance was
requiredSystem.NullReference
Exceptionin (unmanaged) Win32Api.Win32Methods:MessageBoxW ()
in <0x0004a> Win32Api.Class1:Main (string[])
Failure calling Win32Methods.MessageBoxA System.NullReferenceException
A null value was found where an object instance was
requiredSystem.NullReference
Exceptionin (unmanaged) Win32Api.Win32Methods:MessageBoxA ()
in <0x000bf> Win32Api.Class1:Main (string[])
Failure calling Win32Methods.MessageBox System.NotImplementedException
The requested feature is not yet
implementedSystem.NotImplementedExceptionin (un
managed) Win32Api.Win32Methods:MessageBox ()
in <0x00134> Win32Api.Class1:Main (string[])
Failure calling Win32Methods2.MessageBox System.NotImplementedException
The requested feature is not yet
implementedSystem.NotImplementedExceptionin (un
managed) Win32Api.Win32Methods2:MessageBox ()
in <0x001a9> Win32Api.Class1:Main (string[])
------------------------
Output running on WinXP:
------------------------
Failure calling Win32Methods2.MessageBox
System.EntryPointNotFoundException: Unable to find an entry point named
MessageBox in DLL user32.
at Win32Api.Win32Methods2.MessageBox(IntPtr hWnd, String text, String
caption
, Int32 type)
at Win32Api.Class1.Main(String[] args) in
d:\dev\steinar\forms\winforms\class
1.cs:line 46
------------------------
This failure is expected because DllImport.ExactSpelling =3D true
Any help appreciated.
Steinar Herland
------_=_NextPart_001_01C22980.DCC51BBE
Content-Type: application/octet-stream;
name="Class1.cs"
Content-Transfer-Encoding: base64
Content-Description: Class1.cs
Content-Disposition: attachment;
filename="Class1.cs"
dXNpbmcgU3lzdGVtOw0KdXNpbmcgU3lzdGVtLlJ1bnRpbWUuSW50ZXJvcFNlcnZpY2VzOw0KDQpu
YW1lc3BhY2UgV2luMzJBcGkgew0KCWNsYXNzIFdpbjMyTWV0aG9kc3sNCgkJLy9tY3MgJiBjc2Mg
aWw6IC5tZXRob2QgcHVibGljIGhpZGVieXNp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