Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2003

Tip: Looking for answers? Try searching our database.

How to return a structure from unmanaged C DLL?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex Vasylyev - 15 Sep 2003 09:09 GMT
Hi there,

I use an old C API DLL with a function, that returns a
structure like this:

struct err_struct
{
short error_type;
long eppix_status;
char message_data[51];
long error_code;
short isam_error;
char error_text[501];
};

Function is defined like this:
struct err_struct _stdcall DummyFunction(long *);

In C# I define structure and Function like this:
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct ErrorStruct
{
public short error_type;
public int eppix_status;
public string message_data;
public int error_code;
public short isam_error;
public string error_text;
}
[DllImport("Dummy.DLL",
CallingConvention=CallingConvention.StdCall,
CharSet=CharSet.Ansi)]
public static extern ErrorStruct DummyFunction(ref int
SizeOfCache);

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
microsoft - 15 Sep 2003 09:22 GMT
You can try this. I didn't test it.

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct ErrorStruct
{
 public short error_type;
 public int eppix_status;
 [MarshalAs(UnmanagedType.LPStr,SizeConst=51)]
 public string message_data;
 public int error_code;
 public short isam_error;
 [MarshalAs(UnmanagedType.LPStr,SizeConst=501)]
 public string error_text;
}

"Alex Vasylyev" <avasylyev@yahoo.com> ????????????
:028b01c37b60$a06709d0$a001280a@phx.gbl...
> Hi there,
[quoted text clipped - 42 lines]
> Regards,
> Alex
Alex Vasylyev - 15 Sep 2003 09:35 GMT
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 - 50 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.
[quoted text clipped - 6 lines]
>
>.
Jayacham - 15 Sep 2003 10:08 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.