Pass a structure by reference to C++ dll in .Net Compact Framework

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maloney

    Pass a structure by reference to C++ dll in .Net Compact Framework

    I'm trying to pass a structer from C# to a C dll in .Net Compact Framework

    This partially works. If in the GetStatus I only set -- status->fileLength
    = 10; -- and not the second variable then filelength = 10 and fileposition =
    0, but if i set the second one only or both of them then they are fileLength
    = 47244640266 and filePosition = 0; Like the memory is getting over wrote.
    What is the correct way of passing a structure by reference on the Compact
    Framework.

    here is my code

    In the C dll
    -------------------------------------------

    ----------------in the header
    struct FileStatus
    {
    long fileLength;
    long filePosition;
    };

    extern "C" __declspec(dlle xport) bool GetStatus (FileStatus* status);
    ----------------

    ---------------in the cpp
    bool GetStatus (FileStatus* status)
    {
    status->fileLength = 10;
    status->filePosition = 11;
    return true;
    }
    --------------------------------------------

    int my C# code i have
    --------------------------------------------
    namespace Controls
    {
    [StructLayout(La youtKind.Sequen tial)]
    public struct SStatus
    {
    public long fileLength;
    public long filePosition;
    }
    public class VitoSoundExplor er
    {

    [DllImport("Test Dll.dll", EntryPoint="Get Status")]
    public static extern bool GetStatus(ref SStatus status);


    public bool GetStatus()
    {
    SStatus st= new SStatus();
    GetStatus(ref st);
    return true;
    }
    }
    }
    ------------------------------------------

    Any Ideas

    thanks


  • Lloyd Dupont

    #2
    Re: Pass a structure by reference to C++ dll in .Net Compact Framework

    since when a long in C is 8 bytes long ?
    maybe on windows 64, but certainly not on PPC, or so I assume....

    I would rather write:
    [StructLayout(La youtKind.Sequen tial)]
    public struct SStatus
    {
    public int fileLength;
    public int filePosition;
    }

    "Maloney" <sabrogden@soft home.net> wrote in message
    news:OfFSUNUiDH A.3032@tk2msftn gp13.phx.gbl...[color=blue]
    > I'm trying to pass a structer from C# to a C dll in .Net Compact Framework
    >
    > This partially works. If in the GetStatus I only set --[/color]
    status->fileLength[color=blue]
    > = 10; -- and not the second variable then filelength = 10 and fileposition[/color]
    =[color=blue]
    > 0, but if i set the second one only or both of them then they are[/color]
    fileLength[color=blue]
    > = 47244640266 and filePosition = 0; Like the memory is getting over[/color]
    wrote.[color=blue]
    > What is the correct way of passing a structure by reference on the Compact
    > Framework.
    >
    > here is my code
    >
    > In the C dll
    > -------------------------------------------
    >
    > ----------------in the header
    > struct FileStatus
    > {
    > long fileLength;
    > long filePosition;
    > };
    >
    > extern "C" __declspec(dlle xport) bool GetStatus (FileStatus* status);
    > ----------------
    >
    > ---------------in the cpp
    > bool GetStatus (FileStatus* status)
    > {
    > status->fileLength = 10;
    > status->filePosition = 11;
    > return true;
    > }
    > --------------------------------------------
    >
    > int my C# code i have
    > --------------------------------------------
    > namespace Controls
    > {
    > [StructLayout(La youtKind.Sequen tial)]
    > public struct SStatus
    > {
    > public long fileLength;
    > public long filePosition;
    > }
    > public class VitoSoundExplor er
    > {
    >
    > [DllImport("Test Dll.dll", EntryPoint="Get Status")]
    > public static extern bool GetStatus(ref SStatus status);
    >
    >
    > public bool GetStatus()
    > {
    > SStatus st= new SStatus();
    > GetStatus(ref st);
    > return true;
    > }
    > }
    > }
    > ------------------------------------------
    >
    > Any Ideas
    >
    > thanks
    >
    >[/color]


    Comment

    • Lloyd Dupont

      #3
      Re: Pass a structure by reference to C++ dll in .Net Compact Framework

      FYI:
      here is the whole story, I believe:
      C:
      char: 1 bytes
      short: 2 bytes
      long: 4 bytes
      long long (gcc): 8 bytes
      int: size of register (4 on PPC)

      C#:
      int: 4 bytes
      long: 8 bytes
      see ?

      "Maloney" <sabrogden@soft home.net> wrote in message
      news:OfFSUNUiDH A.3032@tk2msftn gp13.phx.gbl...[color=blue]
      > I'm trying to pass a structer from C# to a C dll in .Net Compact Framework
      >
      > This partially works. If in the GetStatus I only set --[/color]
      status->fileLength[color=blue]
      > = 10; -- and not the second variable then filelength = 10 and fileposition[/color]
      =[color=blue]
      > 0, but if i set the second one only or both of them then they are[/color]
      fileLength[color=blue]
      > = 47244640266 and filePosition = 0; Like the memory is getting over[/color]
      wrote.[color=blue]
      > What is the correct way of passing a structure by reference on the Compact
      > Framework.
      >
      > here is my code
      >
      > In the C dll
      > -------------------------------------------
      >
      > ----------------in the header
      > struct FileStatus
      > {
      > long fileLength;
      > long filePosition;
      > };
      >
      > extern "C" __declspec(dlle xport) bool GetStatus (FileStatus* status);
      > ----------------
      >
      > ---------------in the cpp
      > bool GetStatus (FileStatus* status)
      > {
      > status->fileLength = 10;
      > status->filePosition = 11;
      > return true;
      > }
      > --------------------------------------------
      >
      > int my C# code i have
      > --------------------------------------------
      > namespace Controls
      > {
      > [StructLayout(La youtKind.Sequen tial)]
      > public struct SStatus
      > {
      > public long fileLength;
      > public long filePosition;
      > }
      > public class VitoSoundExplor er
      > {
      >
      > [DllImport("Test Dll.dll", EntryPoint="Get Status")]
      > public static extern bool GetStatus(ref SStatus status);
      >
      >
      > public bool GetStatus()
      > {
      > SStatus st= new SStatus();
      > GetStatus(ref st);
      > return true;
      > }
      > }
      > }
      > ------------------------------------------
      >
      > Any Ideas
      >
      > thanks
      >
      >[/color]


      Comment

      Working...