Dear all,
I am using a Delegate to callback from a C dll which
takes in input a function pointer. The Delegate is
called back and the struct is filled properly, but
just after the delegate function finishes, the
application crashes. "Memory cannot be written" The
application referenced memory at "fff000" ...
The Post routine (delegate) takes in an argument x25doneinfo
which belongs to the unmanaged code (I am NOT passing it to the
delegate call), it is just returned whenever the post function is
called.
can this be a problem (as I am not allocating memory to this
structure).
This structure is not defined in the unmanaged code as well (The
network driver returns this structure)
I want to use this structure as it contains the network packet data.
I am working on this problem for quite some days now so
will be grateful if someone can help me with this.
The C# code is like this :
class Networking
{
public unsafe delegate void PostDelegate(ref
DataStructures.x25doneinfo doneInfo);
public PostDelegate post;
[DllImport("Networking.dll")]
public static extern unsafe int recvPacket(int
iCid,IntPtr Buffer, int PacketSize, ref int iInfo,
[MarshalAs(UnmanagedType.FunctionPtr)] Delegate
PostDelegate);
void Recvpacket() {
while (1)
{
post = new PostDelegate(postFunction);
//Calling the C dll function with the Delegate
if( recvPacket(FormMain.callportNumber,cpRecvBuffer,
PACKET_SIZE, ref iInfo, post) < 0 )
{
MessageBox.Show("recv error");
}
}
}
public unsafe void postFunction(ref
DataStructures.x25doneinfo DoneInfo)
{
string error = "";
char[] mapBuff = new char[1050];
this.doneInfo = DoneInfo;
try
{
// process the packet
}
}---->>> after this point the application CRASHES
}
with the above error. Please see if you can help.
Thanks,
Gaurav
Robert Jordan - 03 Jul 2004 21:21 GMT
> I am using a Delegate to callback from a C dll which
> takes in input a function pointer. The Delegate is
> called back and the struct is filled properly, but
> just after the delegate function finishes, the
> application crashes. "Memory cannot be written" The
> application referenced memory at "fff000" ...
this might be a calling convention issue.
the C-DLL is probably expecting a "cdecl" callback.
if you own the sources of that DLL then change the
calling convention to WINAPI.
if not, take a look at
http://groups.google.com/groups?selm=ew6mLkV6BHA.1932%40tkmsftngp03
rob