Re: exporting function with parm pointer to struct
From: Angel (none)
Date: 03/03/04
- Next message: Brad Williams: "Re: String Wrapping"
- Previous message: Hatul Shilgy: "Re: Strange bug: condition true in IF statement, false the next statement"
- In reply to: Nicholas Paldino [.NET/C# MVP]: "Re: exporting function with parm pointer to struct"
- Next in thread: v-jetan_at_online.microsoft.com: "Re: exporting function with parm pointer to struct"
- Reply: v-jetan_at_online.microsoft.com: "Re: exporting function with parm pointer to struct"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 3 Mar 2004 12:31:56 -0400
Thanks for the post.
I did what you suggested but I still get "This type can not be marshaled as
a structure field" when I call the function. This is my initial code:
[DllImport("C:\\zm7\\Developm\\DLL\\ZIP4_W32.DLL")]
public static extern int z9indqry(4_PARM parm);
public static void getParms ()
{
int i;
Zip4_parm parm = new Zip4_parm();
i = z9indqry(parm); // Error: "This type can not be marshaled as a
structure field"
// IntPtr buffer = Marshal.AllocCoTaskMem( Marshal.SizeOf( parm ));
}
What can this error mean?
Also, I'm trying to delare some const vars from the initial H file (eg.
#define Z4_INVADDR 10) but adding it to the cs file that contains the
structs generates error "Expected class, delegate, enum, interface, or
struct".
Any help would be appreciated.
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:#9XbMLTAEHA.580@TK2MSFTNGP11.phx.gbl...
> Angel,
>
> If your structures are really that flexible, then I would recommend
that
> you not use a pointer to the struct, but rather, a pointer to void in the
> declaration. This will prevent you from having to recompile, as well as
> redefine the C# code (at least for the declaration).
>
> Also, it would imply that you need a better design that is more
> flexible, IMO. Changing the structure every two months is very time
> consuming. It might make sense to have a keyed collection of some kind to
> use.
>
> That being said, with what you have now, you will have to declare the
> structure in C#. You will want it to have the same layout in memory as
the
> structure in C# (remember longs in C are ints in C#, etc, etc). Also, you
> will want to adorn the structure with the StructLayout attribute, passing
> LayoutKind.Sequential to the constructor.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
> "Angel" <none> wrote in message
> news:%235%23Z0oSAEHA.2308@tk2msftngp13.phx.gbl...
> > I'm exporting (with DllImport) a C-style function with this syntax:
> >
> > int z9indqry (4_PARM *parm);
> >
> > 4_PARM is a structure declared in a proprietary header file that cannot
be
> > included in my project (due to C# limits).
> >
> > What would be the other best way to do this? I would like to be able to
> use
> > the original struct because these structs have over 40 members each and
> > there are several more Structs similar to 4_PARM. Also, they are
modified
> by
> > the company every two months so I'd need to compare and modify my C#
> structs
> > everytime they change the h structs.
> > I'm not going to use wrappers because the dll function calls are working
> > well when the parms are simple data types (int, char, string, etc...).
I
> > just need a way to be able to call these specialized structs.
> >
> > Thanks again.
> >
> > P.S. - I'm pretty sure this is the last question I have regarding
> unmanaged
> > code :-))
> >
> >
>
>
- Next message: Brad Williams: "Re: String Wrapping"
- Previous message: Hatul Shilgy: "Re: Strange bug: condition true in IF statement, false the next statement"
- In reply to: Nicholas Paldino [.NET/C# MVP]: "Re: exporting function with parm pointer to struct"
- Next in thread: v-jetan_at_online.microsoft.com: "Re: exporting function with parm pointer to struct"
- Reply: v-jetan_at_online.microsoft.com: "Re: exporting function with parm pointer to struct"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|