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

C++ struct to C# With Delegate

3
I have a question. The struct below is used to handle the callback of a Unmannaged c++ dll.

Expand|Select|Wrap|Line Numbers
  1.       struct CALLBACK
  2.       {
  3.           typedef BOOL (*CALL_AP) (LPVOID pThis, GT_HANDLE hFrom, GT_ERROR Error, GT_CALLBACK_TYPE dwType, LPVOID pData);
  4.           BOOL IsVaild()  {
  5.               return (m_pCall != NULL);.
  6.           }
  7.           LPVOID        m_pThis;
  8.           CALL_AP    m_pCall;
  9.       };
  10.  
I tried to translate this struct but probabaly the problem will lie here in the tranalsation of the struct.

Expand|Select|Wrap|Line Numbers
  1.       [StructLayout(LayoutKind.Sequential)]
  2.           struct CALLBACK
  3.           {
  4.               public delegate bool CALL_AP(IntPtr pThis, UInt32 hFrom, GT_STATUS Status, GT_CALLBACK_TYPE dwType, IntPtr pData);
  5.               public bool IsVaild()
  6.               {
  7.                   return m_pCall != null;
  8.               }
  9.               public IntPtr m_pThis;
  10.               public CALL_AP m_pCall;
  11.           }
  12.  
but if i use this code the callback function will run 1 time and crash with a unhandled win32 exception
Aug 14 '08 #1
2 2681
Banfa
9,065 Expert Mod 8TB
By translate you mean you copy the function pointer returned by the DLL to the delegate?

I am reasonably sure that you just can't do that. You need to return to the C# application a .NET class or a C++/CLR defined delegate that can then make the call to the native function.

That is the native/managed interface has to remain in the C++ code, the C# must only have visibility of managed classes and functions.
Aug 14 '08 #2
Gezan
3
thnx for the quick reply. Yes by translate i meant just copy and translate the structure.

I am kind of new to C# . Can u give me an example of how i need to to post this structure to the dll. And let the delegate inside the structure handle the callback ?
Aug 14 '08 #3

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

Similar topics

4
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
6
by: Dan H. | last post by:
Hello, Is there any advantage of using a struct or a class as part of public delegate bool MyEvent(object source, SomeEventArgs e); Struct: public struct SomeEventArgs { public float...
2
by: Aviram Gilboa | last post by:
Hi, I have the following code defined: <StructLayout(LayoutKind.Sequential)> Public Structure Callbacks <MarshalAs(UnmanagedType.FunctionPtr)> Public OnError As OnErrorDelegate...
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
1
by: Vikas | last post by:
Hi I have been browsing C struct to C# mapping emails on the newsgroups, but I haven't been able to find a solution to my problem. My C structure looks like this: struct myCstruct { char *...
11
by: garyusenet | last post by:
I have 'cli via c# on order', and in the mean time am reading 'Pro C# 2005 and the .NET platform' (Andrew Troelson). I'm just reading about the 'five types defined in the CTS'. Specifically Struct....
5
by: not_a_commie | last post by:
It seems that the only way to construct a struct from a type is to use Activator.CreateInstance. Is that true? Can anyone improve (performance-wise) upon this function below: /// <summary>...
0
by: zman77 | last post by:
Hi. In C dll, there is a struct that contains an array of struct pointers. I do not know how to represent that in C#. That's my problem. Here is the relevant C code: struct vector { uint...
1
by: eastlands | last post by:
I need to use an unmanaged c++ dll which uses structs that contain callbacks and also functions. I have included the appropriate c++ definitials and my c# translations below. I first defined the...
0
by: Gezan | last post by:
Hi all, I have a question. The struct below is used to handle the callback of a Unmannaged c++ dll. struct CALLBACK { typedef BOOL (*CALL_AP) (LPVOID pThis, GT_HANDLE hFrom, GT_ERROR...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.