473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to exchange a structure between c++/cli and c, c++

hello everyone.

i have made a dll of c++/cli, then i want to exchange structure between
c++/cli and c.

this is the structure
typedef struct tagExchange
{
int m_nIndex;
float m_fValue;
} exchagned;

first , c will use the dll made by c++/cli.
then c will pass a function pointer to c++/cli like callback function.
because i want to use the callback like event.

typedef void (*EXCHANGED)(int nCount, exchagned stIndex[]);// callback
function.

static void NotifyToC(System::Object ^sender, Dictionary<int, _CRequested^>
^requested)
{
int nLength = requestedIndex->Count;
array<exchagned *>^ arrIndex = gcnew array<exchagned; *>(nLength);
int nCount = 0;
_CRequested^ requested;
for each(KeyValuePair<int, _CRequested^>^ pair in requested)
{
arrIndex[nCount] = new exchagned();
arrIndex[nCount]->m_nIndex = pair->Key;
requestedItem =(_CRequested^) pair->Value;
arrIndex[nCount]->m_fValue =requestedItem->m_fValue;
nCount++;
}
pin_ptr<exchagned*p1 = &arrIndex[0]; --

|-here is my problem.
// gCallBackofExchanged(nCount, arrIndex);--
}

so how can i solve this problem.
Jun 27 '08 #1
1 2154

"lightdoll" <dj******@naver.comha scritto nel messaggio
news:97**********************************@microsof t.com...
i have made a dll of c++/cli, then i want to exchange structure between
c++/cli and c.

this is the structure
typedef struct tagExchange
{
int m_nIndex;
float m_fValue;
} exchagned;

first , c will use the dll made by c++/cli.
If C must use the DLL made by C++/CLI, this DLL must have a *pure C*
interface (i.e. use extern "C", do not pass classes at interface boundaries,
do not use function overloads, etc.).

then c will pass a function pointer to c++/cli like callback function.
because i want to use the callback like event.

typedef void (*EXCHANGED)(int nCount, exchagned stIndex[]);// callback
function.
IMHO, this naming convention is not very good...
I would name the structure as EXCHANGED (instead of lower-case 'exchanged'),
and the call back with something different and more readable, e.g.
EXCHANGED_CALLBACK.

typedef struct tagExchange
{
int m_nIndex;
float m_fValue;
} EXCHANGED;

Moreover, I would remove the m_ prefix which is spurious in that context (it
does have sense in C++ classes, not in C structures like above).

So, the final version should be:

<code>

// C guard
#ifdef __cplusplus
extern "C" {
#endif

typedef struct tagExchange
{
int nIndex;
float fValue;
} EXCHANGED, * PEXCHANGED;
// Call-back
typedef void (*EXCHANGED_CALLBACK)(int /* nCount */, PEXCHANGED /* stIndex
*/ );

#ifdef __cplusplus
}; // extern "C"
#endif

</code>

static void NotifyToC(System::Object ^sender, Dictionary<int,
_CRequested^>
^requested)
{
int nLength = requestedIndex->Count;
array<exchagned *>^ arrIndex = gcnew array<exchagned; *>(nLength);
|-here is my problem.
// gCallBackofExchanged(nCount, arrIndex);
I think that the problem is that you are passing a *managed* array
(allocated using gcnew in the managed heap), to a language (C) that has no
idea about managed heaps.

I'm not sure (you should test that...), but I think that you may consider
creating the array in the "normal" C/C++ heap, and try to pass pointer to
that to the C callback.

Something like this (using the new names I proposed above):

--[ I edited your code inline: ]--

<code>

static void NotifyToC(System::Object ^sender, Dictionary<int, _CRequested^>
^requested)
{
int nLength = requestedIndex->Count;

// array<exchagned *>^ arrIndex = gcnew array<exchagned; *>(nLength);
// Create array on normal heap, not managed heap, to pass to C callback

// Create a vector of nLength EXCHANGED itgems.
// You need to #include <vectorto use STL vector container.
std::vector< EXCHANGED arrIndex( nLength );

int nCount = 0;
_CRequested^ requested; // <--- initialized where?

for each(KeyValuePair<int, _CRequested^>^ pair in requested)
{
// arrIndex[nCount] = new exchagned(); <- REMOVE THAT

// You can use dot notation here:
arrIndex[nCount].nIndex = pair->Key;

requestedItem =(_CRequested^) pair->Value;
arrIndex[nCount].fValue =requestedItem->m_fValue;

// If you have some problems for buffer overruns,
// you may use arrIndex.at() instead of operator[];
// In fact, std::vector::at() is bounds-checked.

nCount++;
}

// Call C call-back
gCallBackofExchanged( nCount, &(arrIndex[0]) );

// No need to remove EXCHANGED array:
// std::vector destructor does that automatically.

}

</code>
HTH,
Giovanni

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: M Rao | last post by:
I have xml data coming in as a stream from a web service running against exchange server.The attributes for the elements dtstart and dtend, b:dt="dateTime.tz"...
19
by: steve | last post by:
// What I want to do Use enumerated types with the Interlocked.Exchange methods Suggestions please // My estimation of problem Seems like Interlocked.Exchange only works with ints,...
8
by: Rob Edwards | last post by:
When trying to add the Microsoft CDO for Exchange Management Library (aka CDOEXM.dll) I receive the following message: "A reference to 'Microsoft CDO for Exchange Management Library' could not be...
2
by: George Durzi | last post by:
We recently upgraded to Exchange2K3/W2K3 from Exchange2K/W2K, and some of my c# code that I used to access users' contacts using WebDAV has stopped working. I'm getting a 401 unauthorized error....
1
by: Eduardo Garcia-Prieto | last post by:
I have come accross a problem in using the Interlocked.Exchange(Object, Object) method while using Option Strict On in my project. I have a private class structure variable which can be updated...
1
by: ouistyty | last post by:
Hi We have a simple asp page that query LDAP attribrute. Everithing is working fine using a native domain account. but when using an external account we have an error 70, acces denie. Here's...
4
by: KDawg44 | last post by:
Hi, I am frustrated with my users who send large files around the office instead of using the network shares. For instance, this is one of many emails I have sent around: "If you take the...
2
by: Salad | last post by:
In A2003 one can create a linked table using File/GetExternalData/Link/Exchange(). It's a nice feature but by and large fairly useless if I am reading the table structure correctly. The From &...
1
by: =?Utf-8?B?bGlnaHRkb2xs?= | last post by:
hello everyone. i couldn't find out a board about c++/cli from , so i wrote my problem writing a code with c++/cli. i have made a dll of c++/cli, then i want to exchange structure between...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.