Help port C++ to C#

  • Thread starter Thread starter Russ Du Preez
  • Start date Start date
R

Russ Du Preez

Hi All,

I am struggling trying to port an API written in c++ to c#. I would
appreciate a little help.

The header file follows:

// API function declarations
__declspec(dllimport) int DiscoverBTDevices(TCHAR&, int);

-------------------------------------------------------------------------------------------------------------
C++ code to call the function follows:

int DiscoverBTDevices(BINQ_STRUCT *deviceArray, int timeoutSecs );

typedef struct
{
char address[21];
char name[20];
}BINQ_STRUCT;

The *deviceArray should be large enough for the discovery of 10
devices. For example:
BINQ_STRUCT *pDeviceDetailsArray = NULL;
BINQ_STRUCT deviceDetailsArray[10];
pDeviceDetailsArray = deviceDetailsArray;

Hope this makes sense to some of you out there.

Thanks,
Russ
 
This one is not a simple one. Unless someone already has it written and is
willing to "donate" it to your cause, you're got some heavy lifting to do.

Basically you need to manually marshal the struct as a fixed byte array of
41 bytes, then use the Encoding.ASCII class to turn it into a string. And
since the API needs a pointer to an array of these, you'll need to use a
GCHandle (though I wouldn't use it if you have any hope of compatibility
between CF1 and CF2), an unsafe code block, or a P/Invoke to LocalAlloc
which you must track and free or you'll get memory leaks. Make sure
whatever you allocate is pinned so the GC can't move it during compaction as
well.
 
Back
Top