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

Unmanaged code into C#

Hi,
Please help with the following:
How to implement a C++ code like this:
MediaFunction( char* filename,
void* info,
int* size,
void** buffer,
int first,
int* last);

into a C# syntax? (via Delegate - how?)
MediaFunction is a callback function.
Please provide some explanatory code. Thank you!


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
5 1650
Saya <an*******@devdex.com> wrote in news:usdZnY1MEHA.1156
@TK2MSFTNGP09.phx.gbl:
How to implement a C++ code like this:
MediaFunction( char* filename,
void* info,
int* size,
void** buffer,
int first,
int* last);

into a C# syntax? (via Delegate - how?)
MediaFunction is a callback function.
Please provide some explanatory code. Thank you!


You havent given us enough info. Do you only want to translate the arguments?
The only tricky part is the buffer, you'll need to rethink that. It depends
on your use of hte function.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 16 '05 #2
Saya,

If you want to transform this into a callback function, you have to
declare the delegate in the following manner (by the way, this assumes a
return type of void, since you didn't specify one):

public delegate void MediaFunction(
[MarshalAs(UnmanagedType.LPStr)] string filename,
IntPtr info,
ref int size,
ref IntPtr buffer,
int first,
ref int last);

Once you have that, you will have to marshal the info and buffer
arguments yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Saya" <an*******@devdex.com> wrote in message
news:us**************@TK2MSFTNGP09.phx.gbl...
Hi,
Please help with the following:
How to implement a C++ code like this:
MediaFunction( char* filename,
void* info,
int* size,
void** buffer,
int first,
int* last);

into a C# syntax? (via Delegate - how?)
MediaFunction is a callback function.
Please provide some explanatory code. Thank you!


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Nicholas, GREAT to have helping people like you around! (Kudzu, also
thanks for responding).

I've now made a 'giant' leap in my C# code (I'm new to it!!): the
callback is now invoked properly and data read from the file is also put
correctly into the specified buffer.

I'm still struggling though ...: the code-line calling the unmanaged
stuff ultimately ends up in an exception error:
"System.NullReferenceException: Object reference not set to an instance
of an object". Now this is heavy stuff for me ....!
I'm not sure you are still willing to help me, but in case of a yes, I
attached the relevant code hereafter.
My C#-app is using a third party legacy-DLL, one of the functions is a
ordinary OpenFile command:
------------------------------------------------------------
the Library information of MC_Open_File: int MC_Open_File (
int AppID,
int FileID,
void* info,
int (*YourCallbackFunction)
I did the following C# implementation of the above: [DllImport("mc3adv.dll")]
public static extern int MC_Open_File(
int AppID,
int FileID,
CBinfo cbi,
MediaToFileDelegate MediaFunction);
returning from this call gives the error; the MediaFunction callback seems to function (after implementing Nicholas' delegate code). CBinfo is a struct of the following (unmanaged) kind: typedef struct CALLBACKINFO
{
FILE* stream;
int messageID;
unsigned char buffer[8*1024];
} CBinfo;
which I rewrote into C#: [StructLayout(LayoutKind.Sequential)]
public class CBinfo
{
public int stream;
public int messageID;
public byte[] buffer;
}
MC_OpenFile is then called up as follows in C#: CBinfo cbi = new CBinfo();
cbi.buffer = new byte[8*1024];

try
{
mcStatus=MC_Open_File(
nAppID,
nFileID,
cbi,
new MediaFunctionDelegate(MediaFunction));
}
> EXCEPTION error <<<<<<

------------------------------------------------------------ 'hope I've given enough information. Meanwhile, thank you for your

time.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Saya,

I think it is the declaration of the callback info structure that is
giving you a problem. It should be:

[StructLayout(LayoutKind.Sequential)]
public class CBinfo
{
public IntPtr stream;
public int messageID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8192)]
public byte[] buffer;
}

This tells the marshaler to include the array as part of the structure,
instead of treating it as a pointer. Also, when passing pointers across the
managed/unmanaged boundary, you should use IntPtr to represent the native
pointer size on the machine that is running the code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Saya" <an*******@devdex.com> wrote in message
news:uO**************@tk2msftngp13.phx.gbl...
Nicholas, GREAT to have helping people like you around! (Kudzu, also
thanks for responding).

I've now made a 'giant' leap in my C# code (I'm new to it!!): the
callback is now invoked properly and data read from the file is also put
correctly into the specified buffer.

I'm still struggling though ...: the code-line calling the unmanaged
stuff ultimately ends up in an exception error:
"System.NullReferenceException: Object reference not set to an instance
of an object". Now this is heavy stuff for me ....!
I'm not sure you are still willing to help me, but in case of a yes, I
attached the relevant code hereafter.
My C#-app is using a third party legacy-DLL, one of the functions is a
ordinary OpenFile command:
------------------------------------------------------------
the Library information of MC_Open_File: int MC_Open_File (
int AppID,
int FileID,
void* info,
int (*YourCallbackFunction)
I did the following C# implementation of the above: [DllImport("mc3adv.dll")]
public static extern int MC_Open_File(
int AppID,
int FileID,
CBinfo cbi,
MediaToFileDelegate MediaFunction);
returning from this call gives the error; the MediaFunction callback seems to function (after implementing Nicholas' delegate code). CBinfo is a struct of the following (unmanaged) kind: typedef struct CALLBACKINFO
{
FILE* stream;
int messageID;
unsigned char buffer[8*1024];
} CBinfo;
which I rewrote into C#: [StructLayout(LayoutKind.Sequential)]
public class CBinfo
{
public int stream;
public int messageID;
public byte[] buffer;
}
MC_OpenFile is then called up as follows in C#: CBinfo cbi = new CBinfo();
cbi.buffer = new byte[8*1024];

try
{
mcStatus=MC_Open_File(
nAppID,
nFileID,
cbi,
new MediaFunctionDelegate(MediaFunction));
}>> EXCEPTION error <<<<<< ------------------------------------------------------------ 'hope I've given enough information. Meanwhile, thank you for your

time.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5
Nicholas,
Again, thanks for the instructions - It did structurize things in the
right way for me although I'm still confronted with the annoying
NullReferenceException error!
The issue now is (I think ..) how to reach the buffer (fourth argument
in MediaFunction); like Kudzu pointed out, it is indeed a tricky part!
(void** buffer --> ref IntPtr buffer).

I'm now juggling in MSDN to get the translation correctly from 'void**'
into the 'Marshal.PtrToStructure' thing:

What I need: a code construction involving the MediaFunction callback:
public int MediaFunction(
string filename,
IntPtr info,
ref int size,
ref IntPtr buffer, <-------
int first,
ref int last);

I have to attach the incoming 'buffer' IntPtr to the CBinfo struct,
something like:
CBinfo cbi = Marshal.PtrToStructure(buffer, typeof(CBinfo));

Result of this line is a <undefined value> for cbi, at debug time ...
..
;-((
Regards.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6

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

Similar topics

4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.