none
HOW CONVERT a IntPtr to Uint32? Somebody can help me?

    Question

  • HOW CONVERT  a IntPtr to Uint32?

    I like to convert a program from C# to C++ managed, but only ONE line I don`t know how convert it.

     

    THE CODE IN C# :

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using CyUSB;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    //........

    IntPtr asd= PInvoke.CreateEvent(0, 0, 0, 0);

    uint qwe = (uint)asd;       ////////////////////////////////////////////////////THIS LINE WORKS FINE///////////////////////////////////

    //.........

    }

    }

    }

     

    I WILL TO CONVERT TO C++ MANAGED. THE CODE IS:

    using namespace System;

    using namespace System::Collections::Generic;

    using namespace System::Linq;

    using namespace System::Text;

    using namespace CyUSB;

    namespace ConsoleApplication1

    {

    private ref class Program

    {

    static void Main(array<System:Tongue Tiedtring^> ^args)

    {

    //.................

    IntPtr asd= PInvoke::CreateEvent(0, 0, 0, 0);

    System::UInt32 qwe = safe_cast<System::UInt32>(asd); //////////DOESN`T WORK ////////////////////////////

    //.......

    }

    };

    }

     

    The compiler says ....cannot use 'safe_cast' to convert from 'System::IntPtr' to 'unsigned int'....

     I try others, but with not success.

    PLEASE, CAN SOMEBODY HELP ME?

    Thanks Marcel.

     

     

     

     

    Thursday, April 17, 2008 9:34 PM

Answers

  • That is dangerous code. IntPtr's size is platform dependent where UInt32 isn't. So you could end up with crashes here which you can't explain.

    Unless you have a good reason to, don't do something like this at all.

    What I would suggest is to use the members of the IntPtr class to do this kind of thing.

    IntPtr has a ToInt32 and ToInt64 member function, whats more, if the handle is 64 bit and you call ToInt32 it will not allow it to happen.

     

    Thursday, April 17, 2008 11:09 PM

All replies

  • That is dangerous code. IntPtr's size is platform dependent where UInt32 isn't. So you could end up with crashes here which you can't explain.

    Unless you have a good reason to, don't do something like this at all.

    What I would suggest is to use the members of the IntPtr class to do this kind of thing.

    IntPtr has a ToInt32 and ToInt64 member function, whats more, if the handle is 64 bit and you call ToInt32 it will not allow it to happen.

     

    Thursday, April 17, 2008 11:09 PM
  • IntPtr is not an opaque type, it is a struct that has methods that do what you need.

    Code Snippet

    IntPtr asd = PInvoke::CreateEvent(0, 0, 0, 0);
    unsigned int qwe = asd.ToInt32();

     

     

    Thursday, April 17, 2008 11:11 PM
  • Thanks,

    //..

    unsigned int qwe = asd.ToInt32();

    /...

    works well.

    But my program doesn´t work how the original in C#. I think that there are something else wrong.

     

    THE ORIGINAL PROGRAM IN C# IS:

    // This is a recursive routine for pinning all the buffers used in the transfer in memory.

    // It will get recursively called QueueSz times. On the QueueSz_th call, it will call

    // XferData, which will loop, transferring data, until the stop button is clicked.

    // Then, the recursion will unwind.

    public unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps)

    {

    // Allocate one set of buffers for the queue

    cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN + IsoPktBlockSize];

    xBufs[j] = new byte[BufSz];

    oLaps[j] = new byte[20];

    fixed (byte* tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) // Pin the buffers in memory

    {

    OVERLAPPED* ovLapStatus = (OVERLAPPED*)tL0;

    // IntPtr qwe =(PInvoke.CreateEvent(0, 0, 0, 0));

    ovLapStatus->hEvent = (uint)PInvoke.CreateEvent(0, 0, 0, 0);

    // Pre-load the queue with a request

    int len = BufSz;

    EndPoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]);

    j++;

    if (j < QueueSz)

    LockNLoad(ref j, cBufs, xBufs, oLaps); // Recursive call to pin next buffers in memory

    else

    XferData(cBufs, xBufs, oLaps); // All loaded. Let's go!

    }

    }

     

    AND MY CONVERSION TO C++ MANAGED (NOT CORRECT):

    // This is a recursive routine for pinning all the buffers used in the transfer in memory.

    // It will get recursively called QueueSz times. On the QueueSz_th call, it will call

    // XferData, which will loop, transferring data, until the stop button is clicked.

    // Then, the recursion will unwind.

    public:

    void LockNLoad(int %j, array<array<System::Byte>^> ^cBufs, array<array<System::Byte>^> ^xBufs, array<array<System::Byte>^> ^oLaps)

    {

    // Allocate one set of buffers for the queue

    cBufs[j] = gcnew array<System::Byte>(CyConst:Tongue TiedINGLE_XFER_LEN + IsoPktBlockSize);

    xBufs[j] = gcnew array<System::Byte>(BufSz);

    oLaps[j] = gcnew array<System::Byte>(20);

    pin_ptr <cli::array<System::Byte>^> tL0 = &oLaps[j];   //// I THINK THAT THIS LINE IS´NT CORRECT////////

    pin_ptr <cli::array<System::Byte>^> tc0 = &cBufs[j];

    pin_ptr <cli::array<System::Byte>^> tb0 = &xBufs[j];

    // pin_ptr* tL0 = oLaps[j];

    // pin_ptr tc0 = cBufs[j];

    // pin_ptr tb0 = xBufs[j];

    OVERLAPPED* ovLapStatus = (OVERLAPPED*)tL0;

    IntPtr asd= PInvoke::CreateEvent(0, 0, 0, 0);

    ovLapStatus->hEvent = asd.ToInt32(); //////////NEW LINE/////////

    // Pre-load the queue with a request

    int len = BufSz;

    EndPoint->BeginDataXfer(cBufs[j], xBufs[j], len, oLaps[j]);

    j++;

    if (j < QueueSz)

    LockNLoad(j, cBufs, xBufs, oLaps); // Recursive call to pin next buffers in memory

    else

    XferData(cBufs, xBufs, oLaps); // All loaded. Let's go!

    }

     

    THE APPLICATION is a test for a kit of development Board Cypress usb Cy7c68013a, for isochronous transfer.

    The application works but more slowly and with a lot of transfer failures than the original in C#.

    I think that the line

    pin_ptr <cli::array<System::Byte>^> tL0 = &oLaps[j];  

    is not correct.

    Thanks.

    If you view what is wrong, please, say me

    Marcel

     

    Friday, April 18, 2008 5:36 PM
  • Hi,

     

    Since your new question (The application works but more slowly and with a lot of transfer failures than the original in C#.) is not directly related to the original <HOW CONVERT a IntPtr to Uint32? Somebody can help me?> issue, it would be best if you open up a new thread for the new question. In this way, our discussion here will not deviate too much from the original issue. This will make answer searching in the forum easier and be beneficial to other community members as well.

     

    Thank you for your understanding.

     

    Wednesday, April 23, 2008 11:03 AM