Ok. The follow is working;
structure define:
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct ErrorStruct
{
public short error_type;
public int eppix_status;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=51)]
public string message_data;
public int error_code;
public short isam_error;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=501)]
public string error_text;
}
PInvoke define:
[DllImport("dlldemo.DLL",
CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Ansi)]
public static extern IntPtr DummyFunction(ref int
SizeOfCache);
invoke sample:
int r=0;
System.IntPtr ip = DummyFunction(ref r);
ErrorStruct es;
es = (ErrorStruct)Marshal.PtrToStructure(ip,typeof(ErrorStruct));
System.Diagnostics.Debug.WriteLine(es.message_data);
"Alex Vasylyev" <avasylyev@yahoo.com> ????????????
:065e01c37b64$59bde8b0$a101280a@phx.gbl...
Sorry, but it does not work :(
Exception is the same.
Alex
>-----Original Message-----
>You can try this. I didn't test it.
[quoted text clipped - 34 lines]
>> In C# I define structure and Function like this:
>> [StructLayout
(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
>> public struct ErrorStruct
>> {
[quoted text clipped - 12 lines]
>>
>> I try to call this function from C# and
receive 'System.Runtime.InteropServices.MarshalDirectiveExc
>> eption with additional text: The method's sinature is
not
>> PInvoke compartible.
>>
>> How do I have to define this structure and function in
C#
>> to be able to call this API function?
>>
>> Regards,
>> Alex
>
>.
Alex Vasylyev - 15 Sep 2003 14:12 GMT
Thank you for answer,
but it still does not work. Now I receive
System.NullReferenceException exception.
Alex
>-----Original Message-----
>Ok. The follow is working;
[quoted text clipped - 103 lines]
>>> Regards,
>>> Alex
David Stucki [MS] - 16 Sep 2003 20:13 GMT
I don't know of anyway your can have a structure be the return value like
this. You could change your API to return an HRESULT and have a pointer to
the struct as the last parameter.
In the DLL:
HRESULT __stdcall DummyFunction(long *, /*out*/ err_struct *pRetVal);
And use either of these definitions:
[
DllImport("dlldemo.DLL",
CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Ansi,
PreserveSig=false)
]
public static extern ErrorStruct DummyFunction(ref int SizeOfCache);
[
DllImport("dlldemo.DLL",
CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Ansi)
]
public static extern int DummyFunction(ref int SizeOfCache, out ErrorStruct
revVal);
Another option would be to create a managed C++ or COM wrapper to call this
(and possible other) method(s).
Hope this helps,
David Stucki
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.