Solved

C# calling unmanaged DLL failed under Windows 7, but successfully under XP

Posted on 2011-09-23
5
798 Views
Last Modified: 2013-12-17
I have an unmanaged 32bit DLL, which uses callback function.

When using unmanaged codes, such as Visual C++, Delphi, Visual Basic, all of them can call the DLL successfully under both Windows XP(SP3) and Windows 7(32bit and 64bit). I have made many tests without any problems.

When using C# to call the DLL under Windows XP(SP3), everything is still OK.

However, when using C# to call the DLL under Windows 7(32bit), I will always get the error message as follows:

 Error messagebox
After some time, the messagebox will ask me whether to debug or not, if I choose to debug, then Visual Studio 2008 will be loaded, as follows:

 VS 2008 Debug Interface
Then if I select the "Break" button, I will get into disassembled codes, as follows:

 VS 2008 Disassembly Codes
The C# program is built for .NET framework 3.5, and I have confirmed that .NET framework 3.5 has been installed on the system.

Can anyone tell me what is the problem? Thanks
0
Comment
Question by:chcw
5 Comments
 
LVL 5

Expert Comment

by:Didier Vally
ID: 36586226
Have you tried to change the .net framework version in your c# project ?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 167 total points
ID: 36586313
Part of the error message - priviliged instruction - I must admit this is something I don't recall ever seeing.  A quick web search gave the following link:
http://stackoverflow.com/questions/89607/what-is-a-privileged-instruction/89716

Which says that it is something that needs to be executed in ring 0 (usually your app would run in ring 3).
It could be something being stopped by the UAC of Win 7 - have you tried to run the app with administrator priviliges?  It could be a bug, but try running with elevated priviliges first - much simpler to try and maybe it then runs correctly.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 166 total points
ID: 36587701
Usually, the framework prevents you from calling privileged instructions from standard C# code.

But C# has a feature that enables you to use pointers, and then you fall out of that prevention zone.

Do you see the work unsafe somewhere in the code, either in front of a declaration, or in a structure such as the following?

unsafe
{
    // code with pointers.
}

If so, that might be the cause of your problem, and there is no solution. Windows XP will let go some operations with pointers that Windows 7, with its higher security, will not let pass. No matter is you are administator or not, you are limited in what you can do with pointers.
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 167 total points
ID: 36598388
Can you post the (un-managed) callback definition and API that registers the function address and your C# implementation along with documentation for the DLL?
0
 
LVL 8

Author Closing Comment

by:chcw
ID: 36618912
Thanks to all of you. I have solved the problem by myself. Acutally the problem is not caused by the DLL or the C# program. Instead, as I use a third-party tool to protect the DLL, which cause the problem. Now I have upgraded the third-party tool to a new version to solve the problem.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question