Wrapper C++/cli class

Hi,

I want to wrapper a my c++ class using Interop.
This class has a method WritePort with defined as  :

UINT WritePort (UINT BytesToWrite, UINT* pBytesWritten, BYTE* pWriteBuff, UINT WriteTimeout);

On my C++/Cli Class I've defined an wrapper method defines as :

int WrapperClass::Class1::WriteSerial(int BytesToWrite, int^ pBytesWritten, String^ WriteBuff, int WriteTimeout);

The code for this wrapper method is :

int WrapperClass::Class1::WriteSerial(int BytesToWrite, int^ pBytesWritten, String^ WriteBuff, int WriteTimeout)
{
UINT retval;
UINT ByteWritten;
BYTE *pWriteBuff;

unsigned char* uWritechars = (unsigned char*)(Marshal::StringToHGlobalAnsi(WriteBuff)).ToPointer();

pin_ptr<unsigned int>pLen = pBytesWritten;  // ERROR

retval = m_pCSerPortComm->WritePort(BytesToWrite,pLen,uWritechars,WriteTimeout);

Marshal::FreeHGlobal(IntPtr((void*)uWritechars));

return (int)retval;

}

But the build fail with the error :
Error 1 error C2440: 'initializing' : cannot convert from 'System::Int32 ^' to 'cli::pin_ptr<Type>' d:\develop\Projects\test\.net\WrapperClass\WrapperClass\class.cpp 57 

Can help me


 PS: sorry for my bad english.....

 



Answer this question

Wrapper C++/cli class

  • jph42

    Thanks for the help.

    I've changed :

    int WrapperClass::Class1::WriteSerial(int BytesToWrite, array<unsigned int>^ Written, array<char>^ WriteBuff, int WriteTimeout)

    {

    UINT retval;

    pin_ptr<unsigned int>pLen = &Written[0];

    pin_ptr<unsigned char>Writechars = &WriteBuff[0]; // ERROR HERE

    retval = m_pCSerPortComm->WritePort(BytesToWrite,pLen,Writechars,WriteTimeout);

    return (int)retval;

    }

    I've only one error :

    Error 1 error C2440: 'initializing' : cannot convert from 'cli::interior_ptr<Type>' to 'cli::pin_ptr<Type>'

    on the line marked above, why



  • ChasOnline

    When you see UINT* in native code, it's preferred to use an array<unsigned int> in managed code. And for BYTE*, use array<char>. Try again with that.

    steven64 wrote:

    Hi,

    I want to wrapper a my c++ class using Interop.
    This class has a method WritePort with defined as :

    UINT WritePort (UINT BytesToWrite, UINT* pBytesWritten, BYTE* pWriteBuff, UINT WriteTimeout);

    On my C++/Cli Class I've defined an wrapper method defines as :

    int WrapperClass::Class1::WriteSerial(int BytesToWrite, int^ pBytesWritten, String^ WriteBuff, int WriteTimeout);

    The code for this wrapper method is :

    int WrapperClass::Class1::WriteSerial(int BytesToWrite, int^ pBytesWritten, String^ WriteBuff, int WriteTimeout)
    {
    UINT retval;
    UINT ByteWritten;
    BYTE *pWriteBuff;

    unsigned char* uWritechars = (unsigned char*)(Marshal::StringToHGlobalAnsi(WriteBuff)).ToPointer();

    pin_ptr<unsigned int>pLen = pBytesWritten; // ERROR

    retval = m_pCSerPortComm->WritePort(BytesToWrite,pLen,uWritechars,WriteTimeout);

    Marshal::FreeHGlobal(IntPtr((void*)uWritechars));

    return (int)retval;

    }

    But the build fail with the error :
    Error 1 error C2440: 'initializing' : cannot convert from 'System::Int32 ^' to 'cli::pin_ptr<Type>' d:\develop\Projects\test\.net\WrapperClass\WrapperClass\class.cpp 57

    Can help me


    PS: sorry for my bad english.....



  • Edgar Au

    Perhaps I have been not clear :

    my problem is convert the unmanaged class param UINT* pBytesWritten to an managed syntax usable on a C# environment.

    I've tried with defining int^ pBytesWritten

    and pin_ptr<unsigned int>pLen = pBytesWritten but doesn't build ...

    Someone can help me

    Thanks.


  • wpfnewbee

    Thank's ,

    I've correct and now is OK,

    thanks awfully !!!!!!

    steve


  • Rob Roberts

    The array is of type char, but your pin_ptr uses unsigned char. They need to match.

    steven64 wrote:

    Thanks for the help.

    I've changed :

    int WrapperClass::Class1::WriteSerial(int BytesToWrite, array<unsigned int>^ Written, array<char>^ WriteBuff, int WriteTimeout)

    {

    UINT retval;

    pin_ptr<unsigned int>pLen = &Written[0];

    pin_ptr<unsigned char>Writechars = &WriteBuff[0]; // ERROR HERE

    retval = m_pCSerPortComm->WritePort(BytesToWrite,pLen,Writechars,WriteTimeout);

    return (int)retval;

    }

    I've only one error :

    Error 1 error C2440: 'initializing' : cannot convert from 'cli::interior_ptr<Type>' to 'cli::pin_ptr<Type>'

    on the line marked above, why




  • Wrapper C++/cli class