You are here:

C#/C Data Structure in C#

Advertisement


Question
I have a DLL that is written in C. I have to use it from within C# code.

Some of the available DLL functions must be implemented as callbacks. This means that when a given DLL function is called from C# managed code, the DLL unmanaged code will "callback" a method implemented in C# managed code.

For this purpose I have managed to use C# delegates with sucess for simple String datatypes (in C that would be a char *).

For example, the following code works just fine:

// Delegate
public delegate int OnCodeLine(String codeline);
[DllImport(DLL)]
private static extern int rdOnCodeLine(OnCodeLine codeline);

// Install the callback
public void Callback()
{
   OnCodeLine codeline = new OnCodeLine OnCodeLineImplementation);
   int rtnval = rdOnCodeLine(codeline);
}

// Implement the callback
public int OnCodeLineImplementation(String codeline)
{
   System.Console.WriteLine("Codeline Debug: " + codeline);
   return 1;
}

Things get out of hand when a given callback implementation must access a C complex data structure (defined has a struct).

The callback in question returns to the managed code a pointer to a data structure.

How can I acess the data stored inside the C data structure?

Please, if you require any detailed code just let me know.

Regards.

Joćo


Answer
Hi

I haven't really worked with Unsafe/Unmanaged code interact with C#

May be this article will help you on this..

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwl...

Happy programming!!

-srini

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Srini Nagarajan

Expertise

can answer any kind of questions in ASP.NET, C#, VB.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience

Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder

©2012 About.com, a part of The New York Times Company. All rights reserved.