Solved

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

Posted on 2011-09-23
5
797 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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