Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to pass a pointer of data to the managed side on a C++ wrapper

How to pass a pointer of data to the managed side on a C++ wrapper

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++data-structureshelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sinosoidal
    wrote on last edited by
    #1

    Hi, I'm doing a simple managed C++ class to grab data from a USB device and pass it to the C# side. This is my class: public __gc class Communication { private: LPVOID dataInBuffer; HANDLE _hFileHandle; int nBytesToRead; int maxPacketSize; public: Communication() { _hFileHandle = INVALID_HANDLE_VALUE; maxPacketSize = 256; dataInBuffer = malloc(sizeof(char) * maxPacketSize); nBytesToRead = maxPacketSize; } ~Communication() { if( _hFileHandle != INVALID_HANDLE_VALUE ) { CloseHandle( _hFileHandle ); } } bool OpenDevice( String* driverName ) { char* lpFileName = new char[driverName->Length + 10]; sprintf( lpFileName, "\\\\?\\%s", driverName ); _hFileHandle = CreateFile((LPCSTR) lpFileName,GENERIC_READ,0,0,OPEN_EXISTING,0,0); if( _hFileHandle == INVALID_HANDLE_VALUE ) { puts("Couldn't not open device"); } delete lpFileName; return( _hFileHandle != INVALID_HANDLE_VALUE ); } BOOL CloseDevice() { return CloseHandle( _hFileHandle ); } int GetPacket() { char outBuffer[256]; int nBytesRead = -1; if (_hFileHandle!=INVALID_HANDLE_VALUE) { ReadFile(_hFileHandle, dataInBuffer, nBytesToRead, (PULONG) &nBytesRead,0); } memset(outBuffer,0,maxPacketSize*sizeof(char)); memcpy(outBuffer,dataInBuffer,maxPacketSize*sizeof(char)); return nBytesRead; } }; My problem right now it how to pass the outBuffer variable in the GetPacket method outside the class. If I let the variable being declared inside the function, it gets erase when the function ends and my data goes with it. If I declare that variable in the class itself, the compiler says the following: 'outBuffer' : must explicitly specify __gc or __nogc for an array declared in a managed type And if I put the __gc attribute on the variable, then the compiler tells the following: 'identifier' : '__gc' can only be applied to a class, struct, interface, array or pointer So... what do I have to do in order to make this variable available in the C# side? I'm using char, because I need a signed 8-bit value on each position of the array. Can anyone give me some lights here? Thanks, Nuno

    N A 2 Replies Last reply
    0
    • S sinosoidal

      Hi, I'm doing a simple managed C++ class to grab data from a USB device and pass it to the C# side. This is my class: public __gc class Communication { private: LPVOID dataInBuffer; HANDLE _hFileHandle; int nBytesToRead; int maxPacketSize; public: Communication() { _hFileHandle = INVALID_HANDLE_VALUE; maxPacketSize = 256; dataInBuffer = malloc(sizeof(char) * maxPacketSize); nBytesToRead = maxPacketSize; } ~Communication() { if( _hFileHandle != INVALID_HANDLE_VALUE ) { CloseHandle( _hFileHandle ); } } bool OpenDevice( String* driverName ) { char* lpFileName = new char[driverName->Length + 10]; sprintf( lpFileName, "\\\\?\\%s", driverName ); _hFileHandle = CreateFile((LPCSTR) lpFileName,GENERIC_READ,0,0,OPEN_EXISTING,0,0); if( _hFileHandle == INVALID_HANDLE_VALUE ) { puts("Couldn't not open device"); } delete lpFileName; return( _hFileHandle != INVALID_HANDLE_VALUE ); } BOOL CloseDevice() { return CloseHandle( _hFileHandle ); } int GetPacket() { char outBuffer[256]; int nBytesRead = -1; if (_hFileHandle!=INVALID_HANDLE_VALUE) { ReadFile(_hFileHandle, dataInBuffer, nBytesToRead, (PULONG) &nBytesRead,0); } memset(outBuffer,0,maxPacketSize*sizeof(char)); memcpy(outBuffer,dataInBuffer,maxPacketSize*sizeof(char)); return nBytesRead; } }; My problem right now it how to pass the outBuffer variable in the GetPacket method outside the class. If I let the variable being declared inside the function, it gets erase when the function ends and my data goes with it. If I declare that variable in the class itself, the compiler says the following: 'outBuffer' : must explicitly specify __gc or __nogc for an array declared in a managed type And if I put the __gc attribute on the variable, then the compiler tells the following: 'identifier' : '__gc' can only be applied to a class, struct, interface, array or pointer So... what do I have to do in order to make this variable available in the C# side? I'm using char, because I need a signed 8-bit value on each position of the array. Can anyone give me some lights here? Thanks, Nuno

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      You are using the obsolete Managed C++ syntax. Are you using a compiler that don't support C++/CLI? If not, I would suggest you to rewrite this in C++/CLI. :)

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • S sinosoidal

        Hi, I'm doing a simple managed C++ class to grab data from a USB device and pass it to the C# side. This is my class: public __gc class Communication { private: LPVOID dataInBuffer; HANDLE _hFileHandle; int nBytesToRead; int maxPacketSize; public: Communication() { _hFileHandle = INVALID_HANDLE_VALUE; maxPacketSize = 256; dataInBuffer = malloc(sizeof(char) * maxPacketSize); nBytesToRead = maxPacketSize; } ~Communication() { if( _hFileHandle != INVALID_HANDLE_VALUE ) { CloseHandle( _hFileHandle ); } } bool OpenDevice( String* driverName ) { char* lpFileName = new char[driverName->Length + 10]; sprintf( lpFileName, "\\\\?\\%s", driverName ); _hFileHandle = CreateFile((LPCSTR) lpFileName,GENERIC_READ,0,0,OPEN_EXISTING,0,0); if( _hFileHandle == INVALID_HANDLE_VALUE ) { puts("Couldn't not open device"); } delete lpFileName; return( _hFileHandle != INVALID_HANDLE_VALUE ); } BOOL CloseDevice() { return CloseHandle( _hFileHandle ); } int GetPacket() { char outBuffer[256]; int nBytesRead = -1; if (_hFileHandle!=INVALID_HANDLE_VALUE) { ReadFile(_hFileHandle, dataInBuffer, nBytesToRead, (PULONG) &nBytesRead,0); } memset(outBuffer,0,maxPacketSize*sizeof(char)); memcpy(outBuffer,dataInBuffer,maxPacketSize*sizeof(char)); return nBytesRead; } }; My problem right now it how to pass the outBuffer variable in the GetPacket method outside the class. If I let the variable being declared inside the function, it gets erase when the function ends and my data goes with it. If I declare that variable in the class itself, the compiler says the following: 'outBuffer' : must explicitly specify __gc or __nogc for an array declared in a managed type And if I put the __gc attribute on the variable, then the compiler tells the following: 'identifier' : '__gc' can only be applied to a class, struct, interface, array or pointer So... what do I have to do in order to make this variable available in the C# side? I'm using char, because I need a signed 8-bit value on each position of the array. Can anyone give me some lights here? Thanks, Nuno

        A Offline
        A Offline
        amatecki
        wrote on last edited by
        #3

        Hi, You could try something like this:

        // CnvBuffLib.h

        #pragma once

        #include <memory.h>;
        #include <string.h>;

        using namespace System;

        namespace CnvBuffLib
        {
        public __gc class ConvertBuffer
        {
        const char __nogc * const _buffer;
        static const int bsize = 256;

            public:
            ConvertBuffer()
            : \_buffer(new char \_\_nogc \[bsize\])
            {
            }
            
            ~ConvertBuffer()
            {
                delete \[\] \_buffer;
            }
            
            int GetPacket()
            {
                // sample of source data
                char tmp\[\] = {'t', 'e', 's', 't'};
                int tmplen = sizeof(tmp);
                
                // clear buffer
                char \_\_nogc \* \_bp = const\_cast<char\*>(\_buffer);
                \_bp = static\_cast<char\*>(memset(\_bp, 0, bsize));
        
                // copy data to buffer
                memcpy(\_bp, tmp, tmplen);
                
                return tmplen;
            }
        
            // this function copies native buffer to managed buffer (C# side)
            SByte GetData() \_\_gc \[\]
            {
                int rlen = strlen(\_buffer);
                if(rlen == 0)
                return 0;
                
                SByte result\[\] = new SByte\[rlen\];
                char \_\_pin \* presult = &result\[0\];
                
                memcpy(presult, \_buffer, rlen);
                
                return result;
            }
        };
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups