473,382 Members | 1,404 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,382 software developers and data experts.

Callbacks from unmanaged to managed C++

Hello,

at first, exuse if the following question is simple to solve, but i
normaly coding with C# and now have to use C++/CLI for one project.

My Problem is that i have to use a native c++ sdk to read pictures
from ip cameras. I get the native c++ decoder worked so far and now
try to send callbacks from unmanaged to managed code, to signal wenn a
picture is fully received.

To get a first view on that area i implemented a little test where i
extract a pointer from a delegate and sending the pointer to unmanaged
code. In the unmanaged function i use the callbackfunction (after
filling an array), jump back to to managed code and marshaling the
generated array to the managed world.
That little example is working fine in debugmode but as soon as i
running in releasemode the programm crashs and i dont know why.

So, at first my Code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #pragma unmanaged
  3. class umTest
  4. {
  5. private:
  6. unsigned char * data;
  7. public:
  8. void Caller(int size, void callback(int, unsigned char*))
  9. {
  10. data = new unsigned char[size];
  11. for(int i = 0; i < size; i++)
  12. data[i] = i;
  13. callback(size,data);
  14. };
  15.  
  16. };
  17. #pragma managed
  18.  
  19. #pragma managed
  20. using namespace System;
  21. using namespace System::Runtime::InteropServices;
  22.  
  23. public delegate void callback(int, unsigned char*);
  24.  
  25. ref class mTest
  26. {
  27. private:
  28. array<unsigned char^destination;
  29. int size;
  30.  
  31. public:
  32. mTest()
  33. {
  34. callback ^d = gcnew callback(this,&mTest::callTest);
  35. IntPtr ptr = Marshal::GetFunctionPointerForDelegate(d);
  36. umTest *umT = new umTest();
  37. [b]umT->Caller(5,
  38. static_cast<void (__cdecl *)(int, unsigned char*)>
  39. (ptr.ToPointer()));
  40. this->print();
  41. }
  42.  
  43. void callTest(int size, unsigned char* test)
  44. {
  45. this->size = size;
  46. destination = gcnew array<unsigned char>(size);
  47. Marshal::Copy(IntPtr(test),destination,0,size);
  48. delete[](test);
  49. test = NULL;
  50. this->print();
  51. };
  52.  
  53. void print()
  54. {
  55. for(int i = 0; i<size; i++)
  56. Console::WriteLine("destination[{0}]={1}",i,destination[i]);
  57. };
  58. };
  59.  
When it runs in release mode, the callbackfunction is normally called
but after finishing the function, he is called again. On the
Marshal::Copy(...)-Line then the programm is crashing without any
exception...

I hope someone of you find the mistake, i have no idea anymore...
Thanks in advance

Greets,

David
Nov 30 '07 #1
8 2249
Hi DavidT!

My Problem is that i have to use a native c++ sdk to read pictures
from ip cameras. I get the native c++ decoder worked so far and now
try to send callbacks from unmanaged to managed code, to signal wenn a
picture is fully received.
Normally callbacks to managed code are done via "gcroot" Template...
See:
http://groups.google.de/group/micros...22b5a25?hl=de&

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 1 '07 #2
On 1 Dez., 08:28, "Jochen Kalmbach [MVP]" <nospam-
Jochen.Kalmb...@holzma.dewrote:
Hi DavidT!
My Problem is that i have to use a native c++ sdk to read pictures
from ip cameras. I get the native c++ decoder worked so far and now
try to send callbacks from unmanaged to managed code, to signal wenn a
picture is fully received.

Normally callbacks to managed code are done via "gcroot" Template...
See:http://groups.google.de/group/micros...anguages.vc/ms...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
At first, thanks for your answer here and at the german c++-board. I
tried it now with gcroot but i have the problem (sorry, i realy new to
C++/CLI) that i can't set my managed class via a setter-method.

Here my code:

using namespace System;
using namespace System::Runtime::InteropServices;

#include <msclr\auto_gcroot.h>

ref class mClass
{
void callback()
{

};
};

class umClass
{
public:
msclr::auto_gcroot<mClass^mClass;

void setCallback()
{

};
};

What is the type of my setCallback parameter? mClass*? pointers on
unmanaged classes are not allowed in C++/CLI, are they? mClass^?
Trakinghandles also not available in native code. In your example it
absolutly clear, but what have i to do in the new C++/CLI?

The other Problem is, it doesnt matter what i try, i get all the time
the following error:

Fehler 2 error C2327: 'umClass::mClass': Ist kein Typname, nicht
statisch und kein Enumerator
19 (engl. mClass is no typename, not statical and no enum)

What is my mistake?

Thanks again,

