473,385 Members | 1,168 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,385 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 1438
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Andrew Cumming | last post by:
I am having trouble using Platform Invoke to call C++ DLLs from C# windows applications. The DLLs seem to load, but I get run-time errors to the effect that the named entry points cannot be found....
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
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...
4
by: repstat | last post by:
Hi I have a project which is going to be doing some string manipulation which needs to be pretty fast. The user interface is going to be written in C#. I am going to write the string handling...
4
by: Troy | last post by:
Hi, I am working on a mixed native/managed project involving DirectShow and am trying to decide how the modules should be partitioned. I am trying to understand the true affect of #pragma...
12
by: doug | last post by:
I understand the basics of what managed code offers and that you open yourself up to security issues if you allow unmanaged code. We already have a decent amount of VB6 code to include COM DLLs. ...
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
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...
4
by: =?Utf-8?B?dmlwZXJ4MTk2Nw==?= | last post by:
We are having an issue with an application we are developing. We have a Legacy COM DLL in C++ that we have converted to Visual Studio 2008. This COM DLL has methods that are calling Managed C#...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.