471,107 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,107 software developers and data experts.

Odd unmanaged calling managed code issues

Hi all,

I'm a MS/.NET newbie, diving right into CLR interop. I've worked
through the various issues foreign to me as a UNIX guy: multiple heap
issues because I was linking to different CRT instances (ugh), calling
conventions, marshalling, etc. I'm stumped on one issue, though.

Quick structural overview: straight C dll wrapped by C# wrapper class.
C# wrapper provides the implementation for a number of function
callbacks via delegates that the C dll invokes. C# wrapper object is
in turn instantiated by a per-request handler thread. C dll is thread
safe.

The stumper: the first time I create the wrapper thread and try to
call the C dll, an exception is thrown when the C dll tries to call
the C# wrapper via a function pointer. The odd part is it's only the
first time -- the second and succeeding calls work just fine. I've
verified there is a function pointer by printing out the value.

C definition: char *(__stdcall *func)(void *, char *);
C# delegate: unsafe delegate string func(void *, sbyte *);

Via instrumenting the code, I'm certain the exception is being thrown
after the C code invokes the function call via the pointer, but before
control reaches my C# delegate. Usually I get:

Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.

but sometimes I get:

Unhandled Exception: System.Runtime.InteropServices.SEHException:
External component has thrown an exception.

Any ideas would be appreciated.

Thanks,
Bill
Nov 17 '05 #1
1 1354
we*****@rit.edu (Bill Soudan) wrote in message
Via instrumenting the code, I'm certain the exception is being thrown
after the C code invokes the function call via the pointer, but before
control reaches my C# delegate. Usually I get:

Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.

but sometimes I get:

Unhandled Exception: System.Runtime.InteropServices.SEHException:
External component has thrown an exception.


After much searching, turns out I wasn't saving references to my
delegates, so they were being deleted by the garbage collector while
still in use.

Bill
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Andrew Cumming | last post: by
5 posts views Thread by Barry Anderberg | last post: by
4 posts views Thread by Rachel Suddeth | last post: by
4 posts views Thread by repstat | last post: by
4 posts views Thread by Troy | last post: by
12 posts views Thread by doug | last post: by
1 post views Thread by MC-Advantica | last post: by
2 posts views Thread by Jon Slaughter | last post: by
4 posts views Thread by =?Utf-8?B?dmlwZXJ4MTk2Nw==?= | last post: by

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.