Results 1 to 5 of 5

Thread: [2.0] getting the first byte of a structure? HELP!

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    [2.0] getting the first byte of a structure? HELP!

    Hi all!
    I'm using COM.... I have a function from a COM object that takes a byte pointer, pointing to the first byte in a structure. My question is, how can i get a byte pointer that would point to the first byte in my structure?


    any help is mucho appreciated!
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] getting the first byte of a structure? HELP!

    You could either use unsafe code if you really must have a genuine pointer, or else the Marshal.StructureToPtr method should do the trick.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: [2.0] getting the first byte of a structure? HELP!

    Quote Originally Posted by jmcilhinney
    You could either use unsafe code if you really must have a genuine pointer, or else the Marshal.StructureToPtr method should do the trick.
    Hi! thanks man!
    I looked at that marshal function,
    i kinda lied actually, i needed a "ref byte" not a byte pointer, so I passed in the first field in the structure and it works fine....
    curious though, in an unsafe context how would you convert the structure to a byte* ?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] getting the first byte of a structure? HELP!

    I've actually never written any unsafe code in C# but I would expect it would be something like:
    CSharp Code:
    1. byte* p = &myStructure;
    That would do the trick in C++, although C# may have an issue with converting the address of one type to a pointer of another type. You may have to cast like this:
    CSharp Code:
    1. byte* p = (byte*)&myStructure;
    Also, I'm not 100% sure that the addressof operator (&) from C/C++ has carried over, but I'm guessing it has.

    Edit: Just checked and that second code snippet does the trick.
    Last edited by jmcilhinney; Oct 20th, 2007 at 03:20 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: [2.0] getting the first byte of a structure? HELP!

    Quote Originally Posted by jmcilhinney
    I've actually never written any unsafe code in C# but I would expect it would be something like:
    CSharp Code:
    1. byte* p = &myStructure;
    That would do the trick in C++, although C# may have an issue with converting the address of one type to a pointer of another type. You may have to cast like this:
    CSharp Code:
    1. byte* p = (byte*)&myStructure;
    Also, I'm not 100% sure that the addressof operator (&) from C/C++ has carried over, but I'm guessing it has.

    Edit: Just checked and that second code snippet does the trick.
    and why did i never think of that?
    thanks
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width