Re: exporting function with parm pointer to struct

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Angel (none)
Date: 03/03/04


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 :-))
> >
> >
>
>



Relevant Pages

  • Re: const qualifier and VC6.0
    ... > emphasis is on types, while in C the emphasis is on expressions. ... When declaring pointer and reference variables, ... int &p; ... For example, the following declaration ...
    (comp.lang.cpp)
  • Re: how to call a variable without reference in c
    ... fuction with out sending as a argument, pointer and can't declare as ... int a=10; ... Not in his declaration it didn't. ...
    (comp.lang.c)
  • Re: C# prototype of C: void**
    ... public unsafe void Capture (int width, int height, bytebuffer) ... but I got the error message "A ref or out argument must be an lvalue". ... declaration in the parameter list of Capture allows changing of the ... > have a pointer to a pointer, you need to pass it by ref. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: would C be easier to read if...
    ... That indicates that it's a pointer to a function. ... int *a a is pointer to int ... In my case a type declaration that reads linearly from left to right would ... Let's say we have a regular int identifier named "foo". ...
    (comp.lang.c)
  • Re: how to call a variable without reference in c
    ... fuction with out sending as a argument, pointer and can't declare as ... main returns an int. ... The 'a' in fun() has no declaration and your compiler ought to have told you so. ...
    (comp.lang.c)