Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Errors when calling C++ dll from C#

Status
Not open for further replies.

Loradan

Programmer
Jun 21, 2006
28
US
Hello,

I'm working with a C++ dll that processes files on the hard drive. When I call the dll from within a C++ console app, everything works beautifully.

I tried to execute it from a C# application and I can't figure out what's wrong. I worked through all of the MarshalAs attributes and verified that the parameters being passed in were correct. I also exposed a duplicate function that performed the same activities except the parameters were hard-coded. I'm getting the exact same issues as before (runs in c++ console, not in C# windows app).

The part that seems to be having a problem is when the dll tries to read an image file into memory. It can read a text file with no problem.

Is there some type of environment setting that could be causing this??? I've been searching the net for several days now and haven't been able to find anything that's similar to this.
 
1) Check your .cfg file
2) Do you have a managed interface to your DLL? You probably need to go C# -> managed C++ -> unmanaged C++. You cannot go directly from C# to unmanaged C++.

That is the price you pay for not having a delete operator and allowing the language to "manage" your memory.
 
I don't have a managed interface. I just used a DllImport Attribute and set up the call with a static extern.
 
Are you calling the function using the right calling convention? With the DllImport attribute you can specify which calling convention to use. If you're not sure, try them all, there are not so many of them.

If that's not the problem, why is it failing? Is it an access violation, a corrupted stack or something else?


Marcel
 
I did have the right calling convention set. I'm almost 100% positive I discovered the problem.

While I was working on the issue I got a BSOD with a memory error. That made me start thinking. Whenever I ran the process in VC++ 6 I only had Visual Studio 6 running. When I would test it from C# I opened up Visual Studio 2005 on top of everything else. That's when I had the problem. I ran a mem test and while it didn't display any errors, my computer rebooted in the middle of it :) I was pretty sure that I had bad memory at that point. So, I copied everything over to another development machine and ran the app. Went through with NO problems.

Now, it's time for me to go order some replacement memory :).

Thanks for the help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top