Hi All,
I'm new to C# and .NET but am required at the moment to be coding some
fairly advanced stuff and would love some help with this problem -
specifically...
I'm using C# to wrap a C library provided by a third party, to which I do
not have the source. I've been successful in wrapping most functions in the
library but am encountering a problem with a function that is expecting to
call back into the managed environment. Within the callback, I'm required
to call out to unmanaged code again, in this case to the fread() function
in the C library. The buffer into which fread() should write is passed into
the callback by the function that's initially call from the managed
environment. The callback is invoked successfully and the call to fread()
from the callback executes properly but the function called initially is
failing after the return from the callback and provides me with a cryptic
(and otherwise undocumented...) "internal error" code.
My best guess right now is that I'm somehow screwing up the declaration of
the buffer parameter that's passed into the callback and subsequently on to
fread() and/or I'm not taking into consideration the persistence issues
associated with the buffer allocated by the library function and passed
into my callback. Here are the relevant parts of the code:
// Delegate for callback:
public delegate int ReadCallbackDelegate(IntPtr destinationBuffer, int
numberOfBytes, IntPtr filePtr);
// 3rd Pary Library function declaration (ordinal used for entry point).
// IntPtr returned from this function is the handle to a resource built
// from the data in the file.
[DllImport("ThirdParty.dll", EntryPoint="#17")]
public static extern IntPtr tpLoadResourceFromFile(IntPtr aHandle,
ReadCallbackDelegate callback, IntPtr filePtr);
// fread() declaration:
[DllImport("msvcrt.dll")]
public static extern int fread(IntPtr buffer, int size, int count, IntPtr
file);
// ReadCallback definition. buffer is supplied by the
// (unmanaged) caller:
public static int ReadCallback(IntPtr buffer, int numberOfBytes, IntPtr
filePtr)
{
return fread(buffer, 1, numberOfBytes, filePtr);
}
// finally, excerpt from method that calls tpLoadResourceFromFile:
...
// This works ok (declaration not shown above)
IntPtr filePtr = fopen(resourceFileName, "rb");
ReadCallbackDelegate rcbDelegate = new ReadCallbackDelegate(ReadCallback);
GC.KeepAlive(ioCallback);
IntPtr resourceHandle = tpLoadResourceFromFile(aHandle, rcbDelegate,
filePtr);
// find out what happened...
if (resourceHandle == IntPtr.Zero) {
Console.WriteLine("failed building resource");
// call another function to retrieve last error...
}
else
Console.WriteLine("succeeded in building resource");
...
Hopefully, the above provides enough to go on. Any thoughts or direction
you can provide would be much appreciated!
-- Ron
===================================
This list is hosted by DevelopMentor� http://www.develop.com
Some .NET courses you may be interested in:
NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls
View archives and manage your subscription(s) at http://discuss.develop.com