[Mono-dev] PROBLEMS CALLING STRUCTURES UNMANAGED TO MANAGED AS PARAMETER USING MONO.

Justin Cherniak compwiz312 at gmail.com
Sat Apr 12 20:23:09 EDT 2008


I don't see any reason the following wouldn't work (and be much easier to
use):

struct Request
{
[MarshalAs(UnmanagedType.LPStr)]
string CategoryID;

[MarshalAs(UnmanagedType.LPStr)]
string Product_Code;

[MarshalAs(UnmanagedType.LPStr)]
string Build_Code;

[MarshalAs(UnmanagedType.LPStr)]
string Manufacturer_id;
uint Serial_Number;

[MarshalAs(UnmanagedType.LPStr)]
string Software_Version;
};

The [MarshalAs(UnmanagedType.LPStr)] attribute is important since you have
ANSI (char*) strings versus unicode strings.  Otherwise marshalling in this
way would be much easier to use.

Enjoy.

-- Justin


On Sat, Apr 12, 2008 at 2:08 AM, Jonathan Pryor <jonpryor at vt.edu> wrote:

> On Fri, 2008-04-11 at 19:01 -0500, Jose Castillo Reyes wrote:
>
> > struct Request
> > {
> >     char *CategoryID;
> >     char *Product_Code;
> >     char *Build_Code;
> >     char *Manufacturer_id;
> >     unsigned int Serial_Number;
> >     char *Software_Version;
> > };
>
>         struct Request {
>                public IntPtr CategoryID, ProductCode, BuildCode,
>                        Manufacturer_id;
>                public uint Serial_Number;
>                public IntPtr Software_Version;
>         }
>
> > int main(void)
> > {
> >     Request ID_Device;
> >     int Resultado;
> >
> >     Resultado = Manufacture(piHandle_coin, &ID_Device);
>
>         class Test {
>                [DllImport ("YourLib.dll")]
>                static extern int Manufacture(/*typeof piHandle_coin*/,
>                        out Request request);
>
>                static void Main (string[] args)
>                {
>                        Request ID_Device;
>                        int Resultado = Manufacture (piHandle_coin,
>                                out ID_Device);
>
> >   //*****************problems in &ID_DEVICE STRUCTURES UNMANAGED TO
> > MANAGED AS PARAMETER
> >
> >     printf("Id Categoria : %s\n",ID_Device.CategoryID);
> >     printf("Id Product: %s\n",ID_Device.Product_Code);
> >     printf("Id Build: %s\n",ID_Device.Build_Code);
> >     printf("Manufacturer: %s\n",ID_Device.Manufacturer_id);
> >     printf("Serial Number: %u\n",ID_Device.Serial_Number);
> >     printf("Software Revision: %s\n\n",ID_Device.Software_Version);
>
>                         Console.WriteLine (
>                                Marshal.PtrToString
>                                (ID_Device.CategoryID));
>                        // ...
>                }
>        }
>
> See also:
>
>        http://www.mono-project.com/dllimport
>
>  - Jon
>
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080412/66874945/attachment-0001.html 


More information about the Mono-devel-list mailing list