How to pass a pointer of data to the managed side on a C++ wrapper
-
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 -
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, NunoYou 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
-
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, NunoHi, 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; } };
}