By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,420 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,420 IT Pros & Developers. It's quick & easy.

PInvoke with struct of function pointers and memcpy, delete[] etc.

P: n/a
Hi developers.

Im trying to migrate a C++ sample from the Canon SDK Version 7.1 into
C#. I have come across lots of pit falls (and luckily solved them) but
now Im stuck with some of the nice features of C++.

Now here is the following C++ code that I try to migrate. I thought
about defining all the following function prototypes into delegates
and put them into the struct.

================================================== ===========================

typedef void cdSTDCALL cdSOpen(cdContext contextH, cdPermission,
cdError* err);
typedef void cdSTDCALL cdSClose(cdContext contextH, cdError* err);
typedef void cdSTDCALL cdSRead(cdContext contextH, void* buf,
cdUInt32* bufsize, cdError* err);
typedef void cdSTDCALL cdSWrite(cdContext contextH, const void *buf,
cdUInt32* bufsize, cdError *err);
typedef void cdSTDCALL cdSSeek(cdContext contextH, cdWhence, cdInt32
offset, cdError* err);
typedef cdInt32 cdSTDCALL cdSTell(cdContext contextH, cdError* err);
/* cdStream */
typedef struct {
cdContext contextH;
/* stream I/O function pointers */
cdSOpen* open;
cdSClose* close;
cdSRead* read;
cdSWrite* write;
cdSSeek* seek;
cdSTell* tell;
} cdStream;
================================================== ===========================

So, that should work, right?!?!

Here comes the function which uses a lot of pointers, "memcpy" and
above all "delete[]". How shall I do this in C#?

Has anybody a suggestion?

================================================== ===========================
/* The WRITE function registered into a stream */
void cdSTDCALL cdSWritecdContext contextH, const void *pBuf, cdUInt32
*pBufsize, cdError *pErr )
MemStreamData *pMemStrm;
cdChar *cpNewBuffer;

*pErr = cdOK;
/* It investigates whether data required for processing is
pMemStrm = (MemStreamData*)contextH;
if( !pMemStrm )

if( !(pMemStrm->mode&MODE_MEMSTREAM_OPEN) )
else if( !(pMemStrm->mode&MODE_MEMSTREAM_WRITE) )

/* It investigates whether there is any enough buffer. */
if( pMemStrm->dwBufferSize < (pMemStrm->lPos+(*pBufsize)) )
/* A buffer is created newly and data is saved. */
cpNewBuffer = new cdChar[pMemStrm->lPos+(*pBufsize)];
if( cpNewBuffer == NULL )
/* Data is saved as much as possible. */
memcpy( pMemStrm->cpBuffer+pMemStrm->lPos, pBuf,
*pBufsize = pMemStrm->dwBufferSize-pMemStrm->lPos;
pMemStrm->lPos = pMemStrm->dwBufferSize;

memcpy( cpNewBuffer, pMemStrm->cpBuffer, pMemStrm->lPos );
memcpy( cpNewBuffer+pMemStrm->lPos, pBuf, *pBufsize );

delete []pMemStrm->cpBuffer;
pMemStrm->cpBuffer = cpNewBuffer;
pMemStrm->dwBufferSize = pMemStrm->lPos+(*pBufsize);
pMemStrm->lPos = pMemStrm->lPos+(*pBufsize);
memcpy( pMemStrm->cpBuffer, pBuf, *pBufsize );
pMemStrm->lPos += (*pBufsize);

if( pMemStrm->dwVisibleSize < (cdUInt32)pMemStrm->lPos )
pMemStrm->dwVisibleSize = pMemStrm->lPos;


================================================== ===========================

Please help me!

Thanks and kind regards
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.