David
Dec 1 '07 #3
Hi DavidT!
>Normally callbacks to managed code are done via "gcroot" Template...
See:http://groups.google.de/group/micros...anguages.vc/ms...
ref class mClass
{
void callback()
{

};
};

class umClass
{
public:
msclr::auto_gcroot<mClass^mClass;

void setCallback()
{

};
};
void setCallback(mClass^ o)
{
mClass = o;
}

and

void doCallback()
{
mClass ^o;
o->callback();
}
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 1 '07 #4
void doCallback()
{
mClass ^o;
o->callback();
}
Upps... should be:

class umClass
{
public:
msclr::auto_gcroot<mClass^_m;

void setCallback(mClass^ o)
{
_m = o;
};
void doCallback()
{
mClass ^o;
o = _m;
o->callback();
}
};

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 1 '07 #5
On 1 Dez., 15:23, "Jochen Kalmbach [MVP]" <nospam-
Jochen.Kalmb...@holzma.dewrote:
Hi DavidT!
Normally callbacks to managed code are done via "gcroot" Template...
See:http://groups.google.de/group/micros...anguages.vc/ms...
ref class mClass
{
void callback()
{
};
};
class umClass
{
public:
msclr::auto_gcroot<mClass^mClass;
void setCallback()
{
};
};

void setCallback(mClass^ o)
{
mClass = o;

}

and

void doCallback()
{
mClass ^o;
o->callback();

}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
i splitted up both classes into different files.

When i do your changed i get the same error (your german, so i keep
translation:
Fehler 3 error C2327: 'umClass::mClass': Ist kein Typname, nicht
statisch und kein Enumerator 11

Fehler 4 error C2065: 'o': nichtdeklarierter Bezeichner 18

Fehler 5 error C2227: Links von "->callback" muss sich ein Zeiger auf
Klassen-/Struktur-/Union-/generischen Typ befinden. 19

Is there something wrong with my vs settings?!?

Thanks a lot,

David
Dec 1 '07 #6
Fehler 3 error C2440: '=': 'msclr::auto_gcroot<_element_type>' kann
nicht in 'mClass ^' konvertiert werden 18
o = _m.get(); <- is it this?

Sorry, i don't wanna get you on your nerves but i absolutely running
out of concentration after weeks trying to pass this (obviously
simple) problem...

Thanks,

David
Dec 1 '07 #7
Ok, it's working now! Thanks a lot, i still dunno where my initial
mistake was but thats not sooo important rigth now :)

A addition question to you, if allowed:

When i produce a unsigned char array in my native class and want to
copy the whole array to the unmanaged world and send it to a
memorystream, is it enough to submit the pointer from umngt to mngt an
copy the data to the stream? Or have i to marshal it first du a mngt-
array and then send to the memory-stream?

Thanks,

David
Dec 1 '07 #8
Hi DavidT!
When i produce a unsigned char array in my native class and want to
copy the whole array to the unmanaged world and send it to a
memorystream, is it enough to submit the pointer from umngt to mngt an
copy the data to the stream? Or have i to marshal it first du a mngt-
array and then send to the memory-stream?
You mean somthing like this:

array<Byte^arr;
Then you can pin it...

array<Byte^bDst = gcnew array<Byte>(10);
pin_ptr<BytepDstPinned = &bDst[0];

Then you can use the "pinned" array and access it like a native array...

For example:

#using <mscorlib.dll>
#include <memory.h>

#include <mscoree.h>
using namespace System;

int main()
{
array<Byte^bDst = gcnew array<Byte>(10);

unsigned char *pSrc = new unsigned char[10];
for(size_t i=0; i<10; i++)
pSrc[i] = i;
{
pin_ptr<BytepDstPinned = &bDst[0];
memcpy(pDstPinned, pSrc, 10);
}

for each (Byte b in bDst)
Console::WriteLine(b);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 1 '07 #9

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

Similar topics

1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
23
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
4
by: R. MacDonald | last post by:
Hello, all, I have a .NET application (VB) that passes the address of a delegate to unmanaged code in a DLL. The unmanaged code then uses the delegate as a call-back. This seems to work...
2
by: cada0310 | last post by:
Hi there, I'm new to C# (but not to C++), and I'm trying to create a web service that calls some of our older software, located in a DLL. I'm getting most the calls to work ok, but one of them...
2
by: Spyder | last post by:
I am trying to wrap a very old dll from Epson with a Managed Dll. Their declaration for the callback is this: typedef int (WINAPI* DLL_BiSetStatusBackFunction)(int, int (CALLBACK EXPORT...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
3
by: kelvin.koogan | last post by:
I have an unmanaged DLL which calls my managed C++ app using a callback (standard C++ function pointer). I can receive the callbacks OK using a static function with global scope, but I can't find a...
1
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.