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

C# App: Error during callback from unmanaged to managed C++

1
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
0 1365

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sasha Nikolic | last post by:
I have one unmanaged and one managed class in the same vc++ project. Managed object creates one instance of unmanaged class and needs to pass a callback reference so that unmanaged class can...
13
by: DD | last post by:
I'm puzzled how to mix managed/unmanaged C++ in the following scenario: Unmanaged ------------ Callback listener class with method that should call an event in managed Form1. Initialization...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
11
by: James Crouch | last post by:
I am trying to create a managed c++ wrapper and have run into a problem is when SetNewNetworkCallBack is run. It doesn't like that NetworkID isn't an UInt16. I get the error: ...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
0
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
1
by: stillh2os | last post by:
Hello. I'm new to .NET, and I'm trying to implement a callback function. I want my managed C++ code to call an unmanaged function, passing in a callback function that the unmanaged C/C++ code...
1
by: Kenneth Porter | last post by:
I have an unmanaged library that can accept an instance of an abstract class and call back into it. I'm writing a wrapper for this library. How do I wrap a managed callback in a class derived...
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: 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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.